Skip to content

Commit 8b06a50

Browse files
committed
TYP: type unit as str (pandas-dev#35099)
1 parent e1507dd commit 8b06a50

File tree

6 files changed

+25
-12
lines changed

6 files changed

+25
-12
lines changed

pandas/_libs/tslib.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@ def format_array_from_datetime(
362362

363363
def array_with_unit_to_datetime(
364364
ndarray values,
365-
object unit,
366-
str errors='coerce'
365+
str unit,
366+
str errors="coerce"
367367
):
368368
"""
369369
Convert the ndarray to datetime according to the time unit.
@@ -383,7 +383,7 @@ def array_with_unit_to_datetime(
383383
----------
384384
values : ndarray of object
385385
Date-like objects to convert.
386-
unit : object
386+
unit : str
387387
Time unit to use during conversion.
388388
errors : str, default 'raise'
389389
Error behavior when parsing.

pandas/_libs/tslibs/conversion.pxd

Lines changed: 1 addition & 1 deletion
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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,19 @@ TD64NS_DTYPE = np.dtype('m8[ns]')
5555
# Unit Conversion Helpers
5656

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

308319

309-
cdef convert_to_tsobject(object ts, tzinfo tz, object unit,
320+
cdef convert_to_tsobject(object ts, tzinfo tz, str unit,
310321
bint dayfirst, bint yearfirst, int32_t nanos=0):
311322
"""
312323
Extract datetime and int64 from any of:
@@ -496,7 +507,7 @@ cdef _TSObject _create_tsobject_tz_using_offset(npy_datetimestruct dts,
496507
return obj
497508

498509

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

pandas/_libs/tslibs/timedeltas.pxd

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,7 @@ class Timestamp(_Timestamp):
10521052
nanosecond = hour
10531053
tz = minute
10541054
freq = None
1055+
unit = None
10551056

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

0 commit comments

Comments
 (0)