BUG: preserve fold in Timestamp.replace by AlexKirko · Pull Request #37644 · pandas-dev/pandas (original) (raw)
- closes BUG: pd.Timestamp loses fold when removing timezone which is inconsistent with datetime.datetime #37610
- 1 tests added / 1 passed
- passes
black pandas
- passes
git diff upstream/master -u -- "*.py" | flake8 --diff
- whatsnew entry
Problem
We currently lose fold
information (whether the Timestamp corresponds to the first or second instance of wall clock time in a DST transition) when calling Timestamp.replace
.
Solution
A simple addition to the code of Timestamp.replace
should fix this.
Test
I added the test for the use case I came up with in the issue discussion. The OP example is losing fold
when deleting timezone information with Timetsamp.replace
, and that's not really a bug, but replacing a valid dateutil timezone with itself and losing fold
definitely is.
The proposed solution fixes the original example as well. I just don't think we should be tracking it in tests, as it's not clear to me why fold
must be preserved in a Timestamp
with no timezone information (but it is the convention recommended in PEP 495, fold section to let users keep invalid fold and to just ignore it).
Some details
IIRC, we ignore fold
, when it doesn't do anything, so we should be safe preserving fold
while replacing tzinfo with None, as in the OP example. I remember this coming up when we introduced fold support, and we made sure that the fold-aware functions don't care what fold is outside of a DST transition with a dateutil timezone (this was done to satisfy the requirements of PEP 495, fold section).