Skip to content
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

possible bug when using datetime.replace() on a tz-aware pd.DatetimeIndex #18785

Closed
alessioarena opened this issue Dec 15, 2017 · 1 comment · Fixed by #22201
Closed

possible bug when using datetime.replace() on a tz-aware pd.DatetimeIndex #18785

alessioarena opened this issue Dec 15, 2017 · 1 comment · Fixed by #22201
Labels
Bug good first issue Testing pandas testing functions or related to the test suite Timezones Timezone data dtype
Milestone

Comments

@alessioarena
Copy link

Code Sample, a copy-pastable example if possible

index = pd.DatetimeIndex(pd.date_range(pd.Timestamp(2000,1,1), pd.Timestamp(2005,1,1), freq='MS', tz='Australia/Melbourne'))
test = pd.DataFrame({'data':range(len(index))}, index=index)
test = test.resample('Y').mean()
print(test.index)
> DatetimeIndex(['2000-12-31 00:00:00+11:00', '2001-12-31 00:00:00+11:00',
               '2002-12-31 00:00:00+11:00', '2003-12-31 00:00:00+11:00',
               '2004-12-31 00:00:00+11:00', '2005-12-31 00:00:00+11:00'],
              dtype='datetime64[ns, Australia/Melbourne]', freq='A-DEC')
test.index = pd.DatetimeIndex([x.replace(month=6, day=1) for x in test.index])
print(test.index)
> DatetimeIndex(['2000-05-31 23:00:00+10:00', '2001-05-31 23:00:00+10:00',
               '2002-05-31 23:00:00+10:00', '2003-05-31 23:00:00+10:00',
               '2004-05-31 23:00:00+10:00', '2005-05-31 23:00:00+10:00'],
              dtype='datetime64[ns, Australia/Melbourne]', freq=None)
#but if I rerun again the same line
test.index = pd.DatetimeIndex([x.replace(month=6, day=1) for x in test.index])
print(test.index)
>DatetimeIndex(['2000-06-01 23:00:00+10:00', '2001-06-01 23:00:00+10:00',
               '2002-06-01 23:00:00+10:00', '2003-06-01 23:00:00+10:00',
               '2004-06-01 23:00:00+10:00', '2005-06-01 23:00:00+10:00'],
              dtype='datetime64[ns, Australia/Melbourne]', freq=None)

Problem description

Hi there,

I'm not sure what is happening, but the output is definitely not what I am expecting.

Let's say that I have a timeserie with a 'MS' frequency.
At some point I want this to be resampled to a mid-year frequency

To do that I'm using the .resample('Y').mean() methods, followed by a direct replacement of the datetime object months/day using the .replace(month=6, day=1) method.
(by the way, if you can think of a better way...)

The expected output would be to have all datetime objects pointing to the first day in June, but instead they are now representing the last day of May (23:00 instead of 00:00).
I believe this problem is in some way related to the DST -> ST conversion, but I'm not sure when this occurs.

just as a test, I tried to then rerun the same line on the newly created object, and that leads to the expected result (all objects representing the first day of June, 23:00)

Is this an actual bug, or am I doing something completely wrong?
Cheers,
Alessio

Expected Output

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-104-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.21.0
pytest: None
pip: 9.0.1
setuptools: 38.2.4
Cython: 0.27.3
numpy: 1.13.3
scipy: 1.0.0
pyarrow: 0.7.1
xarray: None
IPython: 6.2.1
sphinx: None
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.1.0
openpyxl: None
xlrd: 1.1.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: 1.1.13
pymysql: None
psycopg2: 2.7.3.2 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: 0.1.2
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@jreback
Copy link
Contributor

jreback commented Dec 15, 2017

so this is after #18618

In [1]: index = pd.DatetimeIndex(pd.date_range(pd.Timestamp(2000,1,1), pd.Timestamp(2005,1,1), freq='MS', tz='Australia/Melbourne'))
   ...: test = pd.DataFrame({'data':range(len(index))}, index=index)
   ...: test = test.resample('Y').mean()
   ...: 

In [2]: test
Out[2]: 
                           data
2000-12-31 00:00:00+11:00   5.5
2001-12-31 00:00:00+11:00  17.5
2002-12-31 00:00:00+11:00  29.5
2003-12-31 00:00:00+11:00  41.5
2004-12-31 00:00:00+11:00  53.5
2005-12-31 00:00:00+11:00  60.0

In [3]: test.index = pd.DatetimeIndex([x.replace(month=6, day=1) for x in test.index])
   ...: print(test.index)
   ...: 
DatetimeIndex(['2000-06-01 00:00:00+10:00', '2001-06-01 00:00:00+10:00',
               '2002-06-01 00:00:00+10:00', '2003-06-01 00:00:00+10:00',
               '2004-06-01 00:00:00+10:00', '2005-06-01 00:00:00+10:00'],
              dtype='datetime64[ns, Australia/Melbourne]', freq=None)

In [4]: test.index = pd.DatetimeIndex([x.replace(month=6, day=1) for x in test.index])
   ...: print(test.index)
   ...: 
DatetimeIndex(['2000-06-01 00:00:00+10:00', '2001-06-01 00:00:00+10:00',
               '2002-06-01 00:00:00+10:00', '2003-06-01 00:00:00+10:00',
               '2004-06-01 00:00:00+10:00', '2005-06-01 00:00:00+10:00'],
              dtype='datetime64[ns, Australia/Melbourne]', freq=None)

@jreback jreback added Bug Timezones Timezone data dtype labels Dec 15, 2017
@jreback jreback added this to the 0.22.0 milestone Dec 15, 2017
@jreback jreback modified the milestones: 0.23.0, Next Major Release Apr 14, 2018
@mroeschke mroeschke added Testing pandas testing functions or related to the test suite good first issue labels Jul 29, 2018
@jreback jreback modified the milestones: Contributions Welcome, 0.24.0 Aug 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug good first issue Testing pandas testing functions or related to the test suite Timezones Timezone data dtype
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants