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
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
importpandasaspddates=pd.DatetimeIndex(['2022-01-01', '2022-01-02', '2022-01-04']) # missing a daydf=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 wrapperreturnself._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 curriedreturn 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 argmaxreturn 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 _freturn 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)
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.
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
Issue Description
When using resample with
idxmax
/idxmin
it will raise aValueError
if a period is missing.Python traceback
A small thing, I noticed while filling out this bug report was I could not get
pd.show_versions()
to work before I downgradedsetuptools<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)
Installed Versions
Show versions
The text was updated successfully, but these errors were encountered: