Skip to content

Fix read of py27 pytables tz attribute, gh#26443 #28221

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

Merged
merged 3 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/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 :meth:`HDFStore.__getitem__` incorrectly reading tz attribute created in Py2 (:issue:`26443`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Py2 -> Python 2.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think magic methods are in the docs.

It also seems like we don't have a generic HDFStore reference, so just write this as double backticks rather than :meth:.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will commit again.

-


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.
15 changes: 15 additions & 0 deletions pandas/tests/io/pytables/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -5446,3 +5446,18 @@ 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 Python2, but could not be read in
Python3.

GH26443
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is typically written as a comment.

"""
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)