Skip to content

Commit 5147aed

Browse files
clslgrnctux-00
authored andcommitted
Fix tz localize (influxdata#684)
* fix already tz-aware error * fix tests tz_localize * update CHANGELOG.md
1 parent 2d17360 commit 5147aed

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
@@ -847,13 +847,15 @@ def test_query_into_dataframe(self):
847847
pd1 = pd.DataFrame(
848848
[[23422]], columns=['value'],
849849
index=pd.to_datetime(["2009-11-10T23:00:00Z"]))
850-
pd1.index = pd1.index.tz_localize('UTC')
850+
if pd1.index.tzinfo is None:
851+
pd1.index = pd1.index.tz_localize('UTC')
851852
pd2 = pd.DataFrame(
852853
[[23422], [23422], [23422]], columns=['value'],
853854
index=pd.to_datetime(["2009-11-10T23:00:00Z",
854855
"2009-11-10T23:00:00Z",
855856
"2009-11-10T23:00:00Z"]))
856-
pd2.index = pd2.index.tz_localize('UTC')
857+
if pd2.index.tzinfo is None:
858+
pd2.index = pd2.index.tz_localize('UTC')
857859
expected = {
858860
('network', (('direction', ''),)): pd1,
859861
('network', (('direction', 'in'),)): pd2
@@ -900,11 +902,14 @@ def test_multiquery_into_dataframe(self):
900902
index=pd.to_datetime([
901903
"2015-01-29 21:55:43.702900257+0000",
902904
"2015-01-29 21:55:43.702900257+0000",
903-
"2015-06-11 20:46:02+0000"])).tz_localize('UTC')
905+
"2015-06-11 20:46:02+0000"]))
906+
if pd1.index.tzinfo is None:
907+
pd1.index = pd1.index.tz_localize('UTC')
904908
pd2 = pd.DataFrame(
905909
[[3]], columns=['count'],
906-
index=pd.to_datetime(["1970-01-01 00:00:00+00:00"]))\
907-
.tz_localize('UTC')
910+
index=pd.to_datetime(["1970-01-01 00:00:00+00:00"]))
911+
if pd2.index.tzinfo is None:
912+
pd2.index = pd2.index.tz_localize('UTC')
908913
expected = [{'cpu_load_short': pd1}, {'cpu_load_short': pd2}]
909914

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

0 commit comments

Comments
 (0)