Skip to content

Commit 52f8b9a

Browse files
Backport PR pandas-dev#36557: [BUG]: Fix bug with pre epoch normalization (pandas-dev#36652)
Co-authored-by: patrick <[email protected]>
1 parent c6123ad commit 52f8b9a

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

doc/source/whatsnew/v1.1.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Fixed regressions
3838
- Fixed regression in :meth:`DataFrame.apply` with ``raw=True`` and user-function returning string (:issue:`35940`)
3939
- Fixed regression when setting empty :class:`DataFrame` column to a :class:`Series` in preserving name of index in frame (:issue:`36527`)
4040
- Fixed regression in :class:`Period` incorrect value for ordinal over the maximum timestamp (:issue:`36430`)
41+
- Fixed regression in :meth:`Series.dt.normalize` when normalizing pre-epoch dates the result was shifted one day (:issue:`36294`)
4142

4243
.. ---------------------------------------------------------------------------
4344

pandas/_libs/tslibs/conversion.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ cpdef inline datetime localize_pydatetime(datetime dt, object tz):
830830
# ----------------------------------------------------------------------
831831
# Normalization
832832

833-
@cython.cdivision
833+
@cython.cdivision(False)
834834
cdef inline int64_t normalize_i8_stamp(int64_t local_val) nogil:
835835
"""
836836
Round the localized nanosecond timestamp down to the previous midnight.

pandas/tests/scalar/timestamp/test_unary_ops.py

+6
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,12 @@ def test_normalize(self, tz_naive_fixture, arg):
397397
expected = Timestamp("2013-11-30", tz=tz)
398398
assert result == expected
399399

400+
def test_normalize_pre_epoch_dates(self):
401+
# GH: 36294
402+
result = Timestamp("1969-01-01 09:00:00").normalize()
403+
expected = Timestamp("1969-01-01 00:00:00")
404+
assert result == expected
405+
400406
# --------------------------------------------------------------
401407

402408
@td.skip_if_windows

pandas/tests/series/test_datetime_values.py

+8
Original file line numberDiff line numberDiff line change
@@ -702,3 +702,11 @@ def test_week_and_weekofyear_are_deprecated():
702702
series.dt.week
703703
with tm.assert_produces_warning(FutureWarning):
704704
series.dt.weekofyear
705+
706+
707+
def test_normalize_pre_epoch_dates():
708+
# GH: 36294
709+
s = pd.to_datetime(pd.Series(["1969-01-01 09:00:00", "2016-01-01 09:00:00"]))
710+
result = s.dt.normalize()
711+
expected = pd.to_datetime(pd.Series(["1969-01-01", "2016-01-01"]))
712+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)