Skip to content

Commit 507ff0f

Browse files
committed
Minor fix
1 parent 5a0812c commit 507ff0f

File tree

9 files changed

+109
-449
lines changed

9 files changed

+109
-449
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/computation/pytables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def stringify(value):
200200
v = v.tz_convert("UTC")
201201
return TermValue(v, v.value, kind)
202202
elif kind == "timedelta64" or kind == "timedelta":
203-
v = Timedelta(v, unit="s").value
203+
v = Timedelta(v).value
204204
return TermValue(int(v), v, kind)
205205
elif meta == "category":
206206
metadata = extract_array(self.metadata, extract_numpy=True)

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

pandas/tests/indexes/timedeltas/test_ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ def test_drop_duplicates(self, freq_sample, keep, expected, index):
180180

181181
def test_infer_freq(self, freq_sample):
182182
# GH#11018
183-
idx = pd.timedelta_range('1ns', freq=freq, periods=10)
184-
result = pd.TimedeltaIndex(idx.asi8, freq='infer')
183+
idx = pd.timedelta_range("1ns", freq=freq_sample, periods=10)
184+
result = pd.TimedeltaIndex(idx.asi8, freq="infer")
185185
tm.assert_index_equal(idx, result)
186186
assert result.freq == freq_sample
187187

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
import pytest
33

4-
from pandas import Series, timedelta_range
4+
from pandas import IndexSlice, Series, timedelta_range
55
import pandas._testing as tm
66

77

@@ -44,40 +44,4 @@ def test_partial_slice_high_reso(self):
4444
tm.assert_series_equal(result, expected)
4545

4646
result = s["1 days, 10:11:12.001001"]
47-
assert result == s.iloc[1001]
48-
49-
def test_slice_with_negative_step(self):
50-
ts = Series(np.arange(20),
51-
timedelta_range('0ns', periods=20, freq='H'))
52-
SLC = pd.IndexSlice
53-
54-
def assert_slices_equivalent(l_slc, i_slc):
55-
assert_series_equal(ts[l_slc], ts.iloc[i_slc])
56-
assert_series_equal(ts.loc[l_slc], ts.iloc[i_slc])
57-
assert_series_equal(ts.loc[l_slc], ts.iloc[i_slc])
58-
59-
assert_slices_equivalent(SLC[Timedelta(hours=7)::-1], SLC[7::-1])
60-
assert_slices_equivalent(SLC['7 hours'::-1], SLC[7::-1])
61-
62-
assert_slices_equivalent(SLC[:Timedelta(hours=7):-1], SLC[:6:-1])
63-
assert_slices_equivalent(SLC[:'7 hours':-1], SLC[:6:-1])
64-
65-
assert_slices_equivalent(SLC['15 hours':'7 hours':-1], SLC[15:6:-1])
66-
assert_slices_equivalent(SLC[Timedelta(hours=15):Timedelta(hours=7):-
67-
1], SLC[15:6:-1])
68-
assert_slices_equivalent(SLC['15 hours':Timedelta(hours=7):-1],
69-
SLC[15:6:-1])
70-
assert_slices_equivalent(SLC[Timedelta(hours=15):'7 hours':-1],
71-
SLC[15:6:-1])
72-
73-
assert_slices_equivalent(SLC['7 hours':'15 hours':-1], SLC[:0])
74-
75-
def test_slice_with_zero_step_raises(self):
76-
ts = Series(np.arange(20),
77-
timedelta_range('0ns', periods=20, freq='H'))
78-
with pytest.raises(ValueError, match='slice step cannot be zero'):
79-
ts[::0]
80-
with pytest.raises(ValueError, match='slice step cannot be zero'):
81-
ts.loc[::0]
82-
with pytest.raises(ValueError, match='slice step cannot be zero'):
83-
ts.loc[::0]
47+
assert result == s.iloc[1001]

0 commit comments

Comments
 (0)