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

This issue has been tracked since 2022-09-19.

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

More Details About Repo
Owner Name pandas-dev
Repo Name pandas
Full Name pandas-dev/pandas
Language Python
Created Date 2010-08-24
Updated Date 2022-10-04
Star Count 35430
Watcher Count 1120
Fork Count 15089
Issue Count 3589

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date