Pandas version checks

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

  • [X] 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

s = pd.Series([pd.NaT], dtype="datetime64[ns]")
s.dt.strftime("%Y-%m-%d")

Issue Description

Since Pandas 1.4.0 a warning is raised: FutureWarning: In a future version, the Index constructor will not infer numeric dtypes when passed object-dtype sequences (matching Series behavior) This warning is raised when all values in the series are N/A (even though the dtype of the series is correct).

Note: the warning is not raised when one of the values in the series is not NA

Expected Behavior

No FutureWarning should be raised.

Installed Versions

INSTALLED VERSIONS ------------------ commit : bb1f651536508cdfef8550f93ace7849b00046ee python : 3.9.10.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.19044 machine : AMD64 processor : Intel64 Family 6 Model 85 Stepping 4, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : English_United States.1252 pandas : 1.4.0 numpy : 1.22.2 pytz : 2021.3 dateutil : 2.8.2 pip : 21.3.1 setuptools : 59.2.0 Cython : None pytest : 7.0.0 hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : None IPython : None pandas_datareader: None bs4 : None bottleneck : None fastparquet : None fsspec : None gcsfs : None matplotlib : None numba : None numexpr : None odfpy : None openpyxl : 3.0.9 pandas_gbq : None pyarrow : None pyreadstat : None pyxlsb : None s3fs : None scipy : 1.7.3 sqlalchemy : 1.4.31 tables : None tabulate : None xarray : None xlrd : None xlwt : None zstandard : None

Comment From: simonjayhawkins

Thanks @JeremyVriens for the report

Since Pandas 1.4.0 a warning is raised: FutureWarning: In a future version, the Index constructor will not infer numeric dtypes when passed object-dtype sequences (matching Series behavior)

deprecation added in #42870

cc @jbrockmendel

Comment From: jbrockmendel

IIUC in a future version this will return with object dtype, which seems More Correct to me. So might want to catch this warning and then re-issue with something more specific?

Comment From: simonjayhawkins

moving to 1.4.2

Comment From: JeremyVriens

IIUC in a future version this will return with object dtype, which seems More Correct to me. So might want to catch this warning and then re-issue with something more specific?

Why will this return an object dtype in a future version? No warning is raised when there're non-NA values in the datetime64[ns] series ([pd.NaT, "2022-02-18"]). Maybe I'm misunderstanding why this is happening only when there're only NA values in the datetime64[ns] series. Could you maybe elaborate a bit more why you think this seems more correct?

Comment From: jbrockmendel

Why will this return an object dtype in a future version?

Because object-dtype is what we get in every other case with strftime. float64 doesn't make sense as a strftime result.

ser = pd.Series([pd.Timestamp.now(), pd.NaT])

>>> ser.dt.strftime("%Y-%m-%d").dtype
dtype('O')

>>> ser[1:].dt.strftime("%Y-%m-%d").dtype
dtype('float64')

Comment From: simonjayhawkins

moving to 1.4.3

Comment From: simonjayhawkins

moving to 1.4.4

Comment From: TomAugspurger

Agreed that this returning float64 dtype for all-NaN is a bug. The result dtype shouldn't depend on the values.

I think the root bug is that DatetimeIndex.strftime returns a Float64Index for all NaT

In [32]: pd.DatetimeIndex([pd.NaT]).strftime("%Y")
/home/taugspurger/miniconda3/envs/pandas=1.4.2/lib/python3.10/site-packages/pandas/core/indexes/extension.py:101: FutureWarning: In a future version, the Index constructor will not infer numeric dtypes when passed object-dtype sequences (matching Series behavior)
  return Index(result, name=self.name)
Out[32]: Float64Index([nan], dtype='float64')

If that were fixed I think the series case will automatically be fixed too.

Comment From: jorisvandenbossche

The underlying strftime from DatetimeArray actually properly ensures to return object dtype:

In [2]: pd.DatetimeIndex([pd.NaT])._data.strftime("%Y")
Out[2]: array([nan], dtype=object)

but indeed then passing this to Index(..) will try to infer such object dtype array as float at the moment (which is what is deprecated). Since we know here that we always have object dtype, passing that to the Index constructor should be sufficient I think.

Comment From: ashleychontos

hey there, thanks for this! so pandas>=1.4.0 is a dependency in my software package (pysyd) and I've had people reach out to me due to this error. is there a quick hack that I can do from my end or will I/they have to wait until the full v1.4.4 release?

Comment From: wdewey

I am receiving this warning performing a pivot of a table (it will include some date/time data) "FutureWarning: In a future version, the Index constructor will not infer numeric dtypes when passed object-dtype sequences (matching Series behavior)"

Pandas BUG: FutureWarning for pandas datetime dtype series strftime with all NA values

Will the warning become an error in the future? Is there anything that users need to do as a work-around or will this issue be resolved in an upcoming release? (I have upgraded to 1.5.2 and still receive the warning).

Many thanks