diff --git a/doc/source/io.rst b/doc/source/io.rst index 36d4bd89261c4..415e1a7ddaf07 100644 --- a/doc/source/io.rst +++ b/doc/source/io.rst @@ -2808,8 +2808,8 @@ Storing Mixed Types in a Table ++++++++++++++++++++++++++++++ Storing mixed-dtype data is supported. Strings are stored as a -fixed-width using the maximum size of the appended column. Subsequent -appends will truncate strings at this length. +fixed-width using the maximum size of the appended column. Subsequent attempts +at appending longer strings will raise a ``ValueError``. Passing ``min_itemsize={`values`: size}`` as a parameter to append will set a larger minimum for the string columns. Storing ``floats, diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index cd8f7699a85d1..ffe4f4d4cae89 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -488,6 +488,9 @@ def keys(self): """ return [n._v_pathname for n in self.groups()] + def __iter__(self): + return iter(self.keys()) + def items(self): """ iterate on key->group @@ -3247,7 +3250,7 @@ def validate_data_columns(self, data_columns, min_itemsize): # evaluate the passed data_columns, True == use all columns # take only valide axis labels if data_columns is True: - data_columns = axis_labels + data_columns = list(axis_labels) elif data_columns is None: data_columns = [] @@ -4084,7 +4087,7 @@ def write(self, obj, data_columns=None, **kwargs): obj = DataFrame({name: obj}, index=obj.index) obj.columns = [name] return super(AppendableSeriesTable, self).write( - obj=obj, data_columns=obj.columns, **kwargs) + obj=obj, data_columns=list(obj.columns), **kwargs) def read(self, columns=None, **kwargs): @@ -4185,7 +4188,7 @@ def write(self, obj, data_columns=None, **kwargs): if data_columns is None: data_columns = [] elif data_columns is True: - data_columns = obj.columns[:] + data_columns = list(obj.columns[:]) obj, self.levels = self.validate_multiindex(obj) for n in self.levels: if n not in data_columns: diff --git a/pandas/io/tests/test_pytables.py b/pandas/io/tests/test_pytables.py index b08d24747bcd3..77ac1f90472fc 100644 --- a/pandas/io/tests/test_pytables.py +++ b/pandas/io/tests/test_pytables.py @@ -389,8 +389,9 @@ def test_keys(self): store['d'] = tm.makePanel() store['foo/bar'] = tm.makePanel() self.assertEqual(len(store), 5) - self.assertTrue(set( - store.keys()) == set(['/a', '/b', '/c', '/d', '/foo/bar'])) + expected = set(['/a', '/b', '/c', '/d', '/foo/bar']) + self.assertTrue(set(store.keys()) == expected) + self.assertTrue(set(store) == expected) def test_repr(self): @@ -1338,6 +1339,16 @@ def check_col(key, name, size): [[124, 'abcdefqhij'], [346, 'abcdefghijklmnopqrtsuvwxyz']]) self.assertRaises(ValueError, store.append, 'df_new', df_new) + # min_itemsize on Series with Multiindex (GH 10381) + df = tm.makeMixedDataFrame().set_index(['A', 'C']) + store.append('ss', df['B'], min_itemsize={'index': 4}) + tm.assert_series_equal(store.select('ss'), df['B']) + + # min_itemsize with MultiIndex and data_columns=True + store.append('midf', df, data_columns=True, + min_itemsize={'index': 4}) + tm.assert_frame_equal(store.select('midf'), df) + # with nans _maybe_remove(store, 'df') df = tm.makeTimeDataFrame()