Skip to content

Commit c4c4ec6

Browse files
committed
Minor fix
1 parent 5a0812c commit c4c4ec6

File tree

3 files changed

+12
-76
lines changed

3 files changed

+12
-76
lines changed

pandas/_libs/tslibs/timedeltas.pxd

-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ from numpy cimport int64_t
22

33
# Exposed for tslib, not intended for outside use.
44
cpdef parse_timedelta_string(object ts, object specified_unit=*)
5-
cdef int64_t cast_from_unit(object ts, object unit) except? -1
65
cpdef int64_t delta_to_nanoseconds(delta) except? -1
76
cpdef convert_to_timedelta64(object ts, object unit=*)

pandas/_libs/tslibs/timedeltas.pyx

+1-61
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import collections
2+
import warnings
23

34
import cython
45

@@ -257,67 +258,6 @@ def array_to_timedelta64(object[:] values, unit=None, errors='raise'):
257258
return iresult.base # .base to access underlying np.ndarray
258259

259260

260-
cdef inline int64_t cast_from_unit(object ts, object unit) except? -1:
261-
""" return a casting of the unit represented to nanoseconds
262-
round the fractional part of a float to our precision, p """
263-
cdef:
264-
int64_t m
265-
int p
266-
267-
if unit == 'Y':
268-
m = 1000000000L * 31556952
269-
p = 9
270-
elif unit == 'M':
271-
m = 1000000000L * 2629746
272-
p = 9
273-
elif unit == 'W':
274-
m = 1000000000L * DAY_SECONDS * 7
275-
p = 9
276-
elif unit == 'D' or unit == 'd':
277-
m = 1000000000L * DAY_SECONDS
278-
p = 9
279-
elif unit == 'h':
280-
m = 1000000000L * 3600
281-
p = 9
282-
elif unit == 'm':
283-
m = 1000000000L * 60
284-
p = 9
285-
elif unit == 's':
286-
m = 1000000000L
287-
p = 9
288-
elif unit == 'ms':
289-
m = 1000000L
290-
p = 6
291-
elif unit == 'us':
292-
m = 1000L
293-
p = 3
294-
elif unit == 'ns' or unit is None:
295-
m = 1L
296-
p = 0
297-
else:
298-
raise ValueError("cannot cast unit {unit}".format(unit=unit))
299-
300-
# just give me the unit back
301-
if ts is None:
302-
return m
303-
304-
# cast the unit, multiply base/frace separately
305-
# to avoid precision issues from float -> int
306-
base = <int64_t>ts
307-
frac = ts - base
308-
if p:
309-
frac = round(frac, p)
310-
return <int64_t>(base * m) + <int64_t>(frac * m)
311-
312-
313-
cdef inline _decode_if_necessary(object ts):
314-
# decode ts if necessary
315-
if not isinstance(ts, unicode) and not PY3:
316-
ts = str(ts).decode('utf-8')
317-
318-
return ts
319-
320-
321261
cpdef inline parse_timedelta_string(object ts, specified_unit=None):
322262
"""
323263
Parse a regular format timedelta string. Return an int64_t (in ns)

pandas/core/tools/timedeltas.py

+11-14
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
import numpy as np
66

7-
from pandas._libs import tslibs
7+
from pandas._libs.tslibs import NaT
88
from pandas._libs.tslibs.timedeltas import (
9+
Timedelta,
910
convert_to_timedelta64,
1011
parse_timedelta_string,
1112
parse_timedelta_unit,
@@ -115,19 +116,15 @@ def to_timedelta(arg, unit=None, box=True, errors='raise'):
115116
def _coerce_scalar_to_timedelta_type(r, unit=None, box=True, errors='raise'):
116117
"""Convert string 'r' to a timedelta object."""
117118
try:
118-
result = parse_timedelta_string(r, unit)
119-
result = np.timedelta64(result)
120-
except (ValueError, TypeError):
121-
try:
122-
result = convert_to_timedelta64(r, unit)
123-
except ValueError:
124-
if errors == 'raise':
125-
raise
126-
elif errors == 'ignore':
127-
return r
128-
129-
# coerce
130-
result = pd.NaT
119+
result = Timedelta(r, unit)
120+
except ValueError:
121+
if errors == 'raise':
122+
raise
123+
elif errors == 'ignore':
124+
return r
125+
126+
# coerce
127+
result = NaT
131128

132129
return result
133130

0 commit comments

Comments
 (0)