8000 ENH: Quoting column names containing spaces with backticks to use them in query and eval. by hwalinga · Pull Request #24955 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 14 commits into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
8000
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Local import for computation.common; Added example in query
  • Loading branch information
hwalinga committed Mar 10, 2019
commit bb62d7392962521b34046df5760c4411f0497ca1
36 changes: 25 additions & 11 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3034,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):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import pandas.core.algorithms as algos
from pandas.core.base import PandasObject, SelectionMixin
import pandas.core.common as com
from pandas.core.computation.common import _remove_spaces_column_name
from pandas.core.index import (
Index, InvalidIndexError, MultiIndex, RangeIndex, ensure_index)
from pandas.core.indexes.datetimes import DatetimeIndex
Expand Down Expand Up @@ -431,6 +430,7 @@ def _get_space_character_free_column_resolvers(self):
to by backtick quoting.
Used in :meth:`DataFrame.eval`.
"""
from pandas.core.computation.common import _remove_spaces_column_name

return {_remove_spaces_column_name(k): v for k, v
in self.iteritems()}
Expand Down
0