Skip to content

BUG/TST raise a more detailed error when GH6169 occurs, added a test #6200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3958,8 +3958,16 @@ def read(self, columns=None, **kwargs):
columns.insert(0, n)
df = super(AppendableMultiFrameTable, self).read(
columns=columns, **kwargs)
df = df.set_index(self.levels)

try:
df = df.set_index(self.levels)
except KeyError:
if kwargs.get('where') is not None and 'columns' in kwargs.get('where').expr:
raise KeyError(
"Indexes columns were not retrieved because you passed "
"a `where` argument containing columns specification. "
"(see http://github.com/pydata/pandas/issues/6169), try passing "
"the columns specification through the `columns` keyword instead"
)
# remove names for 'level_%d'
df.index = df.index.set_names([
None if self._re_levels.search(l) else l for l in df.index.names
Expand Down
27 changes: 27 additions & 0 deletions pandas/io/tests/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,33 @@ def make_index(names=None):
store.append('df',df)
tm.assert_frame_equal(store.select('df'),df)

def test_select_columns_in_where(self):

# GH 6169
# recreate multi-indexes when columns is passed
# in the `where` argument
index = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'],
['one', 'two', 'three']],
labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3],
[0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
names=['foo_name', 'bar_name'])

# With a DataFrame
df = DataFrame(np.random.randn(10, 3), index=index,
columns=['A', 'B', 'C'])

with ensure_clean_store(self.path) as store:
store.put('df', df, format='table')
tm.assert_frame_equal(store.select('df', where="columns=['A']"),df['A'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So at the time of this commit, the test fails. I'm not sure it's what's expected or if the test should assertRaises instead.

check_index_type=True,check_column_type=True)
# With a Serie
s = Series(np.random.randn(10), index=index,
name='A')
with ensure_clean_store(self.path) as store:
store.put('s', s)
tm.assert_frame_equal(store.select('s', where="columns=['A']"),s,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this should work like that... It is kind of absurd to pass a columns argument to select data from a Series object, so maybe that should rise an error (actually even if columns is passed as an argument to select directly an error should occur).

check_index_type=True,check_column_type=True)

def test_pass_spec_to_storer(self):

df = tm.makeDataFrame()
Expand Down