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
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)"
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