Skip to content

Commit 708e0b1

Browse files
jbrockmendeltm9k1
authored andcommitted
fix and test incorrect case in delta_to_nanoseconds (pandas-dev#23302)
1 parent f6e5e2b commit 708e0b1

File tree

5 files changed

+51
-13
lines changed

5 files changed

+51
-13
lines changed

pandas/_libs/tslibs/fields.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def get_time_micros(ndarray[int64_t] dtindex):
4040
return micros
4141

4242

43-
def build_field_sarray(ndarray[int64_t] dtindex):
43+
def build_field_sarray(int64_t[:] dtindex):
4444
"""
4545
Datetime as int64 representation to a structured array of fields
4646
"""
@@ -542,7 +542,7 @@ def get_date_field(ndarray[int64_t] dtindex, object field):
542542

543543
@cython.wraparound(False)
544544
@cython.boundscheck(False)
545-
def get_timedelta_field(ndarray[int64_t] tdindex, object field):
545+
def get_timedelta_field(int64_t[:] tdindex, object field):
546546
"""
547547
Given a int64-based timedelta index, extract the days, hrs, sec.,
548548
field and return an array of these values.

pandas/_libs/tslibs/timedeltas.pxd

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

55
# Exposed for tslib, not intended for outside use.
6-
cdef parse_timedelta_string(object ts)
7-
cpdef int64_t cast_from_unit(object ts, object unit) except? -1
6+
cdef int64_t cast_from_unit(object ts, object unit) except? -1
87
cpdef int64_t delta_to_nanoseconds(delta) except? -1
98
cpdef convert_to_timedelta64(object ts, object unit)
10-
cpdef array_to_timedelta64(object[:] values, unit=*, errors=*)

pandas/_libs/tslibs/timedeltas.pyx

+7-7
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ def ints_to_pytimedelta(int64_t[:] arr, box=False):
119119
# ----------------------------------------------------------------------
120120

121121
cpdef int64_t delta_to_nanoseconds(delta) except? -1:
122-
if util.is_array(delta):
123-
return delta.astype('m8[ns]').astype('int64')
124122
if hasattr(delta, 'nanos'):
125123
return delta.nanos
126124
if hasattr(delta, 'delta'):
@@ -129,10 +127,12 @@ cpdef int64_t delta_to_nanoseconds(delta) except? -1:
129127
return delta.astype("timedelta64[ns]").item()
130128
if is_integer_object(delta):
131129
return delta
130+
if PyDelta_Check(delta):
131+
return (delta.days * 24 * 60 * 60 * 1000000 +
132+
delta.seconds * 1000000 +
133+
delta.microseconds) * 1000
132134

133-
return (delta.days * 24 * 60 * 60 * 1000000 +
134-
delta.seconds * 1000000 +
135-
delta.microseconds) * 1000
135+
raise TypeError(type(delta))
136136

137137

138138
cpdef convert_to_timedelta64(object ts, object unit):
@@ -198,7 +198,7 @@ cpdef convert_to_timedelta64(object ts, object unit):
198198
return ts.astype('timedelta64[ns]')
199199

200200

201-
cpdef array_to_timedelta64(object[:] values, unit='ns', errors='raise'):
201+
def array_to_timedelta64(object[:] values, unit='ns', errors='raise'):
202202
"""
203203
Convert an ndarray to an array of timedeltas. If errors == 'coerce',
204204
coerce non-convertible objects to NaT. Otherwise, raise.
@@ -235,7 +235,7 @@ cpdef array_to_timedelta64(object[:] values, unit='ns', errors='raise'):
235235
return iresult.base # .base to access underlying np.ndarray
236236

237237

238-
cpdef inline int64_t cast_from_unit(object ts, object unit) except? -1:
238+
cdef inline int64_t cast_from_unit(object ts, object unit) except? -1:
239239
""" return a casting of the unit represented to nanoseconds
240240
round the fractional part of a float to our precision, p """
241241
cdef:

pandas/_libs/tslibs/timezones.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ cpdef bint tz_compare(object start, object end):
322322
return get_timezone(start) == get_timezone(end)
323323

324324

325-
cpdef tz_standardize(object tz):
325+
def tz_standardize(tz: object):
326326
"""
327327
If the passed tz is a pytz timezone object, "normalize" it to the a
328328
consistent version
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: utf-8 -*-
2+
import numpy as np
3+
import pytest
4+
5+
import pandas as pd
6+
from pandas._libs.tslibs.timedeltas import delta_to_nanoseconds
7+
8+
9+
def test_delta_to_nanoseconds():
10+
obj = np.timedelta64(14, 'D')
11+
result = delta_to_nanoseconds(obj)
12+
assert result == 14 * 24 * 3600 * 1e9
13+
14+
obj = pd.Timedelta(minutes=-7)
15+
result = delta_to_nanoseconds(obj)
16+
assert result == -7 * 60 * 1e9
17+
18+
obj = pd.Timedelta(minutes=-7).to_pytimedelta()
19+
result = delta_to_nanoseconds(obj)
20+
assert result == -7 * 60 * 1e9
21+
22+
obj = pd.offsets.Nano(125)
23+
result = delta_to_nanoseconds(obj)
24+
assert result == 125
25+
26+
obj = 1
27+
result = delta_to_nanoseconds(obj)
28+
assert obj == 1
29+
30+
obj = np.int64(2)
31+
result = delta_to_nanoseconds(obj)
32+
assert obj == 2
33+
34+
obj = np.int32(3)
35+
result = delta_to_nanoseconds(obj)
36+
assert result == 3
37+
38+
obj = np.array([123456789], dtype='m8[ns]')
39+
with pytest.raises(TypeError):
40+
delta_to_nanoseconds(obj)

0 commit comments

Comments
 (0)