From 73559c3dfd05e18a9c672fd2d0a7067d5837433a Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Tue, 28 Jan 2014 22:32:18 -0500 Subject: [PATCH] BUG: allow single element bool queries --- doc/source/release.rst | 2 ++ pandas/computation/align.py | 6 +----- pandas/tests/test_frame.py | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index a79182af13955..e867f0f27a646 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -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 ------------- diff --git a/pandas/computation/align.py b/pandas/computation/align.py index 757524965bbef..9fe563574bbd4 100644 --- a/pandas/computation/align.py +++ b/pandas/computation/align.py @@ -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 if not _any_pandas_objects(terms): return np.result_type(*term_values), None diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 4578375ab7dad..29edbd4273fec 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -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