Skip to content

Commit 586ac54

Browse files
committed
util/fromjson: handle pandas 0.25 behaviour change
Pandas for a long time wrongly serialized naive timestamps within an utc time zone (see pandas-dev/pandas#29706). We work around the 0.25 fix that produces correct utc stamps from such serialized stamps.
1 parent 3545928 commit 586ac54

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

test/test_util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from tshistory.util import (
77
bisect_search,
8+
fromjson,
89
nary_pack,
910
nary_unpack,
1011
)
@@ -33,6 +34,14 @@ def test_json():
3334
assert series2.index.dtype.tz.zone == 'UTC'
3435
assert not series.equals(series2)
3536

37+
series2 = fromjson(jsonseries, 'foo', tzaware=False)
38+
if pd.__version__.startswith('0.24'):
39+
assert not getattr(series2.index.dtype, 'tz', False)
40+
assert series.equals(series2)
41+
elif pd.__version__.startswith('0.25'):
42+
assert not getattr(series2.index.dtype, 'tz', False)
43+
assert series.equals(series2)
44+
3645

3746
def test_bisect():
3847
values = [-4, -2, 1, 7]

tshistory/util.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,14 @@ def tojson(ts, precision=1e-14):
271271

272272
def fromjson(jsonb, tsname, tzaware=False):
273273
series = _fromjson(jsonb, tsname).fillna(value=np.nan)
274-
if tzaware and not getattr(series.index.dtype, 'tz', None):
275-
# from pandas 0.25 we are already good
276-
series.index = series.index.tz_localize('utc')
274+
if tzaware:
275+
if not getattr(series.index.dtype, 'tz', None):
276+
# from pandas 0.25 we are already good
277+
series.index = series.index.tz_localize('utc')
278+
else:
279+
if getattr(series.index.dtype, 'tz', None):
280+
# from pandas 0.25 we are now bad
281+
series.index = series.index.tz_localize(None)
277282
return series
278283

279284

0 commit comments

Comments
 (0)