question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

set_index(DatetimeIndex) unexpectedly shifts tz-aware datetime

See original GitHub issue

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:

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:closed
  • Created 8 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jrebackcommented, Feb 17, 2016

no, persistence is good! you got me to actually step thru and see what was happening. always better to test.

0reactions
wavexxcommented, Feb 17, 2016

On Wed, Feb 17 2016, Jeff Reback notifications@github.com wrote:

looks like a bug after all!

fixed by #12365

Sorry for being pedantic!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found