Skip to content

BUG: to_datetime with both origin and unit #51320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,7 @@ Datetimelike
- Bug in :func:`to_datetime` was not returning input with ``errors='ignore'`` when input was out-of-bounds (:issue:`50587`)
- Bug in :func:`DataFrame.from_records` when given a :class:`DataFrame` input with timezone-aware datetime64 columns incorrectly dropping the timezone-awareness (:issue:`51162`)
- Bug in :func:`to_datetime` was raising ``decimal.InvalidOperation`` when parsing date strings with ``errors='coerce'`` (:issue:`51084`)
- Bug in :func:`to_datetime` with both ``unit`` and ``origin`` specified returning incorrect results (:issue:`42624`)
-

Timedelta
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ def _adjust_to_origin(arg, origin, unit):

# we are going to offset back to unix / epoch time
try:
offset = Timestamp(origin)
offset = Timestamp(origin, unit=unit)
except OutOfBoundsDatetime as err:
raise OutOfBoundsDatetime(f"origin {origin} is Out of Bounds") from err
except ValueError as err:
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -3196,6 +3196,16 @@ def julian_dates():


class TestOrigin:
def test_origin_and_unit(self):
# GH#42624
ts = to_datetime(1, unit="s", origin=1)
expected = Timestamp("1970-01-01 00:00:02")
assert ts == expected

ts = to_datetime(1, unit="s", origin=1_000_000_000)
expected = Timestamp("2001-09-09 01:46:41")
assert ts == expected

def test_julian(self, julian_dates):
# gh-11276, gh-11745
# for origin as julian
Expand Down