Skip to content

BUG: construct Timestamp with year out of pydatetime range #52191

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 47 additions & 0 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,30 @@ class Timestamp(_Timestamp):
# When year, month or day is not given, we call the datetime
# constructor to make sure we get the same error message
# since Timestamp inherits datetime

if year is not None and not (0 < year < 10_000):
# GH#52091
obj = cls(
year=1970,
month=month,
day=day,
hour=hour,
minute=minute,
second=second,
microsecond=microsecond,
nanosecond=nanosecond,
fold=fold,
tz=tz,
tzinfo=tzinfo,
unit=unit,
)
if nanosecond is not None and nanosecond != 0:
raise OutOfBoundsDatetime(
f"Cannot construct a Timestamp with year={year} and "
"non-zero nanoseconds"
)
return obj.as_unit("us").replace(year=year)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the year goes beyond the us max year but within the ms max year?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. And same for seconds.

Now you've got me thinking about value-dependent behavior. We should probably get "us" unit whenever nanos is zero-or-None regardless of year. Same for getting "ms" if micros is (zero % 1000)-or-None, etc


datetime_kwargs = {
"hour": hour or 0,
"minute": minute or 0,
Expand All @@ -1645,6 +1669,29 @@ class Timestamp(_Timestamp):
# User passed positional arguments:
# Timestamp(year, month, day[, hour[, minute[, second[,
# microsecond[, tzinfo]]]]])
if not (0 < ts_input < 10_000):
# GH#52091
obj = cls(
1970,
year,
month,
day,
hour,
minute,
second,
microsecond,
nanosecond=nanosecond,
fold=fold,
tz=tz,
tzinfo=tzinfo,
unit=unit,
)
if nanosecond is not None and nanosecond != 0:
raise OutOfBoundsDatetime(
f"Cannot construct a Timestamp with year={ts_input} and "
"non-zero nanoseconds"
)
return obj.as_unit("us").replace(year=ts_input)
ts_input = datetime(ts_input, year, month, day or 0,
hour or 0, minute or 0, second or 0, fold=fold or 0)
unit = None
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/scalar/timestamp/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@


class TestTimestampConstructors:
def test_construct_year_out_of_pydatetime_bounds(self):
# GH#52091 pass a year outside of pydatetime bounds either as positional
# or keyword argument
ts = Timestamp(year=21000, month=1, day=2)
assert ts.year == 21000
assert ts.month == 1
assert ts.day == 2
assert ts.unit == "us"

ts2 = Timestamp(21000, 1, 2)
assert ts2 == ts
assert ts2.unit == "us"

msg = "Cannot construct a Timestamp with year=21000 and non-zero nanoseconds"
with pytest.raises(OutOfBoundsDatetime, match=msg):
Timestamp(year=21000, month=1, day=2, nanosecond=1)

with pytest.raises(OutOfBoundsDatetime, match=msg):
Timestamp(21000, 1, 2, nanosecond=1)

def test_construct_from_string_invalid_raises(self):
# dateutil (weirdly) parses "200622-12-31" as
# datetime(2022, 6, 20, 12, 0, tzinfo=tzoffset(None, -111600)
Expand Down