Skip to content

BUG: Nones in pd.concat MultiIndex keys are not accepted in some cases #57993

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

Open
2 of 3 tasks
batterseapower opened this issue Mar 25, 2024 · 1 comment
Open
2 of 3 tasks
Assignees
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@batterseapower
Copy link
Contributor

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
pd.concat({
    ('a', None): pd.DataFrame(columns=[0], index=['a'])
}, axis=1, names=['a', 'b'])

Issue Description

The program above fails with a "Values not found in passed level" error:

  File "C:\Users\mboling\Anaconda3\envs\py3\lib\site-packages\pandas\core\reshape\concat.py", line 393, in concat
    return op.get_result()
  File "C:\Users\mboling\Anaconda3\envs\py3\lib\site-packages\pandas\core\reshape\concat.py", line 667, in get_result
    for ax, new_labels in enumerate(self.new_axes):
  File "properties.pyx", line 36, in pandas._libs.properties.CachedProperty.__get__
  File "C:\Users\mboling\Anaconda3\envs\py3\lib\site-packages\pandas\core\reshape\concat.py", line 698, in new_axes
    return [
  File "C:\Users\mboling\Anaconda3\envs\py3\lib\site-packages\pandas\core\reshape\concat.py", line 699, in <listcomp>
    self._get_concat_axis if i == self.bm_axis else self._get_comb_axis(i)
  File "properties.pyx", line 36, in pandas._libs.properties.CachedProperty.__get__
  File "C:\Users\mboling\Anaconda3\envs\py3\lib\site-packages\pandas\core\reshape\concat.py", line 758, in _get_concat_axis
    concat_axis = _make_concat_multiindex(
  File "C:\Users\mboling\Anaconda3\envs\py3\lib\site-packages\pandas\core\reshape\concat.py", line 871, in _make_concat_multiindex
    raise ValueError(f"Values not found in passed level: {hlevel[mask]!s}")
ValueError: Values not found in passed level: Index([nan], dtype='float64')

Expected Behavior

If you use keys which contain a None/np.nan, pd.concat should return a new frame with the appropriate index level containing either None or np.nan.

This already works if you use pd.concat to concat a series instead:

>>> pd.concat({
    ('a', None): pd.Series(index=[0])
... }, axis=1, names=['a', 'b'])...
a   a
b NaN
0 NaN

Or if you concat DataFrames, but just have a single level that contains None:

>>> pd.concat({
    None: pd.DataFrame(columns=[0], index=['a'])
... }, axis=1, names=['a'])...
a None
     0
a  NaN

There is obviously some existing inconsistency here about whether None or np.nan is used. I think it would make sense to avoid introducing np.nan, but opinions might differ on this. The important thing is that Pandas shouldn't fail in this case.

Installed Versions

INSTALLED VERSIONS

commit : fd3f571
python : 3.11.6.final.0
python-bits : 64
OS : Linux
OS-release : 4.18.0-348.20.1.el8_5.x86_64
Version : #1 SMP Thu Mar 10 20:59:28 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 2.2.0
numpy : 1.26.3
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 69.0.3
pip : 23.3.2
Cython : None
pytest : 7.4.4
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.1.9
lxml.etree : 5.1.0
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.20.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : 1.3.7
dataframe-api-compat : None
fastparquet : None
fsspec : 2023.12.2
gcsfs : None
matplotlib : 3.8.2
numba : 0.58.1
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 14.0.2
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.12.0
sqlalchemy : None
tables : None
tabulate : None
xarray : 2024.1.0
xlrd : 2.0.1
zstandard : None
tzdata : 2023.4
qtpy : 2.4.1
pyqt5 : None

@batterseapower batterseapower added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 25, 2024
@jsjeon-um
Copy link

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants