Skip to content

Commit ab75d27

Browse files
committed
BUG: Handle numpy strings in index names in HDF5 pandas-dev#13492
1 parent fb47ee5 commit ab75d27

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

doc/source/whatsnew/v0.20.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ I/O
7070
- Bug that raised IndexError HTML-rendering an empty DataFrame (:issue:`15953`)
7171
- Bug in ``pd.read_csv()`` in which tarfile object inputs were raising an error in Python 2.x for the C engine (:issue:`16530`)
7272
- Bug where ``DataFrame.to_html()`` ignored the ``index_names`` parameter (:issue:`16493`)
73+
- Bug where ``pd.read_hdf()`` returns numpy strings for index names (:issue:`13492`)
7374

7475
- Bug in ``HDFStore.select_as_multiple()`` where start/stop arguments were not respected (:issue:`16209`)
7576

pandas/io/pytables.py

+2
Original file line numberDiff line numberDiff line change
@@ -2568,6 +2568,8 @@ def read_index_node(self, node, start=None, stop=None):
25682568

25692569
if 'name' in node._v_attrs:
25702570
name = node._v_attrs.name
2571+
if isinstance(name, compat.string_types):
2572+
name = compat.text_type(name)
25712573

25722574
index_class = self._alias_to_class(getattr(node._v_attrs,
25732575
'index_class', ''))

pandas/tests/io/test_pytables.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
date_range, timedelta_range, Index, DatetimeIndex,
1717
isnull)
1818

19-
from pandas.compat import is_platform_windows, PY3, PY35, BytesIO
19+
from pandas.compat import is_platform_windows, PY3, PY35, BytesIO, text_type
2020
from pandas.io.formats.printing import pprint_thing
2121

2222
tables = pytest.importorskip('tables')
@@ -2920,6 +2920,27 @@ def test_store_index_name_with_tz(self):
29202920
recons = store['frame']
29212921
tm.assert_frame_equal(recons, df)
29222922

2923+
@pytest.mark.parametrize('table_format', ['table', 'fixed'])
2924+
def test_store_index_name_numpy_str(self, table_format):
2925+
# GH #13492
2926+
idx = pd.Index(pd.to_datetime([datetime.date(2000, 1, 1),
2927+
datetime.date(2000, 1, 2)]),
2928+
name=u('cols\u05d2'))
2929+
idx1 = pd.Index(pd.to_datetime([datetime.date(2010, 1, 1),
2930+
datetime.date(2010, 1, 2)]),
2931+
name=u('rows\u05d0'))
2932+
df = pd.DataFrame(np.arange(4).reshape(2, 2), columns=idx, index=idx1)
2933+
2934+
# This used to fail, returning numpy strings instead of python strings.
2935+
with ensure_clean_path(self.path) as path:
2936+
df.to_hdf(path, 'df', format=table_format)
2937+
df2 = read_hdf(path, 'df')
2938+
2939+
assert_frame_equal(df, df2, check_names=True)
2940+
2941+
assert type(df2.index.name) == text_type
2942+
assert type(df2.columns.name) == text_type
2943+
29232944
def test_store_series_name(self):
29242945
df = tm.makeDataFrame()
29252946
series = df['A']

0 commit comments

Comments
 (0)