Skip to content

BUG: Unable to create a MultiIndex with nan values in nullable Float dtypes #39984

Closed
@galipremsagar

Description

@galipremsagar
  • 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.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

>>> df = pd.DataFrame({'a':pd.Series([1, 2, None], dtype='Int64'), 'b':pd.Float64Dtype().__from_arrow__(pa.array([0.2, np.nan, None]))})
>>> df
      a     b
0     1   0.2
1     2   NaN
2  <NA>  <NA>
>>> pd.MultiIndex.from_frame(df)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/indexes/multi.py", line 662, in from_frame
    return cls.from_arrays(columns, sortorder=sortorder, names=names)
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/indexes/multi.py", line 462, in from_arrays
    codes, levels = factorize_from_iterables(arrays)
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/arrays/categorical.py", line 2632, in factorize_from_iterables
    return map(list, zip(*(factorize_from_iterable(it) for it in iterables)))
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/arrays/categorical.py", line 2632, in <genexpr>
    return map(list, zip(*(factorize_from_iterable(it) for it in iterables)))
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/arrays/categorical.py", line 2604, in factorize_from_iterable
    cat = Categorical(values, ordered=False)
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/arrays/categorical.py", line 359, in __init__
    dtype = CategoricalDtype(categories, dtype.ordered)
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/dtypes/dtypes.py", line 160, in __init__
    self._finalize(categories, ordered, fastpath=False)
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/dtypes/dtypes.py", line 314, in _finalize
    categories = self.validate_categories(categories, fastpath=fastpath)
  File "/Users/pgali/PycharmProjects/del/venv1/lib/python3.7/site-packages/pandas/core/dtypes/dtypes.py", line 508, in validate_categories
    raise ValueError("Categorical categories cannot be null")
ValueError: Categorical categories cannot be null

Problem description

Should nan be allowed in the first place while creating a nullable floating array? If yes, then we have an issue while creating a mulit-index from the dataframe.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 7d32926
python : 3.7.3.final.0
python-bits : 64
OS : Darwin
OS-release : 20.3.0
Version : Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 1.2.2
numpy : 1.19.2
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.3
setuptools : 50.3.0
Cython : None
pytest : None
hypothesis : 5.29.0
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 1.0.1
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions