Skip to content

Commit bcb9497

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 bcb9497

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

doc/source/whatsnew/v0.20.3.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Performance Improvements
3737
Bug Fixes
3838
~~~~~~~~~
3939
- Fixed issue with dataframe scatter plot for categorical data that reports incorrect column key not found when categorical data is used for plotting (:issue:`16199`)
40-
40+
- Fixed issue with loading a DataFrame with a PeriodIndex in Python3 that was written in Python2 where the index would come back as a Int64Index (:issue:`16781`)
4141

4242

4343

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)