Skip to content

BUG: can't round-trip non-nano Timestamp #51087

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 26 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
15 changes: 12 additions & 3 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,18 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
if ts == NPY_NAT:
obj.value = NPY_NAT
else:
ts = cast_from_unit(ts, unit)
obj.value = ts
pandas_datetime_to_datetimestruct(ts, NPY_FR_ns, &obj.dts)
in_reso = abbrev_to_npy_unit(unit or "ns")
out_reso = get_supported_reso(in_reso)
try:
value = convert_reso(ts, in_reso, out_reso, False)
except OverflowError:
# GH#26651 re-raise as OutOfBoundsDatetime
raise OutOfBoundsDatetime(
f"cannot convert input {ts} with the unit '{unit}'"
)
obj.value = value
obj.creso = out_reso
pandas_datetime_to_datetimestruct(value, out_reso, &obj.dts)
elif is_float_object(ts):
if ts != ts or ts == NPY_NAT:
obj.value = NPY_NAT
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/scalar/timestamp/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_constructor_from_date_second_reso(self):
@pytest.mark.parametrize("typ", [int, float])
def test_construct_from_int_float_with_unit_out_of_bound_raises(self, typ):
# GH#50870 make sure we get a OutOfBoundsDatetime instead of OverflowError
val = typ(150000000)
val = typ(150000000000000)

msg = f"cannot convert input {val} with the unit 'D'"
with pytest.raises(OutOfBoundsDatetime, match=msg):
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,12 @@ def test_resolution(self, ts):
assert result == expected
assert result._creso == expected._creso

def test_out_of_ns_bounds(self):
# https://github.com/pandas-dev/pandas/issues/51060
result = Timestamp(-52700112000, unit="s")
assert result == Timestamp("0300-01-01")
assert result.to_numpy() == np.datetime64("0300-01-01T00:00:00", "s")


def test_timestamp_class_min_max_resolution():
# when accessed on the class (as opposed to an instance), we default
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1765,7 +1765,7 @@ def test_unit_array_mixed_nans(self, cache):
result = to_datetime(values, unit="D", errors="ignore", cache=cache)
expected = Index(
[
11111111,
Timestamp(11111111, unit="D"),
Timestamp("1970-01-02"),
Timestamp("1970-01-02"),
NaT,
Expand All @@ -1792,7 +1792,9 @@ def test_unit_array_mixed_nans_large_int(self, cache):
values = [1420043460000, iNaT, NaT, np.nan, "NaT"]

result = to_datetime(values, errors="ignore", unit="s", cache=cache)
expected = Index([1420043460000, NaT, NaT, NaT, NaT], dtype=object)
expected = Index(
[Timestamp(1420043460000, unit="s"), NaT, NaT, NaT, NaT], dtype=object
)
tm.assert_index_equal(result, expected)

result = to_datetime(values, errors="coerce", unit="s", cache=cache)
Expand Down