set_index(DatetimeIndex) unexpectedly shifts tz-aware datetime · Issue #12358 · pandas-dev/pandas (original) (raw)

This is another issue I've found in code that used to work:

import pandas as pd
tm = pd.DatetimeIndex(pd.to_datetime(["2014-01-01 10:10:10"]), tz='UTC').tz_convert('Europe/Rome')
df = pd.DataFrame({'tm': tm})
df.set_index(df.tm, inplace=True)
print(df.tm[0].hour)
print(df.index[0].hour)

writes:

It's unclear to me why the time is shifted. If we take a pd.DatetimeIndex which is not directly contained in the df, it works as it should:

tm = pd.DatetimeIndex(pd.to_datetime(["2014-01-01 10:10:10"]), tz='UTC').tz_convert('Europe/Rome')
df = pd.DataFrame({'tm': tm})
df.set_index(tm, inplace=True)
print(df.tm[0].hour)
print(df.index[0].hour)