Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 of 3 tasks
hoxbro opened this issue Jun 14, 2022 · 4 comments
Open
2 of 3 tasks

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

hoxbro opened this issue Jun 14, 2022 · 4 comments
Assignees
Labels
Bug Resample resample method

Comments

@hoxbro
Copy link
Contributor

hoxbro commented Jun 14, 2022

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
@hoxbro hoxbro added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 14, 2022
@phofl phofl added Resample resample method and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 14, 2022
@GYHHAHA
Copy link
Contributor

GYHHAHA commented Jun 16, 2022

It seems that idxmin and idxmax on resampler are not documented.

@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone Jun 16, 2022
@xr-chen xr-chen removed their assignment Jun 16, 2022
@simonjayhawkins
Copy link
Member

It seems that idxmin and idxmax on resampler are not documented.

inherited from pandas.core.groupby.groupby

probably needs to special case for the empty Series in the resample code itself.

I don't think there have been any issues raised about an empty Series raising ValueError: attempt to get argmax of an empty sequence except perhaps the inconsistency with argmax, #33941, so let's assume that is the correct behavior.

Contributions and PRs to fix welcome.

@hamedgibago
Copy link

take

@hamedgibago
Copy link

@simonjayhawkins I'm working on the bug and just reading the code. Need some time. Thanks.

@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Resample resample method
Projects
None yet
Development

No branches or pull requests

7 participants