Skip to content

Commit bca30ba

Browse files
committed
TST: add arithmetic operators fixture
1 parent a5c02d5 commit bca30ba

File tree

2 files changed

+70
-33
lines changed

2 files changed

+70
-33
lines changed

pandas/conftest.py

+19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import numpy as np
44
import pandas as pd
5+
from pandas.compat import PY3
56
import pandas.util._test_decorators as td
67

78

@@ -77,6 +78,24 @@ def observed(request):
7778
return request.param
7879

7980

81+
_all_arithmetic_operators = ['__add__', '__radd__',
82+
'__sub__', '__rsub__',
83+
'__mul__', '__rmul__',
84+
'__floordiv__', '__rfloordiv__',
85+
'__truediv__', '__rtruediv__',
86+
'__pow__', '__rpow__']
87+
if not PY3:
88+
_all_arithmetic_operators.extend(['__div__', '__rdiv__'])
89+
90+
91+
@pytest.fixture(params=_all_arithmetic_operators)
92+
def all_arithmetic_operators(request):
93+
"""
94+
Fixture for common arithmetic operations
95+
"""
96+
return request.param
97+
98+
8099
@pytest.fixture(params=[None, 'gzip', 'bz2', 'zip',
81100
pytest.param('xz', marks=td.skip_if_no_lzma)])
82101
def compression(request):

pandas/tests/series/test_operators.py

+51-33
Original file line numberDiff line numberDiff line change
@@ -827,16 +827,52 @@ def test_sub_datetime64_not_ns(self, box, assert_func):
827827
res = dt64 - obj
828828
assert_func(res, -expected)
829829

830-
def test_operators_datetimelike(self):
831-
def run_ops(ops, get_ser, test_ser):
830+
def test_operators_datetimelike_invalid(self, all_arithmetic_operators):
831+
# these are all TypeEror ops
832+
op_str = all_arithmetic_operators
833+
834+
def check(get_ser, test_ser):
832835

833836
# check that we are getting a TypeError
834837
# with 'operate' (from core/ops.py) for the ops that are not
835838
# defined
836-
for op_str in ops:
837-
op = getattr(get_ser, op_str, None)
838-
with tm.assert_raises_regex(TypeError, 'operate|cannot'):
839-
op(test_ser)
839+
op = getattr(get_ser, op_str, None)
840+
with tm.assert_raises_regex(TypeError, 'operate|cannot'):
841+
op(test_ser)
842+
843+
# ## timedelta64 ###
844+
td1 = Series([timedelta(minutes=5, seconds=3)] * 3)
845+
td1.iloc[2] = np.nan
846+
847+
# ## datetime64 ###
848+
dt1 = Series([Timestamp('20111230'), Timestamp('20120101'),
849+
Timestamp('20120103')])
850+
dt1.iloc[2] = np.nan
851+
dt2 = Series([Timestamp('20111231'), Timestamp('20120102'),
852+
Timestamp('20120104')])
853+
if op_str not in ['__sub__', '__rsub__']:
854+
check(dt1, dt2)
855+
856+
# ## datetime64 with timetimedelta ###
857+
# TODO(jreback) __rsub__ should raise?
858+
if op_str not in ['__add__', '__radd__', '__sub__']:
859+
check(dt1, td1)
860+
861+
# 8260, 10763
862+
# datetime64 with tz
863+
tz = 'US/Eastern'
864+
dt1 = Series(date_range('2000-01-01 09:00:00', periods=5,
865+
tz=tz), name='foo')
866+
dt2 = dt1.copy()
867+
dt2.iloc[2] = np.nan
868+
td1 = Series(timedelta_range('1 days 1 min', periods=5, freq='H'))
869+
td2 = td1.copy()
870+
td2.iloc[1] = np.nan
871+
872+
if op_str not in ['__add__', '__radd__', '__sub__', '__rsub__']:
873+
check(dt1, td1)
874+
875+
def test_operators_datetimelike(self):
840876

