Skip to content

Commit 6c8b21b

Browse files
sinhrksnateGeorge
authored andcommitted
CLN: remove radd workaround in ops.py
Remove workaround added in pandas-dev#353. Author: sinhrks <[email protected]> Closes pandas-dev#13606 from sinhrks/ops_radd_cln and squashes the following commits: d873aad [sinhrks] CLN: remove radd workaround
1 parent a2e5d54 commit 6c8b21b

File tree

3 files changed

+80
-34
lines changed

3 files changed

+80
-34
lines changed

pandas/core/ops.py

+7-29
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
# methods
3535

3636

37-
def _create_methods(arith_method, radd_func, comp_method, bool_method,
37+
def _create_methods(arith_method, comp_method, bool_method,
3838
use_numexpr, special=False, default_axis='columns'):
3939
# creates actual methods based upon arithmetic, comp and bool method
4040
# constructors.
@@ -55,14 +55,14 @@ def names(x):
5555
return "__%s__" % x
5656
else:
5757
names = lambda x: x
58-
radd_func = radd_func or operator.add
58+
5959
# Inframe, all special methods have default_axis=None, flex methods have
6060
# default_axis set to the default (columns)
6161
# yapf: disable
6262
new_methods = dict(
6363
add=arith_method(operator.add, names('add'), op('+'),
6464
default_axis=default_axis),
65-
radd=arith_method(radd_func, names('radd'), op('+'),
65+
radd=arith_method(lambda x, y: y + x, names('radd'), op('+'),
6666
default_axis=default_axis),
6767
sub=arith_method(operator.sub, names('sub'), op('-'),
6868
default_axis=default_axis),
@@ -149,7 +149,7 @@ def add_methods(cls, new_methods, force, select, exclude):
149149

150150
# ----------------------------------------------------------------------
151151
# Arithmetic
152-
def add_special_arithmetic_methods(cls, arith_method=None, radd_func=None,
152+
def add_special_arithmetic_methods(cls, arith_method=None,
153153
comp_method=None, bool_method=None,
154154
use_numexpr=True, force=False, select=None,
155155
exclude=None):
@@ -162,8 +162,6 @@ def add_special_arithmetic_methods(cls, arith_method=None, radd_func=None,
162162
arith_method : function (optional)
163163
factory for special arithmetic methods, with op string:
164164
f(op, name, str_rep, default_axis=None, fill_zeros=None, **eval_kwargs)
165-
radd_func : function (optional)
166-
Possible replacement for ``operator.add`` for compatibility
167165
comp_method : function, optional,
168166
factory for rich comparison - signature: f(op, name, str_rep)
169167
use_numexpr : bool, default True
@@ -176,12 +174,11 @@ def add_special_arithmetic_methods(cls, arith_method=None, radd_func=None,
176174
exclude : iterable of strings (optional)
177175
if passed, will not set functions with names in exclude
178176
"""
179-
radd_func = radd_func or operator.add
180177

181178
# in frame, special methods have default_axis = None, comp methods use
182179
# 'columns'
183180

184-
new_methods = _create_methods(arith_method, radd_func, comp_method,
181+
new_methods = _create_methods(arith_method, comp_method,
185182
bool_method, use_numexpr, default_axis=None,
186183
special=True)
187184

@@ -218,7 +215,7 @@ def f(self, other):
218215
exclude=exclude)
219216

220217

221-
def add_flex_arithmetic_methods(cls, flex_arith_method, radd_func=None,
218+
def add_flex_arithmetic_methods(cls, flex_arith_method,
222219
flex_comp_method=None, flex_bool_method=None,
223220
use_numexpr=True, force=False, select=None,
224221
exclude=None):
@@ -231,9 +228,6 @@ def add_flex_arithmetic_methods(cls, flex_arith_method, radd_func=None,
231228
flex_arith_method : function
232229
factory for special arithmetic methods, with op string:
233230
f(op, name, str_rep, default_axis=None, fill_zeros=None, **eval_kwargs)
234-
radd_func : function (optional)
235-
Possible replacement for ``lambda x, y: operator.add(y, x)`` for
236-
compatibility
237231
flex_comp_method : function, optional,
238232
factory for rich comparison - signature: f(op, name, str_rep)
239233
use_numexpr : bool, default True
@@ -246,9 +240,8 @@ def add_flex_arithmetic_methods(cls, flex_arith_method, radd_func=None,
246240
exclude : iterable of strings (optional)
247241
if passed, will not set functions with names in exclude
248242
"""
249-
radd_func = radd_func or (lambda x, y: operator.add(y, x))
250243
# in frame, default axis is 'columns', doesn't matter for series and panel
251-
new_methods = _create_methods(flex_arith_method, radd_func,
244+
new_methods = _create_methods(flex_arith_method,
252245
flex_comp_method, flex_bool_method,
253246
use_numexpr, default_axis='columns',
254247
special=False)
@@ -858,17 +851,6 @@ def wrapper(self, other):
858851
return wrapper
859852

860853

861-
def _radd_compat(left, right):
862-
radd = lambda x, y: y + x
863-
# GH #353, NumPy 1.5.1 workaround
864-
try:
865-
output = radd(left, right)
866-
except TypeError:
867-
raise
868-
869-
return output
870-
871-
872854
_op_descriptions = {'add': {'op': '+',
873855
'desc': 'Addition',
874856
'reversed': False,
@@ -963,11 +945,9 @@ def flex_wrapper(self, other, level=None, fill_value=None, axis=0):
963945

964946

965947
series_flex_funcs = dict(flex_arith_method=_flex_method_SERIES,
966-
radd_func=_radd_compat,
967948
flex_comp_method=_comp_method_SERIES)
968949

969950
series_special_funcs = dict(arith_method=_arith_method_SERIES,
970-
radd_func=_radd_compat,
971951
comp_method=_comp_method_SERIES,
972952
bool_method=_bool_method_SERIES)
973953

@@ -1209,11 +1189,9 @@ def f(self, other):
12091189

12101190

12111191
frame_flex_funcs = dict(flex_arith_method=_arith_method_FRAME,
1212-
radd_func=_radd_compat,
12131192
flex_comp_method=_flex_comp_method_FRAME)
12141193

12151194
frame_special_funcs = dict(arith_method=_arith_method_FRAME,
1216-
radd_func=_radd_compat,
12171195
comp_method=_comp_method_FRAME,
12181196
bool_method=_arith_method_FRAME)
12191197

pandas/sparse/series.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import numpy as np
99
import warnings
10-
import operator
1110

1211
from pandas.compat.numpy import function as nv
1312
from pandas.core.common import isnull, _values_from_object, _maybe_match_name
@@ -803,7 +802,7 @@ def from_coo(cls, A, dense_index=False):
803802
# overwrite basic arithmetic to use SparseSeries version
804803
# force methods to overwrite previous definitions.
805804
ops.add_special_arithmetic_methods(SparseSeries, _arith_method,
806-
radd_func=operator.add, comp_method=None,
805+
comp_method=None,
807806
bool_method=None, use_numexpr=False,
808807
force=True)
809808

pandas/tests/series/test_operators.py

+72-3
Original file line numberDiff line numberDiff line change
@@ -1259,8 +1259,6 @@ def _check_op(arr, op):
12591259
_check_op(arr, operator.floordiv)
12601260

12611261
def test_series_frame_radd_bug(self):
1262-
import operator
1263-
12641262
# GH 353
12651263
vals = Series(tm.rands_array(5, 10))
12661264
result = 'foo_' + vals
@@ -1273,7 +1271,78 @@ def test_series_frame_radd_bug(self):
12731271
tm.assert_frame_equal(result, expected)
12741272

12751273
# really raise this time
1276-
self.assertRaises(TypeError, operator.add, datetime.now(), self.ts)
1274+
with tm.assertRaises(TypeError):
1275+
datetime.now() + self.ts
1276+
1277+
with tm.assertRaises(TypeError):
1278+
self.ts + datetime.now()
1279+
1280+
def test_series_radd_more(self):
1281+
data = [[1, 2, 3],
1282+
[1.1, 2.2, 3.3],
1283+
[pd.Timestamp('2011-01-01'), pd.Timestamp('2011-01-02'),
1284+
pd.NaT],
1285+
['x', 'y', 1]]
1286+
1287+
for d in data:
1288+
for dtype in [None, object]:
1289+
s = Series(d, dtype=dtype)
1290+
with tm.assertRaises(TypeError):
1291+
'foo_' + s
1292+
1293+
for dtype in [None, object]:
1294+
res = 1 + pd.Series([1, 2, 3], dtype=dtype)
1295+
exp = pd.Series([2, 3, 4], dtype=dtype)
1296+
tm.assert_series_equal(res, exp)
1297+
res = pd.Series([1, 2, 3], dtype=dtype) + 1
1298+
tm.assert_series_equal(res, exp)
1299+
1300+
res = np.nan + pd.Series([1, 2, 3], dtype=dtype)
1301+
exp = pd.Series([np.nan, np.nan, np.nan], dtype=dtype)
1302+
tm.assert_series_equal(res, exp)
1303+
res = pd.Series([1, 2, 3], dtype=dtype) + np.nan
1304+
tm.assert_series_equal(res, exp)
1305+
1306+
s = pd.Series([pd.Timedelta('1 days'), pd.Timedelta('2 days'),
1307+
pd.Timedelta('3 days')], dtype=dtype)
1308+
exp = pd.Series([pd.Timedelta('4 days'), pd.Timedelta('5 days'),
1309+
pd.Timedelta('6 days')])
1310+
tm.assert_series_equal(pd.Timedelta('3 days') + s, exp)
1311+
tm.assert_series_equal(s + pd.Timedelta('3 days'), exp)
1312+
1313+
s = pd.Series(['x', np.nan, 'x'])
1314+
tm.assert_series_equal('a' + s, pd.Series(['ax', np.nan, 'ax']))
1315+
tm.assert_series_equal(s + 'a', pd.Series(['xa', np.nan, 'xa']))
1316+
1317+
def test_frame_radd_more(self):
1318+
data = [[1, 2, 3],
1319+
[1.1, 2.2, 3.3],
1320+
[pd.Timestamp('2011-01-01'), pd.Timestamp('2011-01-02'),
1321+
pd.NaT],
1322+
['x', 'y', 1]]
1323+
1324+
for d in data:
1325+
for dtype in [None, object]:
1326+
s = DataFrame(d, dtype=dtype)
1327+
with tm.assertRaises(TypeError):
1328+
'foo_' + s
1329+
1330+
for dtype in [None, object]:
1331+
res = 1 + pd.DataFrame([1, 2, 3], dtype=dtype)
1332+
exp = pd.DataFrame([2, 3, 4], dtype=dtype)
1333+
tm.assert_frame_equal(res, exp)
1334+
res = pd.DataFrame([1, 2, 3], dtype=dtype) + 1
1335+
tm.assert_frame_equal(res, exp)
1336+
1337+
res = np.nan + pd.DataFrame([1, 2, 3], dtype=dtype)
1338+
exp = pd.DataFrame([np.nan, np.nan, np.nan], dtype=dtype)
1339+
tm.assert_frame_equal(res, exp)
1340+
res = pd.DataFrame([1, 2, 3], dtype=dtype) + np.nan
1341+
tm.assert_frame_equal(res, exp)
1342+
1343+
df = pd.DataFrame(['x', np.nan, 'x'])
1344+
tm.assert_frame_equal('a' + df, pd.DataFrame(['ax', np.nan, 'ax']))
1345+
tm.assert_frame_equal(df + 'a', pd.DataFrame(['xa', np.nan, 'xa']))
12771346

12781347
def test_operators_frame(self):
12791348
# rpow does not work with DataFrame

0 commit comments

Comments
 (0)