Skip to content

BUG: Restrict Timestamp(nanosecond=...) to [0, 999] #48691

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 3 commits into from
Sep 29, 2022
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@ See :ref:`install.dependencies` and :ref:`install.optional_dependencies` for mor

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


.. ---------------------------------------------------------------------------
.. _whatsnew_160.deprecations:

Expand Down
8 changes: 7 additions & 1 deletion pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,13 @@ class Timestamp(_Timestamp):
)
# Once this deprecation is enforced, we can do
# return Timestamp(ts_input).tz_localize(tzobj)
ts = convert_to_tsobject(ts_input, tzobj, unit, 0, 0, nanosecond or 0)

if nanosecond is None:
nanosecond = 0
elif not (999 >= nanosecond >= 0):
raise ValueError("nanosecond must be in 0..999")

ts = convert_to_tsobject(ts_input, tzobj, unit, 0, 0, nanosecond)

if ts.value == NPY_NAT:
return NaT
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/scalar/timestamp/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,3 +677,10 @@ def test_constructor_missing_keyword(kwargs):

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


@pytest.mark.parametrize("nano", [-1, 1000])
def test_timestamp_nano_range(nano):
# GH 48255
with pytest.raises(ValueError, match="nanosecond must be in 0..999"):
Timestamp(year=2022, month=1, day=1, nanosecond=nano)