set_index(DatetimeIndex) unexpectedly shifts tz-aware datetime
See original GitHub issueThis 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:
11
10
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)
11
11
Issue Analytics
- State:
- Created 8 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
pandas.DatetimeIndex.tz_localize
This method takes a time zone (tz) naive Datetime Array/Index object and makes this time zone aware. It does not move the time...
Read more >Shifting DatetimeIndex by one year gives unexpected result
I have a dataframe with a DatetimeIndex and would like to shift the dates by one year. I thought I could do so...
Read more >Pandas Time Series Examples: DatetimeIndex, PeriodIndex ...
Use existing date column as index · Add rows for empty periods · Create lag columns using shift.
Read more >Pandas for time series data — tricks and tips - Adrian G
ref_df.index = ref_date_range# check for missing datetimeindex values based on ... Similar to selecting to a % of dataframe rows, we can repeat...
Read more >Set DatetimeIndex for pandas DataFrame in Python
How to set a datetime variable as index of a pandas DataFrame in Python - 3 Python programming examples - Python programming tutorial....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
no, persistence is good! you got me to actually step thru and see what was happening. always better to test.
On Wed, Feb 17 2016, Jeff Reback notifications@github.com wrote:
Sorry for being pedantic!