Skip to content

BUG: tz_localize with Empty DatetimeIndex in MultiIndex #51676

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
2 of 3 tasks
jules-ch opened this issue Feb 27, 2023 · 2 comments
Closed
2 of 3 tasks

BUG: tz_localize with Empty DatetimeIndex in MultiIndex #51676

jules-ch opened this issue Feb 27, 2023 · 2 comments
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@jules-ch
Copy link

jules-ch commented Feb 27, 2023

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

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 : 2e218d1
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

@jules-ch jules-ch added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 27, 2023
@jules-ch jules-ch changed the title BUG: BUG: tz_localize with Empty DatetimeIndex in MultiIndex Feb 27, 2023
@jules-ch
Copy link
Author

I think it has been fixed with this line on main.

if len(arr) > 0 and is_list_like(arr[0]):

I'll try to test this with the main branch, just need to build pandas from source.

@jules-ch
Copy link
Author

It is a duplicate of #48636, closing

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

1 participant