Skip to content
Advertisement

Change in pandas Series.tz_convert behaviour in v1.X (or thereabouts)?

I am seeing a difference in the behaviour of Series.tz_convert between pandas 0.20.1 and 1.2.4, but I don’t understand the cause and cannot find where this change is documented, if it is intentional.

Here is some test code:

import pandas as pd
import pytz

dates=[
  '2009-12-30T09:00:00.000+10:00',
  '2009-12-31T09:00:00.000+10:00',
  '2010-01-01T09:00:00.000+10:00',
  '2010-01-02T09:00:00.000+10:00',
  ]
data = [0, 1, 4, 0]
tz10 = pytz.FixedOffset(10*60)

s = pd.Series(data, index=dates, dtype='f')
print("s, string index:", s)
s.index = pd.to_datetime(s.index, utc=True)
print("s, datatime index:", s)
s.tz_convert(tz10, copy=False)
print("s, tz=UTC+10:", s)
print("pd.__version__:", pd.__version__)

Under pandas 0.20.1 it gives this output:

s, string index: 2009-12-30T09:00:00.000+10:00    0.0
2009-12-31T09:00:00.000+10:00    1.0
2010-01-01T09:00:00.000+10:00    4.0
2010-01-02T09:00:00.000+10:00    0.0
dtype: float32
s, datetime index: 2009-12-29 23:00:00+00:00    0.0
2009-12-30 23:00:00+00:00    1.0
2009-12-31 23:00:00+00:00    4.0
2010-01-01 23:00:00+00:00    0.0
dtype: float32
s, tz=UTC+10: 2009-12-30 09:00:00+10:00    0.0
2009-12-31 09:00:00+10:00    1.0
2010-01-01 09:00:00+10:00    4.0
2010-01-02 09:00:00+10:00    0.0
dtype: float32
pd.__version__: 0.20.1

But under 1.2.4 we get this:

s, string index: 2009-12-30T09:00:00.000+10:00    0.0
2009-12-31T09:00:00.000+10:00    1.0
2010-01-01T09:00:00.000+10:00    4.0
2010-01-02T09:00:00.000+10:00    0.0
dtype: float32
s, datetime index: 2009-12-29 23:00:00+00:00    0.0
2009-12-30 23:00:00+00:00    1.0
2009-12-31 23:00:00+00:00    4.0
2010-01-01 23:00:00+00:00    0.0
dtype: float32
s, tz=UTC+10: 2009-12-29 23:00:00+00:00    0.0
2009-12-30 23:00:00+00:00    1.0
2009-12-31 23:00:00+00:00    4.0
2010-01-01 23:00:00+00:00    0.0
dtype: float32
pd.__version__: 1.2.4

Looks like under 1.2.4 the Series.tz_convert routine just doesn’t do anything? But surely that can’t be right? Any help understanding this behaviour would be much appreciated.

Advertisement

Answer

Please utilize the below code:

s1=s.tz_convert(tz10, copy=False) 
print(s1)

if you look at github code, the code is

result = result.set_axis(ax, axis=axis, inplace=False)

so the code recommended in the GitHub issue DataFrame/Series.tz_convert does not modifies original data with copy=False is not part of the latest version. recommended code was:

result.set_axis(ax, axis=axis, inplace=True) # 

I think this is aligned with the memory issue which was observed with inplace=True in pandas. so avoiding inplace=True across the library.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement