Skip to content

ENH: DataFrame eval will not work with '"True", "False", "inf", "Inf" #47859

Open
@weikhor

Description

@weikhor

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

With version 1.5.0.dev0+1213.g4fe8b84c77, it not able to support dataframe eval when column name used is from ["True", "False", "inf", "Inf"]

import pandas as pd
import numpy as np

column = "True"
df = pd.DataFrame(np.random.randint(0, 100, size=(10, 2)),
                  columns=[column, "col1"])
expected = df[df[column] > 6]
result = df.query(f"{column}>6")

It shows this error

Traceback (most recent call last):
  File "/home/open_source/pandas_1/development/development.py", line 19, in <module>
    result = df.query(f"{column}>6")
  File "/home/open_source/pandas_1/pandas/pandas/util/_decorators.py", line 317, in wrapper
    return func(*args, **kwargs)
  File "/home/open_source/pandas_1/pandas/pandas/core/frame.py", line 4388, in query
    result = self.loc[res]
  File "/home/open_source/pandas_1/pandas/pandas/core/indexing.py", line 1071, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
  File "/home/open_source/pandas_1/pandas/pandas/core/indexing.py", line 1306, in _getitem_axis
    self._validate_key(key, axis)
  File "/home/open_source/pandas_1/pandas/pandas/core/indexing.py", line 1116, in _validate_key
    raise KeyError(
KeyError: 'False: boolean label can not be used without a boolean index'

Feature Description

Column name from ["True", "False", "inf", "Inf"] can support dataframe eval. For example,

import pandas as pd
import numpy as np

column = "True"
df = pd.DataFrame(np.random.randint(0, 100, size=(10, 2)),
                  columns=[column, "col1"])
expected = df[df[column] > 6]
result = df.query(f"{column}>6")

Alternative Solutions

No alternative solution.

Additional Context

It is related to #44603

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions