diff --git a/pandas/_libs/tslibs/fields.pyx b/pandas/_libs/tslibs/fields.pyx index 684344ceb9002..1af9cd619c5f9 100644 --- a/pandas/_libs/tslibs/fields.pyx +++ b/pandas/_libs/tslibs/fields.pyx @@ -40,7 +40,7 @@ def get_time_micros(ndarray[int64_t] dtindex): return micros -def build_field_sarray(ndarray[int64_t] dtindex): +def build_field_sarray(int64_t[:] dtindex): """ Datetime as int64 representation to a structured array of fields """ @@ -542,7 +542,7 @@ def get_date_field(ndarray[int64_t] dtindex, object field): @cython.wraparound(False) @cython.boundscheck(False) -def get_timedelta_field(ndarray[int64_t] tdindex, object field): +def get_timedelta_field(int64_t[:] tdindex, object field): """ Given a int64-based timedelta index, extract the days, hrs, sec., field and return an array of these values. diff --git a/pandas/_libs/tslibs/timedeltas.pxd b/pandas/_libs/tslibs/timedeltas.pxd index eda4418902513..c02a840281266 100644 --- a/pandas/_libs/tslibs/timedeltas.pxd +++ b/pandas/_libs/tslibs/timedeltas.pxd @@ -3,8 +3,6 @@ from numpy cimport int64_t # Exposed for tslib, not intended for outside use. -cdef parse_timedelta_string(object ts) -cpdef int64_t cast_from_unit(object ts, object unit) except? -1 +cdef int64_t cast_from_unit(object ts, object unit) except? -1 cpdef int64_t delta_to_nanoseconds(delta) except? -1 cpdef convert_to_timedelta64(object ts, object unit) -cpdef array_to_timedelta64(object[:] values, unit=*, errors=*) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 9c8be1901d1dc..d4e0e7f8ad72d 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -119,8 +119,6 @@ def ints_to_pytimedelta(int64_t[:] arr, box=False): # ---------------------------------------------------------------------- cpdef int64_t delta_to_nanoseconds(delta) except? -1: - if util.is_array(delta): - return delta.astype('m8[ns]').astype('int64') if hasattr(delta, 'nanos'): return delta.nanos if hasattr(delta, 'delta'): @@ -129,10 +127,12 @@ cpdef int64_t delta_to_nanoseconds(delta) except? -1: return delta.astype("timedelta64[ns]").item() if is_integer_object(delta): return delta + if PyDelta_Check(delta): + return (delta.days * 24 * 60 * 60 * 1000000 + + delta.seconds * 1000000 + + delta.microseconds) * 1000 - return (delta.days * 24 * 60 * 60 * 1000000 + - delta.seconds * 1000000 + - delta.microseconds) * 1000 + raise TypeError(type(delta)) cpdef convert_to_timedelta64(object ts, object unit): @@ -198,7 +198,7 @@ cpdef convert_to_timedelta64(object ts, object unit): return ts.astype('timedelta64[ns]') -cpdef array_to_timedelta64(object[:] values, unit='ns', errors='raise'): +def array_to_timedelta64(object[:] values, unit='ns', errors='raise'): """ Convert an ndarray to an array of timedeltas. If errors == 'coerce', coerce non-convertible objects to NaT. Otherwise, raise. @@ -235,7 +235,7 @@ cpdef array_to_timedelta64(object[:] values, unit='ns', errors='raise'): return iresult.base # .base to access underlying np.ndarray -cpdef inline int64_t cast_from_unit(object ts, object unit) except? -1: +cdef inline int64_t cast_from_unit(object ts, object unit) except? -1: """ return a casting of the unit represented to nanoseconds round the fractional part of a float to our precision, p """ cdef: diff --git a/pandas/_libs/tslibs/timezones.pyx b/pandas/_libs/tslibs/timezones.pyx index b7e4de81da35c..1fc1347c8b9e3 100644 --- a/pandas/_libs/tslibs/timezones.pyx +++ b/pandas/_libs/tslibs/timezones.pyx @@ -322,7 +322,7 @@ cpdef bint tz_compare(object start, object end): return get_timezone(start) == get_timezone(end) -cpdef tz_standardize(object tz): +def tz_standardize(tz: object): """ If the passed tz is a pytz timezone object, "normalize" it to the a consistent version diff --git a/pandas/tests/tslibs/test_timedeltas.py b/pandas/tests/tslibs/test_timedeltas.py new file mode 100644 index 0000000000000..939c2b828a75f --- /dev/null +++ b/pandas/tests/tslibs/test_timedeltas.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +import numpy as np +import pytest + +import pandas as pd +from pandas._libs.tslibs.timedeltas import delta_to_nanoseconds + + +def test_delta_to_nanoseconds(): + obj = np.timedelta64(14, 'D') + result = delta_to_nanoseconds(obj) + assert result == 14 * 24 * 3600 * 1e9 + + obj = pd.Timedelta(minutes=-7) + result = delta_to_nanoseconds(obj) + assert result == -7 * 60 * 1e9 + + obj = pd.Timedelta(minutes=-7).to_pytimedelta() + result = delta_to_nanoseconds(obj) + assert result == -7 * 60 * 1e9 + + obj = pd.offsets.Nano(125) + result = delta_to_nanoseconds(obj) + assert result == 125 + + obj = 1 + result = delta_to_nanoseconds(obj) + assert obj == 1 + + obj = np.int64(2) + result = delta_to_nanoseconds(obj) + assert obj == 2 + + obj = np.int32(3) + result = delta_to_nanoseconds(obj) + assert result == 3 + + obj = np.array([123456789], dtype='m8[ns]') + with pytest.raises(TypeError): + delta_to_nanoseconds(obj)