Skip to content

ENH: Timestamp constructor now raises more explanatory error message #31653

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
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/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Datetimelike
- :meth:`DatetimeArray.searchsorted`, :meth:`TimedeltaArray.searchsorted`, :meth:`PeriodArray.searchsorted` not recognizing non-pandas scalars and incorrectly raising ``ValueError`` instead of ``TypeError`` (:issue:`30950`)
- Bug in :class:`Timestamp` where constructing :class:`Timestamp` with dateutil timezone less than 128 nanoseconds before daylight saving time switch from winter to summer would result in nonexistent time (:issue:`31043`)
- Bug in :meth:`Period.to_timestamp`, :meth:`Period.start_time` with microsecond frequency returning a timestamp one nanosecond earlier than the correct time (:issue:`31475`)
- :class:`Timestamp` raising confusing error message when year, month or day is missing (:issue:`31200`)

Timedelta
^^^^^^^^^
Expand Down
23 changes: 19 additions & 4 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,25 @@ class Timestamp(_Timestamp):
)

elif ts_input is _no_input:
# User passed keyword arguments.
ts_input = datetime(year, month, day, hour or 0,
minute or 0, second or 0,
microsecond or 0)
# GH 31200
# 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
datetime_kwargs = {
"hour": hour or 0,
"minute": minute or 0,
"second": second or 0,
"microsecond": microsecond or 0
}
if year is not None:
datetime_kwargs["year"] = year
if month is not None:
datetime_kwargs["month"] = month
if day is not None:
datetime_kwargs["day"] = day

ts_input = datetime(**datetime_kwargs)

elif is_integer_object(freq):
# User passed positional arguments:
# Timestamp(year, month, day[, hour[, minute[, second[,
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/scalar/timestamp/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,16 @@ def test_timestamp_constructor_identity():
expected = Timestamp("2017-01-01T12")
result = Timestamp(expected)
assert result is expected


@pytest.mark.parametrize("kwargs", [{}, {"year": 2020}, {"year": 2020, "month": 1}])
def test_constructor_missing_keyword(kwargs):
# GH 31200

# The exact error message of datetime() depends on its version
msg1 = r"function missing required argument '(year|month|day)' \(pos [123]\)"
msg2 = r"Required argument '(year|month|day)' \(pos [123]\) not found"
msg = "|".join([msg1, msg2])

with pytest.raises(TypeError, match=msg):
Timestamp(**kwargs)