Skip to content

Commit 3130903

Browse files
clslgrncLloyd Wallis
authored and
Lloyd Wallis
committed
Fix tz localize (influxdata#684)
* fix already tz-aware error * fix tests tz_localize * update CHANGELOG.md
1 parent 6d8ee71 commit 3130903

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99
### Added
1010

1111
### Changed
12+
- Fix 'TypeError: Already tz-aware' introduced with recent versions of Panda (#671, #676, thx @f4bsch @clslgrnc)
1213

1314
### Removed
1415

influxdb/_dataframe_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ def _to_dataframe(self, rs, dropna=True):
202202
df = pd.DataFrame(data)
203203
df.time = pd.to_datetime(df.time)
204204
df.set_index('time', inplace=True)
205-
df.index = df.index.tz_localize('UTC')
205+
if df.index.tzinfo is None:
206+
df.index = df.index.tz_localize('UTC')
206207
df.index.name = None
207208
result[key].append(df)
208209
for key, data in result.items():

influxdb/tests/dataframe_client_test.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -818,13 +818,15 @@ def test_query_into_dataframe(self):
818818
pd1 = pd.DataFrame(
819819
[[23422]], columns=['value'],
820820
index=pd.to_datetime(["2009-11-10T23:00:00Z"]))
821-
pd1.index = pd1.index.tz_localize('UTC')
821+
if pd1.index.tzinfo is None:
822+
pd1.index = pd1.index.tz_localize('UTC')
822823
pd2 = pd.DataFrame(
823824
[[23422], [23422], [23422]], columns=['value'],
824825
index=pd.to_datetime(["2009-11-10T23:00:00Z",
825826
"2009-11-10T23:00:00Z",
826827
"2009-11-10T23:00:00Z"]))
827-
pd2.index = pd2.index.tz_localize('UTC')
828+
if pd2.index.tzinfo is None:
829+
pd2.index = pd2.index.tz_localize('UTC')
828830
expected = {
829831
('network', (('direction', ''),)): pd1,
830832
('network', (('direction', 'in'),)): pd2
@@ -871,11 +873,14 @@ def test_multiquery_into_dataframe(self):
871873
index=pd.to_datetime([
872874
"2015-01-29 21:55:43.702900257+0000",
873875
"2015-01-29 21:55:43.702900257+0000",
874-
"2015-06-11 20:46:02+0000"])).tz_localize('UTC')
876+
"2015-06-11 20:46:02+0000"]))
877+
if pd1.index.tzinfo is None:
878+
pd1.index = pd1.index.tz_localize('UTC')
875879
pd2 = pd.DataFrame(
876880
[[3]], columns=['count'],
877-
index=pd.to_datetime(["1970-01-01 00:00:00+00:00"]))\
878-
.tz_localize('UTC')
881+
index=pd.to_datetime(["1970-01-01 00:00:00+00:00"]))
882+
if pd2.index.tzinfo is None:
883+
pd2.index = pd2.index.tz_localize('UTC')
879884
expected = [{'cpu_load_short': pd1}, {'cpu_load_short': pd2}]
880885

881886
cli = DataFrameClient('host', 8086, 'username', 'password', 'db')

0 commit comments

Comments
 (0)