Skip to content

BUG: df.query raises TypeError when a column is named datetime #35695

Open
@ganevgv

Description

@ganevgv
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# Your code here
import pandas as pd

col_name = "datetime"
col_data = [0.1, 0.3, 0.5, 1]
df = pd.DataFrame({col_name: col_data})

min_bound, max_bound = 0.25, 0.75
query = f"{min_bound} < {col_name} < {max_bound}"
df.query(query)

Problem description

I know this is a bit of an unfair issue but running the code above raises a TypeError (TypeError: '<' not supported between instances of 'float' and 'type'). Full error bellow:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-fe357c1c3e3c> in <module>
      7 min_bound, max_bound = 0.25, 0.75
      8 query = f"{min_bound} < {col_name} < {max_bound}"
----> 9 df.query(query)

~/personal/hackatlon/pandas/pandas/core/frame.py in query(self, expr, inplace, **kwargs)
   3368         kwargs["level"] = kwargs.pop("level", 0) + 1
   3369         kwargs["target"] = None
-> 3370         res = self.eval(expr, **kwargs)
   3371
   3372         try:

~/personal/hackatlon/pandas/pandas/core/frame.py in eval(self, expr, inplace, **kwargs)
   3498         kwargs["resolvers"] = kwargs.get("resolvers", ()) + tuple(resolvers)
   3499
-> 3500         return _eval(expr, inplace=inplace, **kwargs)
   3501
   3502     def select_dtypes(self, include=None, exclude=None) -> "DataFrame":

~/personal/hackatlon/pandas/pandas/core/computation/eval.py in eval(expr, parser, engine, truediv, local_dict, global_dict, resolvers, level, target, inplace)
    344         eng = _engines[engine]
    345         eng_inst = eng(parsed_expr)
--> 346         ret = eng_inst.evaluate()
    347
    348         if parsed_expr.assigner is None:

~/personal/hackatlon/pandas/pandas/core/computation/engines.py in evaluate(self)
    125
    126     def evaluate(self):
--> 127         return self.expr()
    128
    129     def _evaluate(self) -> None:

~/personal/hackatlon/pandas/pandas/core/computation/expr.py in __call__(self)
    792
    793     def __call__(self):
--> 794         return self.terms(self.env)
    795
    796     def __repr__(self) -> str:

~/personal/hackatlon/pandas/pandas/core/computation/ops.py in __call__(self, env)
    394         """
    395         # recurse over the left/right nodes
--> 396         left = self.lhs(env)
    397         right = self.rhs(env)
    398

~/personal/hackatlon/pandas/pandas/core/computation/ops.py in __call__(self, env)
    397         right = self.rhs(env)
    398
--> 399         return self.func(left, right)
    400
    401     def evaluate(self, env, engine: str, parser, term_type, eval_in_python):

TypeError: '<' not supported between instances of 'float' and 'type'

I tried changing col_name to every value from the list bellow and only datetime raises an error.

["float", "float64", "int", "int64", "category", "string", "object", "datetime"]

I get the same behaviour on pandas 1.1.0 and on the latest dev version 1.2.0.dev0+80.g59ffa2512.

Expected Output

   datetime
1  0.3
2  0.5

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 59ffa25
python : 3.8.5.final.0
python-bits : 64
OS : Darwin
OS-release : 19.3.0
Version : Darwin Kernel Version 19.3.0: Thu Jan 9 20:58:23 PST 2020; root:xnu-6153.81.5~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.2.0.dev0+80.g59ffa2512
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.2
setuptools : 49.3.2
Cython : 0.29.21
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.17.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions