Skip to content

Fixes timezone drop when encoding it in msgpack #25195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Bug Fixes
**I/O**

- Bug in reading a HDF5 table-format ``DataFrame`` created in Python 2, in Python 3 (:issue:`24925`)
- Bug in retaining timezone in :meth:`read_msgpack` when saving and loading the :class"`DataFrame` from ``msgpack``. (:issue:`25148`)
- Bug in reading a JSON with ``orient='table'`` generated by :meth:`DataFrame.to_json` with ``index=False`` (:issue:`25170`)
- Bug where float indexes could have misaligned values when printing (:issue:`25061`)
-
Expand Down
26 changes: 23 additions & 3 deletions pandas/io/packers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
from textwrap import dedent
import warnings

import dateutil
from dateutil.parser import parse
import numpy as np
from pytz import FixedOffset, _FixedOffset

import pandas.compat as compat
from pandas.compat import u, u_safe
Expand Down Expand Up @@ -387,10 +389,19 @@ def encode(obj):
u'compress': compressor}
elif isinstance(obj, DatetimeIndex):
tz = getattr(obj, 'tz', None)

tztype = None
# store tz info and data as UTC
if tz is not None:
tz = u(tz.zone)
if isinstance(tz, _FixedOffset):
tz, tztype = u(str(tz._minutes)), u('fixedoffset')
elif isinstance(tz, dateutil.tz.tzutc):
tz, tztype = u('UTC'), u('tzutc')
elif isinstance(tz, dateutil.tz.tzfile):
tz, tztype = u(tz._filename), u('tzfile')
elif isinstance(tz, dateutil.tz.tzlocal):
tz, tztype = u('local'), u('tzlocal')
else:
tz, tztype = u(tz.zone), u('zone')
obj = obj.tz_convert('UTC')
return {u'typ': u'datetime_index',
u'klass': u(obj.__class__.__name__),
Expand All @@ -399,6 +410,7 @@ def encode(obj):
u'data': convert(obj.asi8),
u'freq': u_safe(getattr(obj, 'freqstr', None)),
u'tz': tz,
u'tztype': tztype,
u'compress': compressor}
elif isinstance(obj, (IntervalIndex, IntervalArray)):
if isinstance(obj, IntervalIndex):
Expand Down Expand Up @@ -609,7 +621,15 @@ def decode(obj):
d = dict(name=obj[u'name'], freq=obj[u'freq'])
result = DatetimeIndex(data, **d)
tz = obj[u'tz']

tztype = obj['tztype'] if 'tztype' in obj.keys() else None
if tztype == u'fixedoffset':
tz = FixedOffset(int(tz))
elif tztype == u'tzfile':
tz = dateutil.tz.gettz(tz)
elif tztype == u'tzlocal':
tz = dateutil.tz.tzlocal()
elif tztype == u'tzutc':
tz = dateutil.tz.tzutc()
# reverse tz conversion
if tz is not None:
result = result.tz_localize('UTC').tz_convert(tz)
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/io/test_packers.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,13 @@ def categorical_index(self):
result = self.encode_decode(df)
tm.assert_frame_equal(result, df)

def test_timezone_encode_decode(self, tz_aware_fixture):
tz = tz_aware_fixture
expected = date_range(start='2018-12-02 14:50:00',
end='2018-12-03 03:11:00', freq='1min', tz=tz)
result = self.encode_decode(expected)
tm.assert_index_equal(result, expected)


class TestSeries(TestPackers):

Expand Down