Skip to content

BUG: Resample with idxmax/idxmin raises ValueError if missing a period #47350

Open
@hoxbro

Description

@hoxbro

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

dates = pd.DatetimeIndex(['2022-01-01', '2022-01-02', '2022-01-04'])  # missing a day
df = pd.DataFrame([0, 1, 2], index=dates)
df.resample("D")[0].idxmax()  # raises value error

Issue Description

When using resample with idxmax/idxmin it will raise a ValueError if a period is missing.

Python traceback
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/shh/miniconda3/envs/pandas_test/lib/python3.8/site-packages/pandas/core/groupby/groupby.py", line 950, in wrapper
    return self._python_apply_general(curried, self._obj_with_exclusions)
  File "/home/shh/miniconda3/envs/pandas_test/lib/python3.8/site-packages/pandas/core/groupby/groupby.py", line 1455, in _python_apply_general
    values, mutated = self.grouper.apply(f, data, self.axis)
  File "/home/shh/miniconda3/envs/pandas_test/lib/python3.8/site-packages/pandas/core/groupby/ops.py", line 761, in apply
    res = f(group)
  File "/home/shh/miniconda3/envs/pandas_test/lib/python3.8/site-packages/pandas/core/groupby/groupby.py", line 939, in curried
    return f(x, *args, **kwargs)
  File "/home/shh/miniconda3/envs/pandas_test/lib/python3.8/site-packages/pandas/core/series.py", line 2404, in idxmax
    i = self.argmax(axis, skipna, *args, **kwargs)
  File "/home/shh/miniconda3/envs/pandas_test/lib/python3.8/site-packages/pandas/core/base.py", line 657, in argmax
    return nanops.nanargmax(  # type: ignore[return-value]
  File "/home/shh/miniconda3/envs/pandas_test/lib/python3.8/site-packages/pandas/core/nanops.py", line 93, in _f
    return f(*args, **kwargs)
  File "/home/shh/miniconda3/envs/pandas_test/lib/python3.8/site-packages/pandas/core/nanops.py", line 1096, in nanargmax
    result = values.argmax(axis)  # type: ignore[var-annotated]
ValueError: attempt to get argmax of an empty sequence

A small thing, I noticed while filling out this bug report was I could not get pd.show_versions() to work before I downgraded setuptools<60.

Expected Behavior

I would expect it to return NaT instead of a ValueError like:

df.resample("D")[0].apply(lambda x: x.idxmax() if len(x) else None)

image

Installed Versions

Show versions
INSTALLED VERSIONS
------------------
commit           : 4bfe3d07b4858144c219b9346329027024102ab6
python           : 3.8.13.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.17.5-76051705-generic
Version          : #202204271406~1653440576~22.04~6277a18 SMP PREEMPT Wed May 25 01
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.4.2
numpy            : 1.22.4
pytz             : 2022.1
dateutil         : 2.8.2
pip              : 22.1.2
setuptools       : 59.8.0
Cython           : None
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 3.1.2
IPython          : 8.4.0
pandas_datareader: None
bs4              : 4.11.1
bottleneck       : None
brotli           : 
fastparquet      : None
fsspec           : None
gcsfs            : None
markupsafe       : 2.1.1
matplotlib       : None
numba            : None
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pyreadstat       : None
pyxlsb           : None
s3fs             : None
scipy            : None
snappy           : None
sqlalchemy       : None
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
xlwt             : None
zstandard        : None

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions