Skip to content

Commit

Permalink
Fix read of py27 pytables tz attribute, gh#26443 (#28221)
Browse files Browse the repository at this point in the history
  • Loading branch information
quintusdias authored and WillAyd committed Aug 30, 2019
1 parent fadb271 commit cad3918
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Datetimelike
^^^^^^^^^^^^
- Bug in :meth:`Series.__setitem__` incorrectly casting ``np.timedelta64("NaT")`` to ``np.datetime64("NaT")`` when inserting into a :class:`Series` with datetime64 dtype (:issue:`27311`)
- Bug in :meth:`Series.dt` property lookups when the underlying data is read-only (:issue:`27529`)
- Bug in ``HDFStore.__getitem__`` incorrectly reading tz attribute created in Python 2 (:issue:`26443`)
-


Expand Down
7 changes: 6 additions & 1 deletion pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2902,7 +2902,12 @@ def read_index_node(self, node, start=None, stop=None):
kwargs["freq"] = node._v_attrs["freq"]

if "tz" in node._v_attrs:
kwargs["tz"] = node._v_attrs["tz"]
if isinstance(node._v_attrs["tz"], bytes):
# created by python2
kwargs["tz"] = node._v_attrs["tz"].decode("utf-8")
else:
# created by python3
kwargs["tz"] = node._v_attrs["tz"]

if kind in ("date", "datetime"):
index = factory(
Expand Down
Binary file added pandas/tests/io/data/legacy_hdf/gh26443.h5
Binary file not shown.
13 changes: 13 additions & 0 deletions pandas/tests/io/pytables/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -5447,3 +5447,16 @@ def test_read_with_where_tz_aware_index(self):
store.append(key, expected, format="table", append=True)
result = pd.read_hdf(path, key, where="DATE > 20151130")
assert_frame_equal(result, expected)

def test_py2_created_with_datetimez(self, datapath):
# The test HDF5 file was created in Python 2, but could not be read in
# Python 3.
#
# GH26443
index = [pd.Timestamp("2019-01-01T18:00").tz_localize("America/New_York")]
expected = DataFrame({"data": 123}, index=index)
with ensure_clean_store(
datapath("io", "data", "legacy_hdf", "gh26443.h5"), mode="r"
) as store:
result = store["key"]
assert_frame_equal(result, expected)

0 comments on commit cad3918

Please sign in to comment.