Skip to content

Commit 1a6df8a

Browse files
mroeschkenoatamir
authored andcommitted
BUG: Restrict Timestamp(nanosecond=...) to [0, 999] (pandas-dev#48691)
1 parent da99346 commit 1a6df8a

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v1.6.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,11 @@ See :ref:`install.dependencies` and :ref:`install.optional_dependencies` for mor
115115

116116
Other API changes
117117
^^^^^^^^^^^^^^^^^
118+
- Passing ``nanoseconds`` greater than 999 or less than 0 in :class:`Timestamp` now raises a ``ValueError`` (:issue:`48538`, :issue:`48255`)
118119
- :func:`read_csv`: specifying an incorrect number of columns with ``index_col`` of now raises ``ParserError`` instead of ``IndexError`` when using the c parser.
119120
-
120121

122+
121123
.. ---------------------------------------------------------------------------
122124
.. _whatsnew_160.deprecations:
123125

pandas/_libs/tslibs/timestamps.pyx

+7-1
Original file line numberDiff line numberDiff line change
@@ -1682,7 +1682,13 @@ class Timestamp(_Timestamp):
16821682
)
16831683
# Once this deprecation is enforced, we can do
16841684
# return Timestamp(ts_input).tz_localize(tzobj)
1685-
ts = convert_to_tsobject(ts_input, tzobj, unit, 0, 0, nanosecond or 0)
1685+
1686+
if nanosecond is None:
1687+
nanosecond = 0
1688+
elif not (999 >= nanosecond >= 0):
1689+
raise ValueError("nanosecond must be in 0..999")
1690+
1691+
ts = convert_to_tsobject(ts_input, tzobj, unit, 0, 0, nanosecond)
16861692

16871693
if ts.value == NPY_NAT:
16881694
return NaT

pandas/tests/scalar/timestamp/test_constructors.py

+7
Original file line numberDiff line numberDiff line change
@@ -677,3 +677,10 @@ def test_constructor_missing_keyword(kwargs):
677677

678678
with pytest.raises(TypeError, match=msg):
679679
Timestamp(**kwargs)
680+
681+
682+
@pytest.mark.parametrize("nano", [-1, 1000])
683+
def test_timestamp_nano_range(nano):
684+
# GH 48255
685+
with pytest.raises(ValueError, match="nanosecond must be in 0..999"):
686+
Timestamp(year=2022, month=1, day=1, nanosecond=nano)

0 commit comments

Comments
 (0)