Skip to content
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

BUG: Fix Timestamp('now') and Timestamp.now unit inconsistency #56281

Merged
merged 8 commits into from
Dec 7, 2023

Conversation

yuanx749
Copy link
Contributor

@yuanx749 yuanx749 commented Dec 1, 2023

This fix make Timestamp('now') and Timestamp('today') return a Timestamp with unit 'us', the same as Timestamp.now() and Timestamp.today().

@@ -646,7 +646,7 @@ cdef _TSObject convert_str_to_tsobject(str ts, tzinfo tz,
reso = get_supported_reso(out_bestunit)
return convert_datetime_to_tsobject(dt, tz, nanos=nanos, reso=reso)

return convert_datetime_to_tsobject(dt, tz)
return convert_datetime_to_tsobject(dt, tz, nanos=0, reso=NPY_FR_us)
Copy link
Member

Choose a reason for hiding this comment

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

This general case probably cannot be hardcoded

Copy link
Contributor Author

@yuanx749 yuanx749 Dec 2, 2023

Choose a reason for hiding this comment

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

Looks like the only cases left to handle here are 'now' and 'today', all other cases have returned.

I think we could combine these two cases and then return, to make the code more clear.

elif ts == "now":
# Issue 9000, we short-circuit rather than going
# into np_datetime_strings which returns utc
dt = datetime.now(tz)
elif ts == "today":
# Issue 9000, we short-circuit rather than going
# into np_datetime_strings which returns a normalized datetime
dt = datetime.now(tz)
# equiv: datetime.today().replace(tzinfo=tz)

@yuanx749 yuanx749 requested a review from mroeschke December 4, 2023 09:55
@@ -464,6 +464,20 @@ def test_constructor_str_infer_reso(self):
ts = Timestamp("2020-01-01 00+00:00")
assert ts.unit == "s"

def test_now_today_unit(self):
# GH#55879
ts_now_from_method = Timestamp.now()
Copy link
Member

Choose a reason for hiding this comment

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

Can you use pytest.mark.parametrize?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@mroeschke mroeschke added the Non-Nano datetime64/timedelta64 with non-nanosecond resolution label Dec 4, 2023
yuanx749 and others added 3 commits December 5, 2023 10:25
@yuanx749 yuanx749 requested a review from mroeschke December 5, 2023 04:20
Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

LGTM cc @jbrockmendel

@@ -594,15 +594,12 @@ cdef _TSObject convert_str_to_tsobject(str ts, tzinfo tz,
obj.value = NPY_NAT
obj.tzinfo = tz
return obj
elif ts == "now":
elif ts in ("now", "today"):
Copy link
Member

Choose a reason for hiding this comment

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

i think ATM we don't use a tuple here for perf reasons. unless something has changed, cython constructs a tuple object

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. Changed back in the new commit.

@yuanx749 yuanx749 requested a review from jbrockmendel December 7, 2023 03:31
@mroeschke mroeschke added this to the 2.2 milestone Dec 7, 2023
@mroeschke mroeschke merged commit 9f51d4f into pandas-dev:main Dec 7, 2023
@mroeschke
Copy link
Member

Thanks @yuanx749

@yuanx749 yuanx749 deleted the timestamp_now_unit branch December 8, 2023 03:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Non-Nano datetime64/timedelta64 with non-nanosecond resolution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: variable datetime dtype since Pandas 2.1.0
3 participants