Skip to content

String dtype: allow string dtype in query/eval with default numexpr engine #59810

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

Conversation

jorisvandenbossche
Copy link
Member

Currently (on main, with no future options enabled), you can use query on an object-dtype column of strings. For exeample:

>>> df = pd.DataFrame({"col" : ['a', 'b', 'c']})
>>> df.query("col < 'b'") 
  col
0   a

This works despite that the default engine is "numexpr" (when installed) and that this engine does not actually evaluate the above expression. We do have checks to essentially fall back to evaluate the expression in python if one of the operands is object dtype (and the operator is one of the allowed operators for that).

But with the future default string dtype, we trigger the warning about explicitly falling back to the python engine:

>>> pd.options.future.infer_string = True
>>> df = pd.DataFrame({"col" : ['a', 'b', 'c']})
>>> df.query("col < 'b'")
RuntimeWarning: Engine has switched to 'python' because numexpr does not support extension array dtypes.
Please set your engine to python manually.
  col
0   a

Given that we were already automatically falling back for object dtype strings, I would propose to keep doing that for the future default string dtype as well.
It is a bit misleading (you might think to get a speedup when using this, which won't be the case), but that is already the case right now anyway, and otherwise a lot of users will start to see that warning (and a lot of people also use query for the convenience of writing the expression, I think, and not only for the speed).

xref #54792

@jorisvandenbossche jorisvandenbossche added Strings String extension data type and string data expressions pd.eval, query labels Sep 15, 2024
@mroeschke mroeschke added this to the 2.3 milestone Sep 16, 2024
@mroeschke mroeschke merged commit 013ac67 into pandas-dev:main Sep 16, 2024
45 of 53 checks passed
@mroeschke
Copy link
Member

Thanks @jorisvandenbossche

@jorisvandenbossche jorisvandenbossche deleted the string-dtype-query-eval branch September 16, 2024 17:42
jorisvandenbossche added a commit to jorisvandenbossche/pandas that referenced this pull request Oct 10, 2024
…ngine (pandas-dev#59810)

String dtype: allow string dtype in query/eval with default mumexpr engine
jorisvandenbossche added a commit that referenced this pull request Oct 10, 2024
…ngine (#59810)

String dtype: allow string dtype in query/eval with default mumexpr engine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backported expressions pd.eval, query Strings String extension data type and string data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants