From 7814a6654eff759d789238f177cb247fafecd42d Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 4 Dec 2016 12:21:44 -0500 Subject: [PATCH] [Backport #14791] BUG: multi-index HDFStore data_columns=True closes #14435 Author: Chris Closes #14791 from chris-b1/hdf-mi-datacolumns and squashes the following commits: 5d32610 [Chris] BUG: multi-index HDFStore data_columns=True (cherry picked from commit 27fcd811f5b5df89eeede049cd048d94a65e7ff4) --- doc/source/whatsnew/v0.19.2.txt | 2 +- pandas/io/pytables.py | 2 +- pandas/io/tests/test_pytables.py | 13 +++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.19.2.txt b/doc/source/whatsnew/v0.19.2.txt index cafbdb731f494..130653441fc0d 100644 --- a/doc/source/whatsnew/v0.19.2.txt +++ b/doc/source/whatsnew/v0.19.2.txt @@ -58,7 +58,7 @@ Bug Fixes - +- Bug ``HDFStore`` writing a ``MultiIndex`` when using ``data_columns=True`` (:issue:`14435`) - Bug in clipboard functions on linux with python2 with unicode and separators (:issue:`13747`) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index b8c2b146b6259..a5ef4e0688ea6 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -4254,7 +4254,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 = obj.columns.tolist() 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 72973105ff3bd..fe20eeda16ce2 100644 --- a/pandas/io/tests/test_pytables.py +++ b/pandas/io/tests/test_pytables.py @@ -1818,6 +1818,19 @@ def test_select_columns_in_where(self): store.put('s', s, format='table') tm.assert_series_equal(store.select('s', where="columns=['A']"), s) + def test_mi_data_columns(self): + # GH 14435 + idx = pd.MultiIndex.from_arrays([date_range('2000-01-01', periods=5), + range(5)], names=['date', 'id']) + df = pd.DataFrame({'a': [1.1, 1.2, 1.3, 1.4, 1.5]}, index=idx) + + with ensure_clean_store(self.path) as store: + store.append('df', df, data_columns=True) + + actual = store.select('df', where='id == 1') + expected = df.iloc[[1], :] + tm.assert_frame_equal(actual, expected) + def test_pass_spec_to_storer(self): df = tm.makeDataFrame()