From 144695acaac83337ad2c0ac2599f7dfb0883c288 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Tue, 15 Jan 2019 18:58:10 -0800 Subject: [PATCH] Avoid unnecessary use of coerce_scalar_to_timedelta_type --- pandas/core/computation/pytables.py | 3 +-- pandas/core/dtypes/cast.py | 4 +--- pandas/core/indexes/timedeltas.py | 3 +-- pandas/tests/frame/test_analytics.py | 10 ++-------- 4 files changed, 5 insertions(+), 15 deletions(-) diff --git a/pandas/core/computation/pytables.py b/pandas/core/computation/pytables.py index db409b215a78d..00de29b07c75d 100644 --- a/pandas/core/computation/pytables.py +++ b/pandas/core/computation/pytables.py @@ -16,7 +16,6 @@ from pandas.core.computation.common import _ensure_decoded from pandas.core.computation.expr import BaseExprVisitor from pandas.core.computation.ops import UndefinedVariableError, is_term -from pandas.core.tools.timedeltas import _coerce_scalar_to_timedelta_type from pandas.io.formats.printing import pprint_thing, pprint_thing_encoded @@ -191,7 +190,7 @@ def stringify(value): v = v.tz_convert('UTC') return TermValue(v, v.value, kind) elif kind == u('timedelta64') or kind == u('timedelta'): - v = _coerce_scalar_to_timedelta_type(v, unit='s').value + v = pd.Timedelta(v, unit='s').value return TermValue(int(v), v, kind) elif meta == u('category'): metadata = com.values_from_object(self.metadata) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index bd6094596c5e1..4049b0321f221 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -569,8 +569,6 @@ def coerce_to_dtypes(result, dtypes): if len(result) != len(dtypes): raise AssertionError("_coerce_to_dtypes requires equal len arrays") - from pandas.core.tools.timedeltas import _coerce_scalar_to_timedelta_type - def conv(r, dtype): try: if isna(r): @@ -578,7 +576,7 @@ def conv(r, dtype): elif dtype == _NS_DTYPE: r = tslibs.Timestamp(r) elif dtype == _TD_DTYPE: - r = _coerce_scalar_to_timedelta_type(r) + r = tslibs.Timedelta(r) elif dtype == np.bool_: # messy. non 0/1 integers do not get converted. if is_integer(r) and r not in [0, 1]: diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index 893926cc076ab..cbe5ae198838f 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -26,7 +26,6 @@ wrap_arithmetic_op) from pandas.core.indexes.numeric import Int64Index from pandas.core.ops import get_op_result_name -from pandas.core.tools.timedeltas import _coerce_scalar_to_timedelta_type from pandas.tseries.frequencies import to_offset @@ -582,7 +581,7 @@ def _maybe_cast_slice_bound(self, label, side, kind): assert kind in ['ix', 'loc', 'getitem', None] if isinstance(label, compat.string_types): - parsed = _coerce_scalar_to_timedelta_type(label, box=True) + parsed = Timedelta(label) lbound = parsed.round(parsed.resolution) if side == 'left': return lbound diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index 9f64b71ea455c..244e8f83bea37 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- -from __future__ import print_function - from datetime import timedelta import operator from string import ascii_lowercase @@ -1128,7 +1126,6 @@ def test_mode_sortwarning(self): tm.assert_frame_equal(result, expected) def test_operators_timedelta64(self): - from datetime import timedelta df = DataFrame(dict(A=date_range('2012-1-1', periods=3, freq='D'), B=date_range('2012-1-2', periods=3, freq='D'), C=Timestamp('20120101') - @@ -1169,12 +1166,9 @@ def test_operators_timedelta64(self): mixed['F'] = Timestamp('20130101') # results in an object array - from pandas.core.tools.timedeltas import ( - _coerce_scalar_to_timedelta_type as _coerce) - result = mixed.min() - expected = Series([_coerce(timedelta(seconds=5 * 60 + 5)), - _coerce(timedelta(days=-1)), + expected = Series([pd.Timedelta(timedelta(seconds=5 * 60 + 5)), + pd.Timedelta(timedelta(days=-1)), 'foo', 1, 1.0, Timestamp('20130101')], index=mixed.columns)