Skip to content

Commit f3205b2

Browse files
author
MarcoGorelli
committed
use libc log10, reso -> out_reso, periods_per_second
1 parent b692b03 commit f3205b2

File tree

2 files changed

+7
-15
lines changed

2 files changed

+7
-15
lines changed

pandas/_libs/tslibs/conversion.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ cdef _TSObject convert_str_to_tsobject(str ts, tzinfo tz, str unit,
4242
cdef int64_t get_datetime64_nanos(object val, NPY_DATETIMEUNIT reso) except? -1
4343

4444
cpdef datetime localize_pydatetime(datetime dt, tzinfo tz)
45-
cdef int64_t cast_from_unit(object ts, str unit, NPY_DATETIMEUNIT reso=*) except? -1
45+
cdef int64_t cast_from_unit(object ts, str unit, NPY_DATETIMEUNIT out_reso=*) except? -1
4646
cpdef (int64_t, int) precision_from_unit(str unit, NPY_DATETIMEUNIT out_reso=*)
4747

4848
cdef maybe_localize_tso(_TSObject obj, tzinfo tz, NPY_DATETIMEUNIT reso)

pandas/_libs/tslibs/conversion.pyx

+6-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22

33
cimport numpy as cnp
4+
from libc.math cimport log10
45
from numpy cimport (
56
int32_t,
67
int64_t,
@@ -84,7 +85,7 @@ TD64NS_DTYPE = np.dtype("m8[ns]")
8485
cdef int64_t cast_from_unit(
8586
object ts,
8687
str unit,
87-
NPY_DATETIMEUNIT reso=NPY_FR_ns
88+
NPY_DATETIMEUNIT out_reso=NPY_FR_ns
8889
) except? -1:
8990
"""
9091
Return a casting of the unit represented to nanoseconds
@@ -103,7 +104,7 @@ cdef int64_t cast_from_unit(
103104
int64_t m
104105
int p
105106

106-
m, p = precision_from_unit(unit, reso)
107+
m, p = precision_from_unit(unit, out_reso)
107108

108109
# just give me the unit back
109110
if ts is None:
@@ -123,7 +124,7 @@ cdef int64_t cast_from_unit(
123124
if is_float_object(ts):
124125
ts = int(ts)
125126
dt64obj = np.datetime64(ts, unit)
126-
return get_datetime64_nanos(dt64obj, reso)
127+
return get_datetime64_nanos(dt64obj, out_reso)
127128

128129
# cast the unit, multiply base/frac separately
129130
# to avoid precision issues from float -> int
@@ -165,16 +166,7 @@ cpdef inline (int64_t, int) precision_from_unit(
165166
int p
166167
NPY_DATETIMEUNIT reso = abbrev_to_npy_unit(unit)
167168

168-
if out_reso == NPY_DATETIMEUNIT.NPY_FR_ns:
169-
multiplier = 1_000_000_000
170-
elif out_reso == NPY_DATETIMEUNIT.NPY_FR_us:
171-
multiplier = 1_000_000
172-
elif out_reso == NPY_DATETIMEUNIT.NPY_FR_ms:
173-
multiplier = 1_000
174-
elif out_reso == NPY_DATETIMEUNIT.NPY_FR_s:
175-
multiplier = 1
176-
else:
177-
raise ValueError(f"Invalid out_reso: {out_reso}")
169+
multiplier = periods_per_second(out_reso)
178170

179171
if reso == NPY_DATETIMEUNIT.NPY_FR_Y:
180172
# each 400 years we have 97 leap years, for an average of 97/400=.2425
@@ -202,7 +194,7 @@ cpdef inline (int64_t, int) precision_from_unit(
202194
m = multiplier // 1_000_000_000
203195
else:
204196
raise ValueError(f"cannot cast unit {unit}")
205-
p = min(9, len(str(m))-1)
197+
p = <int>log10(m) # number of digits in 'm' minus 1
206198
return m, p
207199

208200

0 commit comments

Comments
 (0)