Skip to content

BUG: allow single element bool queries #6163

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

Merged
merged 1 commit into from
Jan 29, 2014
Merged
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
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ Bug Fixes
a datetimelike (:issue:`6152`)
- Fixed a stack overflow bug in ``query``/``eval`` during lexicographic
string comparisons (:issue:`6155`).
- Fixed a bug in ``query`` where the index of a single-element ``Series`` was
being thrown away (:issue:`6148`).

pandas 0.13.0
-------------
Expand Down
6 changes: 1 addition & 5 deletions pandas/computation/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,12 @@ def wrapper(terms):
return _align_core_single_unary_op(terms[0])

term_values = (term.value for term in terms)

# only scalars or indexes
if all(isinstance(term.value, pd.Index) or term.isscalar for term in
terms):
return np.result_type(*term_values), None

# single element ndarrays
all_has_size = all(hasattr(term.value, 'size') for term in terms)
if all_has_size and all(term.value.size == 1 for term in terms):
return np.result_type(*term_values), None

# no pandas objects
Copy link
Contributor

Choose a reason for hiding this comment

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

out of curiosity why was this squeezing in here? (IIRC you wanted to support scalar booleans, e.g. True/False)?

Copy link
Member Author

Choose a reason for hiding this comment

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

IIRC it was because I wanted to support numpy scalars...turns out not necessary to make that check to do that

Copy link
Contributor

Choose a reason for hiding this comment

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

gr8...go ahead and merge on green then

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm trying out some pathological cases to make sure that I didn't break something we didn't write tests for (if that's even a thing) :)

if not _any_pandas_objects(terms):
return np.result_type(*term_values), None
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -12860,6 +12860,20 @@ def test_query_lex_compare_strings(self):
for parser, engine in product(PARSERS, ENGINES):
yield self.check_query_lex_compare_strings, parser, engine

def check_query_single_element_booleans(self, parser, engine):
tm.skip_if_no_ne(engine)
columns = 'bid', 'bidsize', 'ask', 'asksize'
data = np.random.randint(2, size=(1, len(columns))).astype(bool)
df = DataFrame(data, columns=columns)
res = df.query('bid & ask', engine=engine, parser=parser)
expected = df[df.bid & df.ask]
assert_frame_equal(res, expected)

def test_query_single_element_booleans(self):
for parser, engine in product(PARSERS, ENGINES):
yield self.check_query_single_element_booleans, parser, engine


class TestDataFrameEvalNumExprPandas(tm.TestCase):

@classmethod
Expand Down