Skip to content

BUG: MultiIndex.set_levels fails on empty levels (breaks tz_localize) #48636

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

Closed
3 tasks done
pikulmar opened this issue Sep 19, 2022 · 0 comments · Fixed by #48643
Closed
3 tasks done

BUG: MultiIndex.set_levels fails on empty levels (breaks tz_localize) #48636

pikulmar opened this issue Sep 19, 2022 · 0 comments · Fixed by #48643

Comments

@pikulmar
Copy link

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

# FAILS
pd.MultiIndex.from_product(
    [
        pd.DatetimeIndex([]),
    ],
    names=["A"],
).set_levels(pd.DatetimeIndex([]), level="A")

# WORKS
pd.MultiIndex.from_product(
    [
        pd.DatetimeIndex([]),
    ],
    names=["A"],
).set_levels(pd.DatetimeIndex([pd.Timestamp("20220101")]), level="A")

Issue Description

The code block with the "FAILS" comment above should complete without error, instead it yields

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File ".../lib/python3.9/site-packages/pandas/util/_decorators.py", line 317, in wrapper
    return func(*args, **kwargs)
  File ".../lib/python3.9/site-packages/pandas/core/indexes/multi.py", line 936, in set_levels
    level, levels = _require_listlike(level, levels, "Levels")
  File ".../lib/python3.9/site-packages/pandas/core/indexes/multi.py", line 4006, in _require_listlike
    if is_list_like(arr[0]):
  File ".../lib/python3.9/site-packages/pandas/core/indexes/base.py", line 5365, in __getitem__
    return getitem(key)
  File ".../lib/python3.9/site-packages/pandas/core/arrays/datetimelike.py", line 360, in __getitem__
    "Union[DatetimeLikeArrayT, DTScalarOrNaT]", super().__getitem__(key)
  File ".../lib/python3.9/site-packages/pandas/core/arrays/_mixins.py", line 289, in __getitem__
    result = self._ndarray[key]
IndexError: index 0 is out of bounds for axis 0 with size 0

The code block with the "WORKS" comment does not fail, which might facilitate debugging/fixing the issue.

Expected Behavior

Code should complete without error.

Installed Versions

INSTALLED VERSIONS

commit : bf856a8
python : 3.9.13.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.102.1-microsoft-standard-WSL2
Version : #1 SMP Wed Mar 2 00:30:59 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.6.0.dev0+145.gbf856a85a
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
setuptools : 63.4.1
pip : 22.1.2
Cython : None
pytest : 7.1.2
hypothesis : None
sphinx : 5.0.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.4.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : 1.3.5
brotli :
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.2
numba : 0.55.2
numexpr : 2.8.3
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 6.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.7.3
snappy : None
sqlalchemy : 1.4.41
tables : None
tabulate : 0.8.10
xarray : 0.20.1
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@pikulmar pikulmar added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 19, 2022
@phofl phofl added MultiIndex and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants