Skip to content

Avoid unnecessary use of _coerce_scalar_to_timedelta_type #24793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pandas/core/computation/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,16 +569,14 @@ 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:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw an upcoming PR is going to make the try/except part of this function unnecessary.

if isna(r):
pass
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]:
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
10 changes: 2 additions & 8 deletions pandas/tests/frame/test_analytics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-

from __future__ import print_function

from datetime import timedelta
import operator
from string import ascii_lowercase
Expand Down Expand Up @@ -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') -
Expand Down Expand Up @@ -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)
Expand Down