Skip to content

Commit b45c333

Browse files
committed
BUG: Fix read of py3 PeriodIndex DataFrame HDF made in py2 (pandas-dev#16781)
In Python3, reading a DataFrame with a PeriodIndex from an HDF file created in Python2 would incorrectly return a DataFrame with an Int64Index.
1 parent 664348c commit b45c333

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

pandas/io/pytables.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2591,8 +2591,8 @@ def read_index_node(self, node, start=None, stop=None):
25912591
if 'name' in node._v_attrs:
25922592
name = _ensure_str(node._v_attrs.name)
25932593

2594-
index_class = self._alias_to_class(getattr(node._v_attrs,
2595-
'index_class', ''))
2594+
index_class = self._alias_to_class(_ensure_decoded(
2595+
getattr(node._v_attrs, 'index_class', '')))
25962596
factory = self._get_index_factory(index_class)
25972597

25982598
kwargs = {}

pandas/tests/io/data/testpi.h5

7.14 KB
Binary file not shown.

pandas/tests/io/test_pytables.py

+13
Original file line numberDiff line numberDiff line change
@@ -5264,6 +5264,19 @@ def test_fspath(self):
52645264
with pd.HDFStore(path) as store:
52655265
assert os.fspath(store) == str(path)
52665266

5267+
def test_read_py2_hdf_file_in_py3(self):
5268+
# GH 16781
5269+
5270+
# tests reading a PeriodIndex DataFrame written in Python2 in Python3
5271+
expected = pd.DataFrame([1., 2, 3], index=pd.PeriodIndex(
5272+
['2015-01-01', '2015-01-02', '2015-01-05'], freq='B'))
5273+
5274+
with ensure_clean_store(
5275+
tm.get_data_path('testpi.h5'),
5276+
mode='r') as store:
5277+
result = store['p']
5278+
assert_frame_equal(result, expected)
5279+
52675280

52685281
class TestHDFComplexValues(Base):
52695282
# GH10447

0 commit comments

Comments
 (0)