Skip to content

Commit bde903f

Browse files
committed
BUG GH9057
Have DataFrame.to_hdf() raise an error when using pytables with non-string column types
1 parent fe735be commit bde903f

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

pandas/io/pytables.py

+24
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,30 @@ def _tables():
257257
def to_hdf(path_or_buf, key, value, mode=None, complevel=None, complib=None,
258258
append=None, **kwargs):
259259
""" store this object, close it if we opened it """
260+
261+
# PyTables has some limitations that we need to check for
262+
if kwargs.get('format', None) == 'table':
263+
if value.columns.dtype.kind == 'S':
264+
pass
265+
elif value.columns.dtype.kind == 'O':
266+
# PyTables doesn't support Unicode columns, but types 'O' can still
267+
# contain unicode values so check for that
268+
for col in value.columns:
269+
try:
270+
col.encode('ascii').decode('ascii')
271+
except UnicodeEncodeError:
272+
raise ValueError(
273+
"PyTables does not support UTF-8 column headers\n"
274+
"but header {} is at least not encodable to ASCII"
275+
.format(col)
276+
)
277+
else:
278+
raise ValueError(
279+
"Storing data in HDF with PyTables requires that the column\n"
280+
"types be of some string type or an object.\n"
281+
" Instead, your columns: {}\n"
282+
" are of type {}".format(value.columns, value.columns.dtype))
283+
260284
if append:
261285
f = lambda store: store.append(key, value, **kwargs)
262286
else:

pandas/io/tests/test_pytables.py

+13
Original file line numberDiff line numberDiff line change
@@ -4617,6 +4617,19 @@ def test_preserve_timedeltaindex_type(self):
46174617
store['df'] = df
46184618
assert_frame_equal(store['df'], df)
46194619

4620+
def test_to_hdf_with_integer_column_names(self):
4621+
# GH9057
4622+
df = DataFrame(columns=[5, 10], data=[[1, 0]])
4623+
4624+
with ensure_clean_path(self.path) as path:
4625+
self.assertRaises(ValueError, df.to_hdf, path, 'df', format='table', data_columns=True)
4626+
4627+
def test_to_hdf_with_unicode_column_names(self):
4628+
# GH9057
4629+
df = DataFrame(columns=[u('\u1234'), u('\u2345')], data=[[1, 0]])
4630+
4631+
with ensure_clean_path(self.path) as path:
4632+
self.assertRaises(ValueError, df.to_hdf, path, 'df', format='table', data_columns=True)
46204633

46214634
def _test_sort(obj):
46224635
if isinstance(obj, DataFrame):

0 commit comments

Comments
 (0)