Skip to content

Commit aa2d98d

Browse files
authored
BUG: to_datetime with both origin and unit (#51320)
1 parent ee84ef2 commit aa2d98d

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,7 @@ Datetimelike
11851185
- Bug in :func:`to_datetime` was not returning input with ``errors='ignore'`` when input was out-of-bounds (:issue:`50587`)
11861186
- Bug in :func:`DataFrame.from_records` when given a :class:`DataFrame` input with timezone-aware datetime64 columns incorrectly dropping the timezone-awareness (:issue:`51162`)
11871187
- Bug in :func:`to_datetime` was raising ``decimal.InvalidOperation`` when parsing date strings with ``errors='coerce'`` (:issue:`51084`)
1188+
- Bug in :func:`to_datetime` with both ``unit`` and ``origin`` specified returning incorrect results (:issue:`42624`)
11881189
-
11891190

11901191
Timedelta

pandas/core/tools/datetimes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ def _adjust_to_origin(arg, origin, unit):
606606

607607
# we are going to offset back to unix / epoch time
608608
try:
609-
offset = Timestamp(origin)
609+
offset = Timestamp(origin, unit=unit)
610610
except OutOfBoundsDatetime as err:
611611
raise OutOfBoundsDatetime(f"origin {origin} is Out of Bounds") from err
612612
except ValueError as err:

pandas/tests/tools/test_to_datetime.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3196,6 +3196,16 @@ def julian_dates():
31963196

31973197

31983198
class TestOrigin:
3199+
def test_origin_and_unit(self):
3200+
# GH#42624
3201+
ts = to_datetime(1, unit="s", origin=1)
3202+
expected = Timestamp("1970-01-01 00:00:02")
3203+
assert ts == expected
3204+
3205+
ts = to_datetime(1, unit="s", origin=1_000_000_000)
3206+
expected = Timestamp("2001-09-09 01:46:41")
3207+
assert ts == expected
3208+
31993209
def test_julian(self, julian_dates):
32003210
# gh-11276, gh-11745
32013211
# for origin as julian

0 commit comments

Comments
 (0)