Skip to content

Commit 09a17c7

Browse files
CLN: Expose arguments in DataFrame.query (#61413)
1 parent 7d8f5bd commit 09a17c7

File tree

1 file changed

+87
-10
lines changed

1 file changed

+87
-10
lines changed

pandas/core/frame.py

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4481,18 +4481,58 @@ def _get_item(self, item: Hashable) -> Series:
44814481

44824482
@overload
44834483
def query(
4484-
self, expr: str, *, inplace: Literal[False] = ..., **kwargs
4484+
self,
4485+
expr: str,
4486+
*,
4487+
parser: Literal["pandas", "python"] = ...,
4488+
engine: Literal["python", "numexpr"] | None = ...,
4489+
local_dict: dict[str, Any] | None = ...,
4490+
global_dict: dict[str, Any] | None = ...,
4491+
resolvers: list[Mapping] | None = ...,
4492+
level: int = ...,
4493+
inplace: Literal[False] = ...,
44854494
) -> DataFrame: ...
44864495

44874496
@overload
4488-
def query(self, expr: str, *, inplace: Literal[True], **kwargs) -> None: ...
4497+
def query(
4498+
self,
4499+
expr: str,
4500+
*,
4501+
parser: Literal["pandas", "python"] = ...,
4502+
engine: Literal["python", "numexpr"] | None = ...,
4503+
local_dict: dict[str, Any] | None = ...,
4504+
global_dict: dict[str, Any] | None = ...,
4505+
resolvers: list[Mapping] | None = ...,
4506+
level: int = ...,
4507+
inplace: Literal[True],
4508+
) -> None: ...
44894509

44904510
@overload
44914511
def query(
4492-
self, expr: str, *, inplace: bool = ..., **kwargs
4512+
self,
4513+
expr: str,
4514+
*,
4515+
parser: Literal["pandas", "python"] = ...,
4516+
engine: Literal["python", "numexpr"] | None = ...,
4517+
local_dict: dict[str, Any] | None = ...,
4518+
global_dict: dict[str, Any] | None = ...,
4519+
resolvers: list[Mapping] | None = ...,
4520+
level: int = ...,
4521+
inplace: bool = ...,
44934522
) -> DataFrame | None: ...
44944523

4495-
def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | None:
4524+
def query(
4525+
self,
4526+
expr: str,
4527+
*,
4528+
parser: Literal["pandas", "python"] = "pandas",
4529+
engine: Literal["python", "numexpr"] | None = None,
4530+
local_dict: dict[str, Any] | None = None,
4531+
global_dict: dict[str, Any] | None = None,
4532+
resolvers: list[Mapping] | None = None,
4533+
level: int = 0,
4534+
inplace: bool = False,
4535+
) -> DataFrame | None:
44964536
"""
44974537
Query the columns of a DataFrame with a boolean expression.
44984538
@@ -4511,11 +4551,41 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | No
45114551
45124552
See the documentation for :meth:`DataFrame.eval` for details on
45134553
referring to column names and variables in the query string.
4554+
parser : {'pandas', 'python'}, default 'pandas'
4555+
The parser to use to construct the syntax tree from the expression. The
4556+
default of ``'pandas'`` parses code slightly different than standard
4557+
Python. Alternatively, you can parse an expression using the
4558+
``'python'`` parser to retain strict Python semantics. See the
4559+
:ref:`enhancing performance <enhancingperf.eval>` documentation for
4560+
more details.
4561+
engine : {'python', 'numexpr'}, default 'numexpr'
4562+
4563+
The engine used to evaluate the expression. Supported engines are
4564+
4565+
- None : tries to use ``numexpr``, falls back to ``python``
4566+
- ``'numexpr'`` : This default engine evaluates pandas objects using
4567+
numexpr for large speed ups in complex expressions with large frames.
4568+
- ``'python'`` : Performs operations as if you had ``eval``'d in top
4569+
level python. This engine is generally not that useful.
4570+
4571+
More backends may be available in the future.
4572+
local_dict : dict or None, optional
4573+
A dictionary of local variables, taken from locals() by default.
4574+
global_dict : dict or None, optional
4575+
A dictionary of global variables, taken from globals() by default.
4576+
resolvers : list of dict-like or None, optional
4577+
A list of objects implementing the ``__getitem__`` special method that
4578+
you can use to inject an additional collection of namespaces to use for
4579+
variable lookup. For example, this is used in the
4580+
:meth:`~DataFrame.query` method to inject the
4581+
``DataFrame.index`` and ``DataFrame.columns``
4582+
variables that refer to their respective :class:`~pandas.DataFrame`
4583+
instance attributes.
4584+
level : int, optional
4585+
The number of prior stack frames to traverse and add to the current
4586+
scope. Most users will **not** need to change this parameter.
45144587
inplace : bool
45154588
Whether to modify the DataFrame rather than creating a new one.
4516-
**kwargs
4517-
See the documentation for :func:`eval` for complete details
4518-
on the keyword arguments accepted by :meth:`DataFrame.query`.
45194589
45204590
Returns
45214591
-------
@@ -4628,10 +4698,17 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | No
46284698
if not isinstance(expr, str):
46294699
msg = f"expr must be a string to be evaluated, {type(expr)} given"
46304700
raise ValueError(msg)
4631-
kwargs["level"] = kwargs.pop("level", 0) + 1
4632-
kwargs["target"] = None
46334701

4634-
res = self.eval(expr, **kwargs)
4702+
res = self.eval(
4703+
expr,
4704+
level=level + 1,
4705+
parser=parser,
4706+
target=None,
4707+
engine=engine,
4708+
local_dict=local_dict,
4709+
global_dict=global_dict,
4710+
resolvers=resolvers or (),
4711+
)
46354712

46364713
try:
46374714
result = self.loc[res]

0 commit comments

Comments
 (0)