-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Quoting column names containing spaces with backticks to use them in query and eval. #24955
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
Changes from all commits
ff463ca
db9c769
22686fd
bfebb9d
a65f5a5
da60955
2125068
5200b0c
63c25bf
b104766
e496671
d3877d1
bb62d73
192c093
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2967,6 +2967,15 @@ def query(self, expr, inplace=False, **kwargs): | |
The query string to evaluate. You can refer to variables | ||
in the environment by prefixing them with an '@' character like | ||
``@a + b``. | ||
|
||
.. versionadded:: 0.25.0 | ||
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. can you add an example in the Examples section as well 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. Done, but don't know what this means: 1 Warnings found: |
||
|
||
You can refer to column names that contain spaces by surrounding | ||
them in backticks. | ||
|
||
For example, if one of your columns is called ``a a`` and you want | ||
to sum it with ``b``, your query should be ```a a` + b``. | ||
|
||
inplace : bool | ||
Whether the query should modify the data in place or return | ||
a modified copy. | ||
|
@@ -3025,23 +3034,37 @@ def query(self, expr, inplace=False, **kwargs): | |
|
||
Examples | ||
-------- | ||
>>> df = pd.DataFrame({'A': range(1, 6), 'B': range(10, 0, -2)}) | ||
>>> df = pd.DataFrame({'A': range(1, 6), | ||
... 'B': range(10, 0, -2), | ||
... 'C C': range(10, 5, -1)}) | ||
>>> df | ||
A B | ||
0 1 10 | ||
1 2 8 | ||
2 3 6 | ||
3 4 4 | ||
4 5 2 | ||
A B C C | ||
0 1 10 10 | ||
1 2 8 9 | ||
2 3 6 8 | ||
3 4 4 7 | ||
4 5 2 6 | ||
>>> df.query('A > B') | ||
A B | ||
4 5 2 | ||
A B C C | ||
4 5 2 6 | ||
|
||
The previous expression is equivalent to | ||
|
||
>>> df[df.A > df.B] | ||
A B | ||
4 5 2 | ||
A B C C | ||
4 5 2 6 | ||
|
||
For columns with spaces in their name, you can use backtick quoting. | ||
|
||
>>> df.query('B == `C C`') | ||
A B C C | ||
0 1 10 10 | ||
|
||
The previous expression is equivalent to | ||
|
||
>>> df[df.B == df['C C']] | ||
A B C C | ||
0 1 10 10 | ||
""" | ||
inplace = validate_bool_kwarg(inplace, 'inplace') | ||
if not isinstance(expr, compat.string_types): | ||
|
@@ -3160,7 +3183,9 @@ def eval(self, expr, inplace=False, **kwargs): | |
kwargs['level'] = kwargs.pop('level', 0) + 1 | ||
if resolvers is None: | ||
index_resolvers = self._get_index_resolvers() | ||
hwalinga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resolvers = dict(self.iteritems()), index_resolvers | ||
column_resolvers = \ | ||
self._get_space_character_free_column_resolvers() | ||
resolvers = column_resolvers, index_resolvers | ||
if 'target' not in kwargs: | ||
kwargs['target'] = self | ||
kwargs['resolvers'] = kwargs.get('resolvers', ()) + tuple(resolvers) | ||
|
Uh oh!
There was an error while loading. Please reload this page.