Skip to content

Commit

Permalink
fix and test incorrect case in delta_to_nanoseconds (pandas-dev#23302)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and tm9k1 committed Nov 19, 2018
1 parent f6e5e2b commit 708e0b1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/fields.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions pandas/_libs/tslibs/timedeltas.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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=*)
14 changes: 7 additions & 7 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand All @@ -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):
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/timezones.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions pandas/tests/tslibs/test_timedeltas.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 708e0b1

Please sign in to comment.