841877
# ## timedelta64 ###
842878
td1 = Series([timedelta(minutes=5, seconds=3)] * 3)
@@ -848,47 +884,31 @@ def run_ops(ops, get_ser, test_ser):
848884
dt1.iloc[2] = np.nan
849885
dt2 = Series([Timestamp('20111231'), Timestamp('20120102'),
850886
Timestamp('20120104')])
851-
ops = ['__add__', '__mul__', '__floordiv__', '__truediv__', '__div__',
852-
'__pow__', '__radd__', '__rmul__', '__rfloordiv__',
853-
'__rtruediv__', '__rdiv__', '__rpow__']
854-
run_ops(ops, dt1, dt2)
855887
dt1 - dt2
856888
dt2 - dt1
857889

858890
# ## datetime64 with timetimedelta ###
859-
ops = ['__mul__', '__floordiv__', '__truediv__', '__div__', '__pow__',
860-
'__rmul__', '__rfloordiv__', '__rtruediv__', '__rdiv__',
861-
'__rpow__']
862-
run_ops(ops, dt1, td1)
863891
dt1 + td1
864892
td1 + dt1
865893
dt1 - td1
866894
# TODO: Decide if this ought to work.
867895
# td1 - dt1
868896

869897
# ## timetimedelta with datetime64 ###
870-
ops = ['__sub__', '__mul__', '__floordiv__', '__truediv__', '__div__',
871-
'__pow__', '__rmul__', '__rfloordiv__', '__rtruediv__',
872-
'__rdiv__', '__rpow__']
873-
run_ops(ops, td1, dt1)
874898
td1 + dt1
875899
dt1 + td1
876900

877-
# 8260, 10763
878-
# datetime64 with tz
879-
ops = ['__mul__', '__floordiv__', '__truediv__', '__div__', '__pow__',
880-
'__rmul__', '__rfloordiv__', '__rtruediv__', '__rdiv__',
881-
'__rpow__']
901+
def test_operators_datetimelike_with_timezones(self):
882902

883903
tz = 'US/Eastern'
884904
dt1 = Series(date_range('2000-01-01 09:00:00', periods=5,
885905
tz=tz), name='foo')
886906
dt2 = dt1.copy()
887907
dt2.iloc[2] = np.nan
908+
888909
td1 = Series(timedelta_range('1 days 1 min', periods=5, freq='H'))
889910
td2 = td1.copy()
890911
td2.iloc[1] = np.nan
891-
run_ops(ops, dt1, td1)
892912

893913
result = dt1 + td1[0]
894914
exp = (dt1.dt.tz_localize(None) + td1[0]).dt.tz_localize(tz)
@@ -1133,25 +1153,23 @@ def test_dt64_series_arith_overflow(self):
11331153
res = dt - ser
11341154
tm.assert_series_equal(res, -expected)
11351155

1156+
@pytest.mark.parametrize('op', ['__add__', '__radd__',
1157+
'__sub__', '__rsub__'])
11361158
@pytest.mark.parametrize('tz', [None, 'Asia/Tokyo'])
1137-
def test_dt64_series_add_intlike(self, tz):
1159+
def test_dt64_series_add_intlike(self, tz, op):
11381160
# GH#19123
11391161
dti = pd.DatetimeIndex(['2016-01-02', '2016-02-03', 'NaT'], tz=tz)
11401162
ser = Series(dti)
11411163

11421164
other = Series([20, 30, 40], dtype='uint8')
11431165

1144-
pytest.raises(TypeError, ser.__add__, 1)
1145-
pytest.raises(TypeError, ser.__sub__, 1)
1166+
pytest.raises(TypeError, getattr(ser, op), 1)
11461167

1147-
pytest.raises(TypeError, ser.__add__, other)
1148-
pytest.raises(TypeError, ser.__sub__, other)
1168+
pytest.raises(TypeError, getattr(ser, op), other)
11491169

1150-
pytest.raises(TypeError, ser.__add__, other.values)
1151-
pytest.raises(TypeError, ser.__sub__, other.values)
1170+
pytest.raises(TypeError, getattr(ser, op), other.values)
11521171

1153-
pytest.raises(TypeError, ser.__add__, pd.Index(other))
1154-
pytest.raises(TypeError, ser.__sub__, pd.Index(other))
1172+
pytest.raises(TypeError, getattr(ser, op), pd.Index(other))
11551173

11561174

11571175
class TestSeriesOperators(TestData):

0 commit comments

Comments
 (0)