@@ -4481,18 +4481,58 @@ def _get_item(self, item: Hashable) -> Series:
4481
4481
4482
4482
@overload
4483
4483
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 ]= ...,
4485
4494
)-> DataFrame : ...
4486
4495
4487
4496
@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 : ...
4489
4509
4490
4510
@overload
4491
4511
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 = ...,
4493
4522
)-> DataFrame | None : ...
4494
4523
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 :
4496
4536
"""
4497
4537
Query the columns of a DataFrame with a boolean expression.
4498
4538
@@ -4511,11 +4551,41 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | No
4511
4551
4512
4552
See the documentation for :meth:`DataFrame.eval` for details on
4513
4553
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.
4514
4587
inplace : bool
4515
4588
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`.
4519
4589
4520
4590
Returns
4521
4591
-------
@@ -4628,10 +4698,17 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | No
4628
4698
if not isinstance (expr ,str ):
4629
4699
msg = f"expr must be a string to be evaluated,{ type (expr )} given"
4630
4700
raise ValueError (msg )
4631
- kwargs ["level" ]= kwargs .pop ("level" ,0 )+ 1
4632
- kwargs ["target" ]= None
4633
4701
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
+ )
4635
4712
4636
4713
try :
4637
4714
result = self .loc [res ]