Skip to content

Commit 7453810

Browse files
authored
TYP: type unit as str (pandas-dev#35099)
1 parent 15884f3 commit 7453810

File tree

6 files changed

+25
-12
lines changed

6 files changed

+25
-12
lines changed

pandas/_libs/tslib.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,8 @@ def format_array_from_datetime(
363363

364364
def array_with_unit_to_datetime(
365365
ndarray values,
366-
object unit,
367-
str errors='coerce'
366+
str unit,
367+
str errors="coerce"
368368
):
369369
"""
370370
Convert the ndarray to datetime according to the time unit.
@@ -384,7 +384,7 @@ def array_with_unit_to_datetime(
384384
----------
385385
values : ndarray of object
386386
Date-like objects to convert.
387-
unit : object
387+
unit : str
388388
Time unit to use during conversion.
389389
errors : str, default 'raise'
390390
Error behavior when parsing.

pandas/_libs/tslibs/conversion.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ cdef class _TSObject:
1313
bint fold
1414

1515

16-
cdef convert_to_tsobject(object ts, tzinfo tz, object unit,
16+
cdef convert_to_tsobject(object ts, tzinfo tz, str unit,
1717
bint dayfirst, bint yearfirst,
1818
int32_t nanos=*)
1919

pandas/_libs/tslibs/conversion.pyx

+16-4
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,19 @@ TD64NS_DTYPE = np.dtype('m8[ns]')
5656
# Unit Conversion Helpers
5757

5858
cdef inline int64_t cast_from_unit(object ts, str unit) except? -1:
59-
""" return a casting of the unit represented to nanoseconds
60-
round the fractional part of a float to our precision, p """
59+
"""
60+
Return a casting of the unit represented to nanoseconds
61+
round the fractional part of a float to our precision, p.
62+
63+
Parameters
64+
----------
65+
ts : int, float, or None
66+
unit : str
67+
68+
Returns
69+
-------
70+
int64_t
71+
"""
6172
cdef:
6273
int64_t m
6374
int p
@@ -307,7 +318,7 @@ cdef class _TSObject:
307318
return self.value
308319

309320

310-
cdef convert_to_tsobject(object ts, tzinfo tz, object unit,
321+
cdef convert_to_tsobject(object ts, tzinfo tz, str unit,
311322
bint dayfirst, bint yearfirst, int32_t nanos=0):
312323
"""
313324
Extract datetime and int64 from any of:
@@ -497,7 +508,7 @@ cdef _TSObject _create_tsobject_tz_using_offset(npy_datetimestruct dts,
497508
return obj
498509

499510

500-
cdef _TSObject _convert_str_to_tsobject(object ts, tzinfo tz, object unit,
511+
cdef _TSObject _convert_str_to_tsobject(object ts, tzinfo tz, str unit,
501512
bint dayfirst=False,
502513
bint yearfirst=False):
503514
"""
@@ -513,6 +524,7 @@ cdef _TSObject _convert_str_to_tsobject(object ts, tzinfo tz, object unit,
513524
Value to be converted to _TSObject
514525
tz : tzinfo or None
515526
timezone for the timezone-aware output
527+
unit : str or None
516528
dayfirst : bool, default False
517529
When parsing an ambiguous date string, interpret e.g. "3/4/1975" as
518530
April 3, as opposed to the standard US interpretation March 4.

pandas/_libs/tslibs/timedeltas.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from numpy cimport int64_t
33

44
# Exposed for tslib, not intended for outside use.
55
cpdef int64_t delta_to_nanoseconds(delta) except? -1
6-
cdef convert_to_timedelta64(object ts, object unit)
6+
cdef convert_to_timedelta64(object ts, str unit)
77
cdef bint is_any_td_scalar(object obj)
88

99

pandas/_libs/tslibs/timedeltas.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ cpdef int64_t delta_to_nanoseconds(delta) except? -1:
160160
raise TypeError(type(delta))
161161

162162

163-
cdef convert_to_timedelta64(object ts, object unit):
163+
cdef convert_to_timedelta64(object ts, str unit):
164164
"""
165165
Convert an incoming object to a timedelta64 if possible.
166166
Before calling, unit must be standardized to avoid repeated unit conversion
@@ -218,7 +218,7 @@ cdef convert_to_timedelta64(object ts, object unit):
218218

219219
@cython.boundscheck(False)
220220
@cython.wraparound(False)
221-
def array_to_timedelta64(object[:] values, unit=None, errors='raise'):
221+
def array_to_timedelta64(object[:] values, str unit=None, str errors="raise"):
222222
"""
223223
Convert an ndarray to an array of timedeltas. If errors == 'coerce',
224224
coerce non-convertible objects to NaT. Otherwise, raise.
@@ -470,7 +470,7 @@ cdef inline timedelta_from_spec(object number, object frac, object unit):
470470
return cast_from_unit(float(n), unit)
471471

472472

473-
cpdef inline str parse_timedelta_unit(object unit):
473+
cpdef inline str parse_timedelta_unit(str unit):
474474
"""
475475
Parameters
476476
----------

pandas/_libs/tslibs/timestamps.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,7 @@ class Timestamp(_Timestamp):
10501050
nanosecond = hour
10511051
tz = minute
10521052
freq = None
1053+
unit = None
10531054

10541055
if getattr(ts_input, 'tzinfo', None) is not None and tz is not None:
10551056
raise ValueError("Cannot pass a datetime or Timestamp with tzinfo with "

0 commit comments

Comments
 (0)