-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
REF: de-duplicate precision_from_unit attempt 2 #51594
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ from pandas._libs.tslibs.np_datetime cimport ( | |
NPY_FR_us, | ||
check_dts_bounds, | ||
convert_reso, | ||
get_conversion_factor, | ||
get_datetime64_unit, | ||
get_datetime64_value, | ||
get_implementation_bounds, | ||
|
@@ -83,9 +84,9 @@ TD64NS_DTYPE = np.dtype("m8[ns]") | |
# Unit Conversion Helpers | ||
|
||
cdef int64_t cast_from_unit( | ||
object ts, | ||
str unit, | ||
NPY_DATETIMEUNIT out_reso=NPY_FR_ns | ||
object ts, | ||
str unit, | ||
NPY_DATETIMEUNIT out_reso=NPY_FR_ns | ||
) except? -1: | ||
""" | ||
Return a casting of the unit represented to nanoseconds | ||
|
@@ -104,12 +105,6 @@ cdef int64_t cast_from_unit( | |
int64_t m | ||
int p | ||
|
||
m, p = precision_from_unit(unit, out_reso) | ||
|
||
# just give me the unit back | ||
if ts is None: | ||
return m | ||
MarcoGorelli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if unit in ["Y", "M"]: | ||
if is_float_object(ts) and not ts.is_integer(): | ||
# GH#47267 it is clear that 2 "M" corresponds to 1970-02-01, | ||
|
@@ -126,6 +121,8 @@ cdef int64_t cast_from_unit( | |
dt64obj = np.datetime64(ts, unit) | ||
return get_datetime64_nanos(dt64obj, out_reso) | ||
|
||
m, p = precision_from_unit(unit, out_reso) | ||
|
||
# cast the unit, multiply base/frac separately | ||
# to avoid precision issues from float -> int | ||
try: | ||
|
@@ -148,8 +145,8 @@ cdef int64_t cast_from_unit( | |
|
||
|
||
cpdef inline (int64_t, int) precision_from_unit( | ||
str unit, | ||
NPY_DATETIMEUNIT out_reso=NPY_DATETIMEUNIT.NPY_FR_ns, | ||
str unit, | ||
NPY_DATETIMEUNIT out_reso=NPY_DATETIMEUNIT.NPY_FR_ns, | ||
): | ||
MarcoGorelli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Return a casting of the unit represented to nanoseconds + the precision | ||
|
@@ -166,34 +163,21 @@ cpdef inline (int64_t, int) precision_from_unit( | |
int p | ||
NPY_DATETIMEUNIT reso = abbrev_to_npy_unit(unit) | ||
|
||
multiplier = periods_per_second(out_reso) | ||
|
||
if reso == NPY_DATETIMEUNIT.NPY_FR_GENERIC: | ||
reso = NPY_DATETIMEUNIT.NPY_FR_ns | ||
if reso == NPY_DATETIMEUNIT.NPY_FR_Y: | ||
# each 400 years we have 97 leap years, for an average of 97/400=.2425 | ||
# extra days each year. We get 31556952 by writing | ||
# 3600*24*365.2425=31556952 | ||
multiplier = periods_per_second(out_reso) | ||
m = multiplier * 31556952 | ||
elif reso == NPY_DATETIMEUNIT.NPY_FR_M: | ||
# 2629746 comes from dividing the "Y" case by 12. | ||
multiplier = periods_per_second(out_reso) | ||
m = multiplier * 2629746 | ||
elif reso == NPY_DATETIMEUNIT.NPY_FR_W: | ||
m = multiplier * 3600 * 24 * 7 | ||
elif reso == NPY_DATETIMEUNIT.NPY_FR_D: | ||
m = multiplier * 3600 * 24 | ||
elif reso == NPY_DATETIMEUNIT.NPY_FR_h: | ||
m = multiplier * 3600 | ||
elif reso == NPY_DATETIMEUNIT.NPY_FR_m: | ||
m = multiplier * 60 | ||
elif reso == NPY_DATETIMEUNIT.NPY_FR_s: | ||
m = multiplier | ||
elif reso == NPY_DATETIMEUNIT.NPY_FR_ms: | ||
m = multiplier // 1_000 | ||
elif reso == NPY_DATETIMEUNIT.NPY_FR_us: | ||
m = multiplier // 1_000_000 | ||
elif reso == NPY_DATETIMEUNIT.NPY_FR_ns or reso == NPY_DATETIMEUNIT.NPY_FR_GENERIC: | ||
m = multiplier // 1_000_000_000 | ||
else: | ||
raise ValueError(f"cannot cast unit {unit}") | ||
m = get_conversion_factor(reso, out_reso) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment sounds good There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to keep from clogging the CI, mind if i add this comment in my next CLN: assorted branch? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds good! |
||
|
||
p = <int>log10(m) # number of digits in 'm' minus 1 | ||
return m, p | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.