Skip to content

Commit ae824d9

Browse files
committed
BUG: Fixes reading in Py3 PeriodIndex DataFrame HDF created in Py2 (pandas-dev#16781)
Reading a DataFrame with a PeriodIndex from an HDF file created in Python2 in Python3 would incorrectly make the index an In64Index.
1 parent 664348c commit ae824d9

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-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

+12
Original file line numberDiff line numberDiff line change
@@ -5264,6 +5264,18 @@ 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)
52675279

52685280
class TestHDFComplexValues(Base):
52695281
# GH10447

0 commit comments

Comments
 (0)