Skip to content

Commit

Permalink
BUG: Fix read of py3 PeriodIndex DataFrame HDF made in py2 (pandas-de…
Browse files Browse the repository at this point in the history
…v#16781) (pandas-dev#16790)

In Python3, reading a DataFrame with a PeriodIndex from an HDF file
created in Python2 would incorrectly return a DataFrame with an
Int64Index.

(cherry picked from commit 794e060)
  • Loading branch information
forbdonut authored and TomAugspurger committed Jul 6, 2017
1 parent 222ebc4 commit 8122288
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Bug Fixes
~~~~~~~~~
- 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`)
- Fixed a pytest marker failing downstream packages' tests suites (:issue:`16680`)
- Fixed compat with loading a ``DataFrame`` with a ``PeriodIndex``, from a ``format='fixed'`` HDFStore, in Python 3, that was written in Python 2 (:issue:`16781`)


Conversion
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2584,8 +2584,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 not shown.
20 changes: 20 additions & 0 deletions pandas/tests/io/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -5207,6 +5207,26 @@ 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

# the file was generated in Python 2.7 like so:
#
# df = pd.DataFrame([1.,2,3], index=pd.PeriodIndex(
# ['2015-01-01', '2015-01-02', '2015-01-05'], freq='B'))
# df.to_hdf('periodindex_0.20.1_x86_64_darwin_2.7.13.h5', 'p')

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('periodindex_0.20.1_x86_64_darwin_2.7.13.h5'),
mode='r') as store:
result = store['p']
assert_frame_equal(result, expected)


class TestHDFComplexValues(Base):
# GH10447
Expand Down

0 comments on commit 8122288

Please sign in to comment.