-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
CLN: Expose arguments in DataFrame.query #61413
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
Open
loicdiridollou
wants to merge
9
commits into
pandas-dev:main
Choose a base branch
from
loicdiridollou:gh61405_df_query
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0d96e9b
GH61405 Expose arguments in DataFrame.query
loicdiridollou b8267cb
GH61405 Expose arguments in DataFrame.query
loicdiridollou 183a50f
GH61405 Expose arguments in DataFrame.query
loicdiridollou 7998fd2
GH61405 Expose arguments in DataFrame.query
loicdiridollou 3df3068
Merge branch 'main' of github.com:loicdiridollou/pandas into gh61405_…
loicdiridollou b99d62e
GH61405 Expose arguments in DataFrame.query
loicdiridollou 3f50ff9
Fix pre-commit
loicdiridollou b0f90c2
GH61405 Add whatsnew docs
loicdiridollou 6457559
Fix pre-commit
loicdiridollou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4477,18 +4477,58 @@ def _get_item(self, item: Hashable) -> Series: | |
|
||
@overload | ||
def query( | ||
self, expr: str, *, inplace: Literal[False] = ..., **kwargs | ||
self, | ||
expr: str, | ||
*, | ||
parser: Literal["pandas", "python"] = ..., | ||
engine: Literal["python", "numexpr"] | None = ..., | ||
local_dict: dict[str, Any] | None = ..., | ||
global_dict: dict[str, Any] | None = ..., | ||
resolvers: list[Mapping] | None = ..., | ||
level: int = ..., | ||
inplace: Literal[False] = ..., | ||
) -> DataFrame: ... | ||
|
||
@overload | ||
def query(self, expr: str, *, inplace: Literal[True], **kwargs) -> None: ... | ||
def query( | ||
self, | ||
expr: str, | ||
*, | ||
parser: Literal["pandas", "python"] = ..., | ||
engine: Literal["python", "numexpr"] | None = ..., | ||
local_dict: dict[str, Any] | None = ..., | ||
global_dict: dict[str, Any] | None = ..., | ||
resolvers: list[Mapping] | None = ..., | ||
level: int = ..., | ||
inplace: Literal[True], | ||
) -> None: ... | ||
|
||
@overload | ||
def query( | ||
self, expr: str, *, inplace: bool = ..., **kwargs | ||
self, | ||
expr: str, | ||
*, | ||
parser: Literal["pandas", "python"] = ..., | ||
engine: Literal["python", "numexpr"] | None = ..., | ||
local_dict: dict[str, Any] | None = ..., | ||
global_dict: dict[str, Any] | None = ..., | ||
resolvers: list[Mapping] | None = ..., | ||
level: int = ..., | ||
inplace: bool = ..., | ||
) -> DataFrame | None: ... | ||
|
||
def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | None: | ||
def query( | ||
self, | ||
expr: str, | ||
*, | ||
parser: Literal["pandas", "python"] = "pandas", | ||
engine: Literal["python", "numexpr"] | None = None, | ||
local_dict: dict[str, Any] | None = None, | ||
global_dict: dict[str, Any] | None = None, | ||
resolvers: list[Mapping] | None = None, | ||
level: int = 0, | ||
inplace: bool = False, | ||
) -> DataFrame | None: | ||
""" | ||
Query the columns of a DataFrame with a boolean expression. | ||
|
||
|
@@ -4507,11 +4547,41 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | No | |
|
||
See the documentation for :meth:`DataFrame.eval` for details on | ||
referring to column names and variables in the query string. | ||
parser : {'pandas', 'python'}, default 'pandas' | ||
The parser to use to construct the syntax tree from the expression. The | ||
default of ``'pandas'`` parses code slightly different than standard | ||
Python. Alternatively, you can parse an expression using the | ||
``'python'`` parser to retain strict Python semantics. See the | ||
:ref:`enhancing performance <enhancingperf.eval>` documentation for | ||
more details. | ||
engine : {'python', 'numexpr'}, default 'numexpr' | ||
|
||
The engine used to evaluate the expression. Supported engines are | ||
|
||
- None : tries to use ``numexpr``, falls back to ``python`` | ||
- ``'numexpr'`` : This default engine evaluates pandas objects using | ||
numexpr for large speed ups in complex expressions with large frames. | ||
- ``'python'`` : Performs operations as if you had ``eval``'d in top | ||
level python. This engine is generally not that useful. | ||
|
||
More backends may be available in the future. | ||
local_dict : dict or None, optional | ||
A dictionary of local variables, taken from locals() by default. | ||
global_dict : dict or None, optional | ||
A dictionary of global variables, taken from globals() by default. | ||
resolvers : list of dict-like or None, optional | ||
A list of objects implementing the ``__getitem__`` special method that | ||
you can use to inject an additional collection of namespaces to use for | ||
variable lookup. For example, this is used in the | ||
:meth:`~DataFrame.query` method to inject the | ||
``DataFrame.index`` and ``DataFrame.columns`` | ||
variables that refer to their respective :class:`~pandas.DataFrame` | ||
instance attributes. | ||
level : int, optional | ||
The number of prior stack frames to traverse and add to the current | ||
scope. Most users will **not** need to change this parameter. | ||
inplace : bool | ||
Whether to modify the DataFrame rather than creating a new one. | ||
**kwargs | ||
See the documentation for :func:`eval` for complete details | ||
on the keyword arguments accepted by :meth:`DataFrame.query`. | ||
|
||
Returns | ||
------- | ||
|
@@ -4624,8 +4694,15 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> DataFrame | No | |
if not isinstance(expr, str): | ||
msg = f"expr must be a string to be evaluated, {type(expr)} given" | ||
raise ValueError(msg) | ||
kwargs["level"] = kwargs.pop("level", 0) + 1 | ||
kwargs["target"] = None | ||
kwargs: Any = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It appears to me there is no reason to construct a dict here anymore. Assuming that's correct, can you instead pass these as normal function arugments. |
||
"level": level + 1, | ||
"target": None, | ||
"parser": parser, | ||
"engine": engine, | ||
"local_dict": local_dict, | ||
"global_dict": global_dict, | ||
"resolvers": resolvers or (), | ||
} | ||
|
||
res = self.eval(expr, **kwargs) | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is fully backwards compatible, I think we do not need to mention this in the whatsnew.