You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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")
exprstr - The query string to evaluate.
....
... You can refer to column names that are not valid Python variable names by surrounding them in backticks.
...
Hence the above will work by using result = df.query(f"`{column}`>6")
For inf and Inf
Specifying the df name before the column name should do the trick result = df.query(f"@df.{column}>6")
Below is example working snippet -
import pandas as pd
import numpy as np
literal_columns = ["True", "False"]
for column in literal_columns:
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")
print(result)
inf_columns = ["inf", "Inf"]
for column in inf_columns:
df = pd.DataFrame(np.random.randint(0, 100, size=(10, 2)),
columns=[column, "col1"])
expected = df[df[column] > 6]
result = df.query(f"@df.{column}>6")
print(result)
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"]
It shows this error
Feature Description
Column name from ["True", "False", "inf", "Inf"] can support dataframe eval. For example,
Alternative Solutions
No alternative solution.
Additional Context
It is related to #44603
The text was updated successfully, but these errors were encountered: