-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
Conversation
pandas/_libs/tslibs/conversion.pyx
Outdated
@@ -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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
pandas/pandas/_libs/tslibs/conversion.pyx
Lines 597 to 605 in d6fdbee
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) |
@@ -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() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM cc @jbrockmendel
pandas/_libs/tslibs/conversion.pyx
Outdated
@@ -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"): |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Thanks @yuanx749 |
This fix make
Timestamp('now')
andTimestamp('today')
return a Timestamp with unit 'us', the same asTimestamp.now()
andTimestamp.today()
.doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.