diff --git a/pandas/core/series.py b/pandas/core/series.py index 7dd9a84199ea8..e75471a34746a 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -42,6 +42,7 @@ _np_version = np.version.short_version _np_version_under1p6 = LooseVersion(_np_version) < '1.6' +_np_version_under1p7 = LooseVersion(_np_version) < '1.7' _SHOW_WARNINGS = True @@ -71,19 +72,30 @@ def na_op(x, y): def wrapper(self, other): from pandas.core.frame import DataFrame + wrap_results = lambda x: x + + lvalues, rvalues = self, other + + if (com.is_datetime64_dtype(self) and + com.is_datetime64_dtype(other)): + lvalues = lvalues.view('i8') + rvalues = rvalues.view('i8') + + wrap_results = lambda rs: rs.astype('timedelta64[ns]') + + if isinstance(rvalues, Series): + lvalues = lvalues.values + rvalues = rvalues.values + - if isinstance(other, Series): if self.index.equals(other.index): name = _maybe_match_name(self, other) - return Series(na_op(self.values, other.values), + return Series(wrap_results(na_op(lvalues, rvalues)), index=self.index, name=name) join_idx, lidx, ridx = self.index.join(other.index, how='outer', return_indexers=True) - lvalues = self.values - rvalues = other.values - if lidx is not None: lvalues = com.take_1d(lvalues, lidx) @@ -98,7 +110,7 @@ def wrapper(self, other): return NotImplemented else: # scalars - return Series(na_op(self.values, other), + return Series(na_op(lvalues.values, rvalues), index=self.index, name=self.name) return wrapper diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index f0112e1eeea18..91fdc244b077f 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -1543,6 +1543,13 @@ def test_operators_empty_int_corner(self): # it works! _ = s1 * s2 + def test_operators_datetime64(self): + v1 = date_range('2012-1-1', periods=3, freq='D') + v2 = date_range('2012-1-2', periods=3, freq='D') + rs = Series(v2) - Series(v1) + xp = Series(1e9 * 3600 * 24, rs.index).astype('timedelta64[ns]') + assert_series_equal(rs, xp) + # NumPy limitiation =( # def test_logical_range_select(self):