Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 of 3 tasks
JeremyVriens opened this issue Feb 7, 2022 · 11 comments · Fixed by #48147
Closed
2 of 3 tasks

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

JeremyVriens opened this issue Feb 7, 2022 · 11 comments · Fixed by #48147
Labels
Bug Index Related to the Index class or subclasses Warnings Warnings that appear or should be added to pandas
Milestone

Comments

@JeremyVriens
Copy link

JeremyVriens commented Feb 7, 2022

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

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 : bb1f651
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

@JeremyVriens JeremyVriens added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 7, 2022
@JeremyVriens JeremyVriens changed the title BUG: BUG: FutureWarning for pandas datetime dtype series strftime with all NA values Feb 7, 2022
@simonjayhawkins
Copy link
Member

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

@simonjayhawkins simonjayhawkins added Warnings Warnings that appear or should be added to pandas and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 10, 2022
@simonjayhawkins simonjayhawkins added this to the 1.4.1 milestone Feb 10, 2022
@simonjayhawkins simonjayhawkins added the Index Related to the Index class or subclasses label Feb 10, 2022
@jbrockmendel
Copy link
Member

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?

@simonjayhawkins
Copy link
Member

moving to 1.4.2

@simonjayhawkins simonjayhawkins modified the milestones: 1.4.1, 1.4.2 Feb 11, 2022
@JeremyVriens
Copy link
Author

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?

@jbrockmendel
Copy link
Member

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

@simonjayhawkins
Copy link
Member

moving to 1.4.3

@simonjayhawkins simonjayhawkins modified the milestones: 1.4.2, 1.4.3 Apr 1, 2022
@simonjayhawkins
Copy link
Member

moving to 1.4.4

@simonjayhawkins simonjayhawkins modified the milestones: 1.4.3, 1.4.4 Jun 22, 2022
@TomAugspurger
Copy link
Contributor

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.

@jorisvandenbossche
Copy link
Member

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.

@ashleychontos
Copy link

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?

@wdewey
Copy link

wdewey commented Dec 22, 2022

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

image

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Index Related to the Index class or subclasses Warnings Warnings that appear or should be added to pandas
Projects
None yet
7 participants