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 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Other API changes
Deprecations
~~~~~~~~~~~~
-
-
- Deprecated passing ``nanoseconds`` greater than 999 or less than 0 in :class:`Timestamp` (:issue:`48538`, :issue:`48255`)

.. ---------------------------------------------------------------------------
.. _whatsnew_160.performance:
Expand Down
13 changes: 12 additions & 1 deletion pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,18 @@ 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):
warnings.warn(
Copy link
Member

Choose a reason for hiding this comment

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

could we just fix these now? The -1 case at least looks very broken

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm probably could be okay for 2.0 API change. Technically 999 < nanosecond < int32_max works okay, but sure can fix it in one go.

"In a future version, nanosecond must be between 0 and 999 inclusive "
"and will raise a ValueError otherwise. For nanoseconds > 999, "
"please distribute excess nanoseconds to microseconds and "
"other components as needed.",
FutureWarning,
stacklevel=find_stack_level(inspect.currentframe()),
)
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_deprecated(nano):
# GH 48255
with tm.assert_produces_warning(FutureWarning):
Timestamp(year=2022, month=1, day=1, nanosecond=nano)