Skip to content

BUG: dtype of DataFrame.idxmax/idxmin incorrect if other dimension is 0 length #53265

Closed
@batterseapower

Description

@batterseapower

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

assert pd.DataFrame().idxmax(axis=1).dtype.kind == 'i' # Fails: O in pandas 2.0.1, f in 1.5.3
assert pd.DataFrame().idxmax(axis=0).dtype.kind == 'i' # Fails: O in pandas 2.0.1, f in 1.5.3

assert pd.DataFrame([[0], [1]], index=pd.DatetimeIndex(['2010-01-01', '2010-01-02'])).idxmax(axis=0).dtype.kind == 'M' # Works OK
pd.DataFrame([[], []], index=pd.DatetimeIndex(['2010-01-01', '2010-01-02'])).idxmax(axis=0).dtype.kind == 'M' # Fails: O in pandas 2.0.1, f in 1.5.3

Issue Description

When idxmax/idxmin creates the series, it should explictly specify the dtype rather than relying on the Series constructor figuring it out from the input data.

(Pandas 1.5.3 raises a FutureWarning on all the failing examples above saying "The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.")

Note this is a different issue than the related problem with Series reported in #33941.

Expected Behavior

Asserts above should pass

Installed Versions

INSTALLED VERSIONS ------------------ commit : 37ea63d python : 3.11.3.final.0 python-bits : 64 OS : Linux OS-release : 4.18.0-348.20.1.el8_5.x86_64 Version : #1 SMP Thu Mar 10 20:59:28 UTC 2022 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_GB.UTF-8 LOCALE : en_GB.UTF-8

pandas : 2.0.1
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.7.2
pip : 23.1.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.13.2
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
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
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    AlgosNon-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diffBug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions