Skip to content

Commit

Permalink
BUG: Fix read of py3 PeriodIndex DataFrame HDF made in py2 (#16781)
Browse files Browse the repository at this point in the history
In Python3, reading a DataFrame with a PeriodIndex from an HDF file
created in Python2 would incorrectly return a DataFrame with an
Int64Index.
  • Loading branch information
forbdonut committed Jun 28, 2017
1 parent 664348c commit 86f1edb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2591,8 +2591,8 @@ def read_index_node(self, node, start=None, stop=None):
if 'name' in node._v_attrs:
name = _ensure_str(node._v_attrs.name)

index_class = self._alias_to_class(getattr(node._v_attrs,
'index_class', ''))
index_class = self._alias_to_class(_ensure_decoded(
getattr(node._v_attrs, 'index_class', '')))
factory = self._get_index_factory(index_class)

kwargs = {}
Expand Down
Binary file added pandas/tests/io/data/testpi.h5
Binary file not shown.
12 changes: 12 additions & 0 deletions pandas/tests/io/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -5264,6 +5264,18 @@ def test_fspath(self):
with pd.HDFStore(path) as store:
assert os.fspath(store) == str(path)

def test_read_py2_hdf_file_in_py3(self):
# GH 16781

# tests reading a PeriodIndex DataFrame written in Python2 in Python3
expected = pd.DataFrame([1.,2,3], index=pd.PeriodIndex(
['2015-01-01', '2015-01-02', '2015-01-05'], freq='B'))

with ensure_clean_store(
tm.get_data_path('testpi.h5'),
mode='r') as store:
result = store['p']
assert_frame_equal(result, expected)

class TestHDFComplexValues(Base):
# GH10447
Expand Down

0 comments on commit 86f1edb

Please sign in to comment.