Open kdebrab opened 4 years ago
I also experience this behaviour and it is also unexpected to me. Here more than 3 years later, sadly, the bug is still present in Pandas 2.1.2. Works as expected at midnight:
In [63]: ts = pd.Timestamp("2023-11-01 00:00:00", tz="UTC")
In [64]: pd.offsets.MonthBegin(normalize=True).rollback(ts)
Out[64]: Timestamp('2023-11-01 00:00:00+0000', tz='UTC')
In [65]: pd.offsets.MonthBegin().rollback(ts)
Out[65]: Timestamp('2023-11-01 00:00:00+0000', tz='UTC')
Unexpected behaviour at other times of the same day:
In [66]: ts = pd.Timestamp("2023-11-01 14:39:00", tz="UTC")
In [67]: pd.offsets.MonthBegin(normalize=True).rollback(ts)
Out[67]: Timestamp('2023-10-01 00:00:00+0000', tz='UTC')
In [68]: pd.offsets.MonthBegin().rollback(ts)
Out[68]: Timestamp('2023-11-01 14:39:00+0000', tz='UTC')
I just got bit by this bug and did not understand why normalize results in such unusual behaviour between midnight and a second after midnight. I am using Pandas 1.2.4, does the latest 2.1.3 fix this bug? Thanks to kdebrab for the stackoverflow link, looks like that is a solution to get around the bug.
Code Sample, a copy-pastable example if possible
Problem description
It rollbacks to the beginning of the previous month instead of the current month. This happens only when normalize=True and the timestamp is within the first day of the month but strangely, it's correct for midnight:
Expected Output
In general, I would expect the following to hold for any timestamp
dt
:Output of
pd.show_versions()