Skip to content

BUG: Bug in timeops with non-aligned Series (GH7500) #7503

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 1 commit into from
Jun 19, 2014
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
4 changes: 2 additions & 2 deletions doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ Bug Fixes
~~~~~~~~~


- Bug in timeops with non-aligned Series (:issue:`7500`)



- Bug in ``value_counts`` where ``NaT`` did not qualify as missing (``NaN``) (:issue:`7423`)
- Bug in ``value_counts`` where ``NaT`` did not qualify as missing (``NaN``) (:issue:`7423`)



Expand Down
12 changes: 10 additions & 2 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ class _TimeOp(object):
def __init__(self, left, right, name):
self.name = name

# need to make sure that we are aligning the data
if isinstance(left, pd.Series) and isinstance(right, pd.Series):
left, right = left.align(right)

self.left = left
self.right = right
lvalues = self._convert_to_array(left, name=name)
rvalues = self._convert_to_array(right, name=name, other=lvalues)

Expand Down Expand Up @@ -426,6 +432,7 @@ def maybe_convert_for_time_op(cls, left, right, name):
is_datetime_lhs = com.is_datetime64_dtype(left)
if not (is_datetime_lhs or is_timedelta_lhs):
return None

# rops are allowed. No need for special checks, just strip off
# r part.
if name.startswith('__r'):
Expand Down Expand Up @@ -463,6 +470,7 @@ def wrapper(left, right, name=name):

if isinstance(right, pd.DataFrame):
return NotImplemented

time_converted = _TimeOp.maybe_convert_for_time_op(left, right, name)

if time_converted is None:
Expand All @@ -472,8 +480,8 @@ def wrapper(left, right, name=name):
elif time_converted == NotImplemented:
return NotImplemented
else:
lvalues = time_converted.lvalues
rvalues = time_converted.rvalues
left, right = time_converted.left, time_converted.right
lvalues, rvalues = time_converted.lvalues, time_converted.rvalues
dtype = time_converted.dtype
wrap_results = time_converted.wrap_results

Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2838,6 +2838,24 @@ def run_ops(ops, get_ser, test_ser):
td1 + dt1
dt1 + td1

def test_ops_datetimelike_align(self):
if _np_version_under1p7:
raise nose.SkipTest("timedelta broken in np < 1.7")

# GH 7500
# datetimelike ops need to align
dt = Series(date_range('2012-1-1', periods=3, freq='D'))
dt.iloc[2] = np.nan
dt2 = dt[::-1]

expected = Series([timedelta(0),timedelta(0),pd.NaT])

result = dt2-dt
assert_series_equal(result,expected)

result = (dt2.to_frame()-dt.to_frame())[0]
assert_series_equal(result,expected)

def test_timedelta64_functions(self):

from datetime import timedelta
Expand Down