Skip to content

PERF: DataFrame.query slow when it contains PeriodIndex/Array #35247

Open
@semio

Description

@semio
  • 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.

Code Sample, a copy-pastable example

In [1]: import numpy as np

In [2]: import pandas as pd

In [23]: country = list(range(200))

In [24]: year = list(range(1600, 2100))

In [25]: idx = pd.MultiIndex.from_product([country, year], names=['country', 'year'])

In [27]: values = np.random.rand(idx.shape[0], 1)

In [30]: df = pd.DataFrame(values, index=idx, columns=['values']).reset_index()

In [41]: df2 = df.copy()

In [42]: df2['year'] = df2['year'].astype(str).apply(lambda x: pd.Period(x, 'Y'))


# performance when year column is int dtype
In [47]: %timeit df.query("year > 2020")
3.48 ms ± 80.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [48]: %timeit df[df.year > 2020]
1.07 ms ± 16.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

# performance when year column is Peroid dtype
In [49]: %timeit df2.query("year > '2020'")
1.03 s ± 18.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [50]: %timeit df2[df2.year > '2020']
1.46 ms ± 21.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Problem description

In above example, the year column of df2 is a Series of Period objects. Running query() on the year column is a lot slower than running indexing (1.03s vs 1.46ms). Changing engine and parser for query() doesn't help.

Expected Output

I was expecting performance difference between querying and boolean indexing is similar to that when the year column's dtype is int (~3x)

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.1.final.0
python-bits : 64
OS : Darwin
OS-release : 18.7.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : zh_CN.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.5
numpy : 1.18.4
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 41.2.0
Cython : None
pytest : 5.4.2
hypothesis : 5.16.0
sphinx : 3.0.4
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.14.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.5.1
matplotlib : None
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.4.2
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : 0.8.7
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    PerformanceMemory or execution speed performanceexpressionspd.eval, query

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions