Skip to content

Commit 2ed9ef3

Browse files
domsmrzAloqeely
authored andcommitted
BUG: Allow using numpy in DataFrame.eval and DataFrame.query via @-notation (pandas-dev#58057)
* Allow @np to be used within df.eval and df.query Add additional check to make sure that ndim of the provided variable is an int. This makes sure that the ndim guard doesn't trigger if it is something else (e.g., a function in case of a numpy). This allow for using @np in df.eval and df.query methods. * Add whatsnew * Fix typo Co-authored-by: Abdulaziz Aloqeely <[email protected]> * Test: skip checking names due to inconsistencies between OSes * Elaborate futher on whatsnew message * Fix the test by explicitly specifing engine. Also move the test to more appropriate file. --------- Co-authored-by: Abdulaziz Aloqeely <[email protected]>
1 parent 6abcfcb commit 2ed9ef3

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

pandas/core/computation/ops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def _resolve_name(self):
115115
res = self.env.resolve(local_name, is_local=is_local)
116116
self.update(res)
117117

118-
if hasattr(res, "ndim") and res.ndim > 2:
118+
if hasattr(res, "ndim") and isinstance(res.ndim, int) and res.ndim > 2:
119119
raise NotImplementedError(
120120
"N-dimensional objects, where N > 2, are not supported with eval"
121121
)

pandas/tests/frame/test_query_eval.py

+10
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,16 @@ def test_eval_object_dtype_binop(self):
188188
expected = DataFrame({"a1": ["Y", "N"], "c": [True, False]})
189189
tm.assert_frame_equal(res, expected)
190190

191+
def test_using_numpy(self, engine, parser):
192+
# GH 58041
193+
skip_if_no_pandas_parser(parser)
194+
df = Series([0.2, 1.5, 2.8], name="a").to_frame()
195+
res = df.eval("@np.floor(a)", engine=engine, parser=parser)
196+
expected = np.floor(df["a"])
197+
if engine == "numexpr":
198+
expected.name = None # See GH 58069
199+
tm.assert_series_equal(expected, res)
200+
191201

192202
class TestDataFrameQueryWithMultiIndex:
193203
def test_query_with_named_multiindex(self, parser, engine):

0 commit comments

Comments
 (0)