Skip to content

Commit 98e0b0c

Browse files
committed
Address review
1 parent 47f0008 commit 98e0b0c

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

Diff for: doc/source/whatsnew/v0.23.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ Datetimelike API Changes
586586
- Operations between a :class:`Series` with dtype ``dtype='datetime64[ns]'`` and a :class:`PeriodIndex` will correctly raises ``TypeError`` (:issue:`18850`)
587587
- Subtraction of :class:`Series` with timezone-aware ``dtype='datetime64[ns]'`` with mis-matched timezones will raise ``TypeError`` instead of ``ValueError`` (:issue:`18817`)
588588
- :func:`pandas.merge` provides a more informative error message when trying to merge on timezone-aware and timezone-naive columns (:issue:`15800`)
589-
- :class:`Timestamp` constructor now accepts a `nanosecond` keyword or positional argument (:issue:`18898`)
589+
- :class:`Timestamp` constructor now accepts a ``nanosecond`` keyword or positional argument (:issue:`18898`)
590590

591591
.. _whatsnew_0230.api.other:
592592

Diff for: pandas/_libs/tslibs/timestamps.pyx

+13-2
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,12 @@ class Timestamp(_Timestamp):
423423
example, 's' means seconds and 'ms' means milliseconds.
424424
year, month, day : int
425425
.. versionadded:: 0.19.0
426-
hour, minute, second, microsecond, nanosecond : int, optional, default 0
426+
hour, minute, second, microsecond : int, optional, default 0
427427
.. versionadded:: 0.19.0
428428
tzinfo : datetime.tzinfo, optional, default None
429429
.. versionadded:: 0.19.0
430+
nanosecond : int, optional, default 0
431+
.. versionadded:: 0.23.0
430432
431433
Notes
432434
-----
@@ -588,7 +590,16 @@ class Timestamp(_Timestamp):
588590
elif tz is not None:
589591
raise ValueError('Can provide at most one of tz, tzinfo')
590592

591-
if ts_input is _no_input:
593+
if is_string_object(ts_input):
594+
# User passed a date string to parse.
595+
# Check that the user didn't also pass a date attribute kwarg.
596+
date_attrs = [year, month, day, hour, minute, second, microsecond,
597+
nanosecond]
598+
if any(arg is not None for arg in date_attrs):
599+
raise ValueError('Cannot pass a date attribute keyword '
600+
'argument when passing a date string')
601+
602+
elif ts_input is _no_input:
592603
# User passed keyword arguments.
593604
if tz is None:
594605
# Handle the case where the user passes `tz` and not `tzinfo`

Diff for: pandas/tests/scalar/timestamp/test_timestamp.py

+7
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,13 @@ def test_constructor_nanosecond(self, result):
397397
expected = expected + Timedelta(nanoseconds=1)
398398
assert result == expected
399399

400+
@pytest.mark.parametrize('arg', ['year', 'month', 'day', 'hour', 'minute',
401+
'second', 'microsecond', 'nanosecond'])
402+
def test_invalid_date_kwarg_with_string_input(self, arg):
403+
kwarg = {arg: 1}
404+
with pytest.raises(ValueError):
405+
ts = Timestamp('2010-10-10 12:59:59.999999999', **kwarg)
406+
400407
def test_out_of_bounds_value(self):
401408
one_us = np.timedelta64(1).astype('timedelta64[us]')
402409

0 commit comments

Comments
 (0)