Description
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
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