Skip to content

REGR: ValueError: cannot convert float NaN to integer - on dataframe.reset_index() in pandas 1.1.0 #35606

Closed
@ndhansen

Description

@ndhansen
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

from pandas.core.dtypes.cast import construct_1d_arraylike_from_scalar
from pandas.core.dtypes.missing import na_value_for_dtype
import numpy as np

dtype = np.datetime64().dtype
length = 10
works_value = np.NaN
fails_value = na_value_for_dtype(dtype)

# works
construct_1d_arraylike_from_scalar(works_value, length, dtype)

# fails
construct_1d_arraylike_from_scalar(fails_value, length, dtype)

Results in:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
    construct_1d_arraylike_from_scalar(value, length, dtype)
  File "/usr/lib/python3.8/site-packages/pandas/core/dtypes/cast.py", line 1453, in construct_1d_arraylike_from_scalar
    subarr.fill(value)
ValueError: cannot convert float NaN to integer

Problem description

This is a problem was found when calling reset_index on a MultiIndex Dataframe with an index containing datetime information. The expected behaviour would be that it would return an array of NaTs, which ironically only happens if you pass it np.NaN.
I'm not familiar with the source code of numpy, so I can only speculate on how to fix it, but I would assume we would need special handling for time values, so we fill time arrays with np.NaNs instead of pandas._libs.tslibs.nattype.NaTTypes.

Expected Output

array(['NaT', 'NaT', 'NaT', 'NaT', 'NaT', 'NaT', 'NaT', 'NaT', 'NaT', 'NaT'], dtype=datetime64)

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.3.final.0
python-bits : 64
OS : Linux
OS-release : 5.7.9-1-MANJARO
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.0.5
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 49.2.0
Cython : 0.29.21
pytest : 5.4.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.2
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.16.1
pandas_datareader: 0.10.0dev0
bs4 : 4.9.1
bottleneck : 1.3.2
fastparquet : None
gcsfs : None
lxml.etree : 4.5.2
matplotlib : 3.3.0
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.4.3
pyxlsb : None
s3fs : None
scipy : 1.5.1
sqlalchemy : 1.3.17
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDatetimeDatetime data dtypeMultiIndexRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions