Skip to content

Commit cad3918

Browse files
quintusdiasWillAyd
authored andcommitted
Fix read of py27 pytables tz attribute, gh#26443 (#28221)
1 parent fadb271 commit cad3918

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Datetimelike
9797
^^^^^^^^^^^^
9898
- 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`)
9999
- Bug in :meth:`Series.dt` property lookups when the underlying data is read-only (:issue:`27529`)
100+
- Bug in ``HDFStore.__getitem__`` incorrectly reading tz attribute created in Python 2 (:issue:`26443`)
100101
-
101102

102103

pandas/io/pytables.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2902,7 +2902,12 @@ def read_index_node(self, node, start=None, stop=None):
29022902
kwargs["freq"] = node._v_attrs["freq"]
29032903

29042904
if "tz" in node._v_attrs:
2905-
kwargs["tz"] = node._v_attrs["tz"]
2905+
if isinstance(node._v_attrs["tz"], bytes):
2906+
# created by python2
2907+
kwargs["tz"] = node._v_attrs["tz"].decode("utf-8")
2908+
else:
2909+
# created by python3
2910+
kwargs["tz"] = node._v_attrs["tz"]
29062911

29072912
if kind in ("date", "datetime"):
29082913
index = factory(
7 KB
Binary file not shown.

pandas/tests/io/pytables/test_pytables.py

+13
Original file line numberDiff line numberDiff line change
@@ -5447,3 +5447,16 @@ def test_read_with_where_tz_aware_index(self):
54475447
store.append(key, expected, format="table", append=True)
54485448
result = pd.read_hdf(path, key, where="DATE > 20151130")
54495449
assert_frame_equal(result, expected)
5450+
5451+
def test_py2_created_with_datetimez(self, datapath):
5452+
# The test HDF5 file was created in Python 2, but could not be read in
5453+
# Python 3.
5454+
#
5455+
# GH26443
5456+
index = [pd.Timestamp("2019-01-01T18:00").tz_localize("America/New_York")]
5457+
expected = DataFrame({"data": 123}, index=index)
5458+
with ensure_clean_store(
5459+
datapath("io", "data", "legacy_hdf", "gh26443.h5"), mode="r"
5460+
) as store:
5461+
result = store["key"]
5462+
assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)