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

This issue has been created 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-09-29
Star Count 35373
Watcher Count 1123
Fork Count 15031
Issue Count 3581

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
*-native-* modules for qtjambi 6.3.0 are missing at maven central 5 2022-04-05 2022-07-02
Mechanism for embedding Obsidian links inside diagrams 6 2022-03-20 2022-09-10
Question: How to customize theme colors 0 2022-02-23 2022-09-15
extension points for plugin 3 2021-10-28 2022-09-19
APP退回桌面未登出,重新进入后离线消息丢失 1 2022-06-11 2022-09-19
Check stalkerware apps 1 2022-05-24 2022-07-16
Would the left sidebar collapsable? 2 2022-03-13 2022-09-27
Pin sidebar toggle on smaller screens 0 2022-03-25 2022-08-19
what's the hyphen '-' used for in yaml 2 2022-01-17 2022-08-01
Click on the bottom bar to pop up the current URL and the copied URL will flash. 1 2022-05-13 2022-09-02
[Bug] Dark mode activated but white flash persist 1 2022-05-12 2022-09-02
ImportError: cannot import name 'TypeGuard' from 'typing_extensions' 2 2021-07-23 2022-09-01
Tables with track changes badly read 4 2018-10-23 2022-09-09
bug: Creating a bot error display 0 2022-07-06 2022-09-12
Error on "Retry send" 1 2022-07-04 2022-09-12
Не загружаются файлы в /home/selenium/Downloads 4 2022-03-22 2022-09-22
Feature request: Quiet mode 0 2019-03-27 2022-08-23
ul role=listbox should define an aria-label or aria-labelledby 2 2021-01-06 2022-09-08
Broken code generated for nested messages 3 2019-07-27 2022-08-04
MNT: Rename default branch from master to main 1 2021-03-09 2022-08-29
Include time (in seconds) in tempo and signature events? 2 2021-03-24 2022-09-02
Range/Index parser 1 2021-06-19 2022-01-18
[com.google.fonts/check/unreachable_glyphs] Does not handle glyphs reachable only from COLR table 1 2022-05-09 2022-09-01
Registration between two volumes produces distorted, cut off output 6 2022-02-11 2022-09-19
how to integrate .bmp files and other types of files into the exe 3 2022-07-28 2022-09-24
Cannot read property 'add' of undefined 20 2020-01-17 2022-09-21
Audio track select button to be added to picture in picture (PIP) 5 2022-02-28 2022-09-02
GTP engine can't be synced to current state. 3 2021-02-11 2022-09-08
Add a command to do merge on streams (opposite of without) 5 2012-11-13 2022-07-26
Bug: Small canvas still shows full image in preview window (low priority) 2 2020-04-04 2022-09-28
link to file activity notification for boards leads to ipynb file 2 2022-06-01 2022-08-08
Does this work on prs? 1 2022-06-03 2022-09-18
Arrange - add parameter to keep given distance from bed edges 1 2022-06-30 2022-09-14
Add support to rename attribute values in Schemas 2 2021-09-15 2022-09-10
LAB_AK_04_Lab3_Ex4_Microsoft_Teams.md -- Teams admin UI has changed 1 2021-11-11 2022-09-10
Bitlbee keeps posting connected messages 7 2017-01-23 2022-09-26
Helm Lint Error templates/secret.yml" at <b64enc>: invalid value; expected string 1 2022-03-29 2022-09-23
Simplify examples how to use callbacks 1 2021-02-25 2022-09-21
Include missing javadoc comments 1 2022-04-07 2022-09-16
Find my Device doesn't work when VPN is on 0 2020-11-01 2022-09-22
www.flashscore.ru 0 2021-10-30 2021-12-09
`islinear` and `isaffine` on `AbstractSystem` ignores nonlinear algebraic equations 0 2021-11-25 2022-09-19
Routed Subinterface enhancements 1 2021-08-06 2022-09-19
Cannot evaluate a method inside of itself if the method is `@tailrec` 2 2021-10-06 2022-09-05
docs: rename section "Working with Skaffold" to "Guides" 1 2021-08-30 2021-12-30
I am getting encrpted cookie when asking for access token 2 2021-11-14 2022-09-06
String search in Korean does not work for single characters or partial strings 1 2021-06-26 2022-07-30
[Feature] support string type cast to bitmap 0 2022-03-23 2022-08-19
doc: nodejs.dev site says npx "allows you to run that npm command without installing it first" -- but that appears to no longer be true 12 2021-09-09 2022-09-29
[SYCL] Enqueuing USM memory free 1 2022-06-23 2022-08-23