possible bug when using datetime.replace() on a tz-aware pd.DatetimeIndex · Issue #18785 · pandas-dev/pandas (original) (raw)
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