Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] 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
df = pd.DataFrame(
data={
'time': pd.Series(dtype='datetime64[ns, UTC]'),
'depth': pd.Series(dtype='str'),
'device_id': pd.Series(dtype='str'),
'value': pd.Series(dtype='float64'),
},
index=pd.Index([], dtype="int64"),
)
df = df.set_index(['time', "device_id", "depth"])
df = df.tz_localize(level="time", tz=None)
df
Issue Description
When converting an Empty dataframe with a timezone aware datetimeIndex with MultiIndex with tz_localize
, pandas throw the follwing error.
Although it works on single index or non empty multiIndex.
Expected Behavior
Index is converted with datime naive datetime64
on index.
It works on both single index & non empty multiIndex.
import pandas as pd
df = pd.DataFrame(
data={
'time': pd.Series(np.array(["2023-02-27T15:52:03"]),dtype='datetime64[ns, UTC]'),
'depth': pd.Series(data=["20"],dtype='str'),
'device_id': pd.Series(data=["30"],dtype='str'),
'value': pd.Series(data=[30.0], dtype='float64'),
},
index=pd.Index([0], dtype="int64"),
)
df = df.set_index(['time', "device_id", "depth"])
df = df.tz_localize(level="time", tz=None)
df
import pandas as pd
df = pd.DataFrame(
data={
'time': pd.Series(dtype='datetime64[ns, UTC]'),
'depth': pd.Series(dtype='str'),
'device_id': pd.Series(dtype='str'),
'value': pd.Series(dtype='float64'),
},
index=pd.Index([], dtype="int64"),
)
df = df.set_index(['time'])
df = df.tz_localize(level="time", tz=None)
df
Installed Versions
INSTALLED VERSIONS
commit : 2e218d10984e9919f0296931d92ea851c6a6faf5 python : 3.8.12.final.0 python-bits : 64 OS : Linux OS-release : 5.19.0-32-generic Version : #33~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Jan 30 17:03:34 UTC 2 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8
pandas : 1.5.3 numpy : 1.24.2 pytz : 2021.3 dateutil : 2.8.2 setuptools : 67.0.0 pip : 22.3.1 Cython : None pytest : 7.0.1 hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : 2.9.3 jinja2 : 3.1.2 IPython : 8.10.0 pandas_datareader: None bs4 : 4.11.2 bottleneck : None brotli : None fastparquet : None fsspec : None gcsfs : None matplotlib : 3.7.0 numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : 3.0.0 pyreadstat : None pyxlsb : None s3fs : None scipy : 1.6.1 snappy : None sqlalchemy : None tables : None tabulate : None xarray : 2023.1.0 xlrd : None xlwt : None zstandard : None tzdata : None
Comment From: jules-ch
I think it has been fixed with this line on main.
https://github.com/pandas-dev/pandas/blob/29140d44f9e96197fc5c74f460e6b9d87f731ea9/pandas/core/indexes/multi.py#L3905
I'll try to test this with the main branch, just need to build pandas from source.
Comment From: jules-ch
It is a duplicate of #48636, closing