Skip to content

Commit 512830b

Browse files
jbrockmendeljreback
authored andcommitted
Avoid unnecessary use of coerce_scalar_to_timedelta_type (#24793)
1 parent 9f4357b commit 512830b

File tree

4 files changed

+5
-15
lines changed

4 files changed

+5
-15
lines changed

pandas/core/computation/pytables.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from pandas.core.computation.common import _ensure_decoded
1717
from pandas.core.computation.expr import BaseExprVisitor
1818
from pandas.core.computation.ops import UndefinedVariableError, is_term
19-
from pandas.core.tools.timedeltas import _coerce_scalar_to_timedelta_type
2019

2120
from pandas.io.formats.printing import pprint_thing, pprint_thing_encoded
2221

@@ -191,7 +190,7 @@ def stringify(value):
191190
v = v.tz_convert('UTC')
192191
return TermValue(v, v.value, kind)
193192
elif kind == u('timedelta64') or kind == u('timedelta'):
194-
v = _coerce_scalar_to_timedelta_type(v, unit='s').value
193+
v = pd.Timedelta(v, unit='s').value
195194
return TermValue(int(v), v, kind)
196195
elif meta == u('category'):
197196
metadata = com.values_from_object(self.metadata)

pandas/core/dtypes/cast.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -569,16 +569,14 @@ def coerce_to_dtypes(result, dtypes):
569569
if len(result) != len(dtypes):
570570
raise AssertionError("_coerce_to_dtypes requires equal len arrays")
571571

572-
from pandas.core.tools.timedeltas import _coerce_scalar_to_timedelta_type
573-
574572
def conv(r, dtype):
575573
try:
576574
if isna(r):
577575
pass
578576
elif dtype == _NS_DTYPE:
579577
r = tslibs.Timestamp(r)
580578
elif dtype == _TD_DTYPE:
581-
r = _coerce_scalar_to_timedelta_type(r)
579+
r = tslibs.Timedelta(r)
582580
elif dtype == np.bool_:
583581
# messy. non 0/1 integers do not get converted.
584582
if is_integer(r) and r not in [0, 1]:

pandas/core/indexes/timedeltas.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
wrap_arithmetic_op)
2727
from pandas.core.indexes.numeric import Int64Index
2828
from pandas.core.ops import get_op_result_name
29-
from pandas.core.tools.timedeltas import _coerce_scalar_to_timedelta_type
3029

3130
from pandas.tseries.frequencies import to_offset
3231

@@ -582,7 +581,7 @@ def _maybe_cast_slice_bound(self, label, side, kind):
582581
assert kind in ['ix', 'loc', 'getitem', None]
583582

584583
if isinstance(label, compat.string_types):
585-
parsed = _coerce_scalar_to_timedelta_type(label, box=True)
584+
parsed = Timedelta(label)
586585
lbound = parsed.round(parsed.resolution)
587586
if side == 'left':
588587
return lbound

pandas/tests/frame/test_analytics.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# -*- coding: utf-8 -*-
22

3-
from __future__ import print_function
4-
53
from datetime import timedelta
64
import operator
75
from string import ascii_lowercase
@@ -1128,7 +1126,6 @@ def test_mode_sortwarning(self):
11281126
tm.assert_frame_equal(result, expected)
11291127

11301128
def test_operators_timedelta64(self):
1131-
from datetime import timedelta
11321129
df = DataFrame(dict(A=date_range('2012-1-1', periods=3, freq='D'),
11331130
B=date_range('2012-1-2', periods=3, freq='D'),
11341131
C=Timestamp('20120101') -
@@ -1169,12 +1166,9 @@ def test_operators_timedelta64(self):
11691166
mixed['F'] = Timestamp('20130101')
11701167

11711168
# results in an object array
1172-
from pandas.core.tools.timedeltas import (
1173-
_coerce_scalar_to_timedelta_type as _coerce)
1174-
11751169
result = mixed.min()
1176-
expected = Series([_coerce(timedelta(seconds=5 * 60 + 5)),
1177-
_coerce(timedelta(days=-1)),
1170+
expected = Series([pd.Timedelta(timedelta(seconds=5 * 60 + 5)),
1171+
pd.Timedelta(timedelta(days=-1)),
11781172
'foo', 1, 1.0,
11791173
Timestamp('20130101')],
11801174
index=mixed.columns)

0 commit comments

Comments
 (0)