Skip to content

Commit f458b65

Browse files
committed
BUG: also fix DataFrame.__radd__, GH #353
1 parent b5e7ade commit f458b65

File tree

2 files changed

+41
-35
lines changed

2 files changed

+41
-35
lines changed

pandas/core/frame.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,10 @@ def __contains__(self, key):
332332
sub = _arith_method(operator.sub, 'subtract')
333333
div = _arith_method(lambda x, y: x / y, 'divide')
334334

335-
radd = _arith_method(operator.add, 'add')
336-
rmul = _arith_method(operator.mul, 'multiply')
337-
rsub = _arith_method(lambda x, y: y - x, 'subtract')
338-
rdiv = _arith_method(lambda x, y: y / x, 'divide')
335+
radd = _arith_method(lambda x, y: y + x, 'radd')
336+
rmul = _arith_method(operator.mul, 'rmultiply')
337+
rsub = _arith_method(lambda x, y: y - x, 'rsubtract')
338+
rdiv = _arith_method(lambda x, y: y / x, 'rdivide')
339339

340340
__add__ = _arith_method(operator.add, '__add__', default_axis=None)
341341
__sub__ = _arith_method(operator.sub, '__sub__', default_axis=None)
@@ -346,7 +346,7 @@ def __contains__(self, key):
346346
default_axis=None)
347347
__pow__ = _arith_method(operator.pow, '__pow__', default_axis=None)
348348

349-
__radd__ = _arith_method(operator.add, '__radd__', default_axis=None)
349+
__radd__ = _arith_method(lambda x, y: y + x, '__radd__', default_axis=None)
350350
__rmul__ = _arith_method(operator.mul, '__rmul__', default_axis=None)
351351
__rsub__ = _arith_method(lambda x, y: y - x, '__rsub__', default_axis=None)
352352
__rtruediv__ = _arith_method(lambda x, y: y / x, '__rtruediv__',

pandas/tests/test_series.py

+36-30
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import pandas.core.datetools as datetools
1616
from pandas.util import py3compat
1717
from pandas.util.testing import assert_series_equal, assert_almost_equal
18-
import pandas.util.testing as common
18+
import pandas.util.testing as tm
1919

2020
#-------------------------------------------------------------------------------
2121
# Series test cases
@@ -138,13 +138,13 @@ def test_to_sparse_pass_name(self):
138138
class TestSeries(unittest.TestCase, CheckNameIntegration):
139139

140140
def setUp(self):
141-
self.ts = common.makeTimeSeries()
141+
self.ts = tm.makeTimeSeries()
142142
self.ts.name = 'ts'
143143

144-
self.series = common.makeStringSeries()
144+
self.series = tm.makeStringSeries()
145145
self.series.name = 'series'
146146

147-
self.objSeries = common.makeObjectSeries()
147+
self.objSeries = tm.makeObjectSeries()
148148
self.objSeries.name = 'objects'
149149

150150
self.empty = Series([], index=[])
@@ -157,7 +157,7 @@ def test_constructor(self):
157157
derived = Series(self.ts)
158158
self.assert_(isinstance(derived, TimeSeries))
159159

160-
self.assert_(common.equalContents(derived.index, self.ts.index))
160+
self.assert_(tm.equalContents(derived.index, self.ts.index))
161161
# Ensure new index is not created
162162
self.assertEquals(id(self.ts.index), id(derived.index))
163163

@@ -181,7 +181,7 @@ def test_constructor_default_index(self):
181181
assert_almost_equal(s.index, np.arange(3))
182182

183183
def test_constructor_corner(self):
184-
df = common.makeTimeDataFrame()
184+
df = tm.makeTimeDataFrame()
185185
objs = [df, df]
186186
s = Series(objs, index=[0, 1])
187187
self.assert_(isinstance(s, Series))
@@ -204,7 +204,7 @@ def test_fromDict(self):
204204
data = {'a' : 0, 'b' : 1, 'c' : 2, 'd' : 3}
205205

206206
series = Series(data)
207-
self.assert_(common.is_sorted(series.index))
207+
self.assert_(tm.is_sorted(series.index))
208208

209209
data = {'a' : 0, 'b' : '1', 'c' : '2', 'd' : datetime.now()}
210210
series = Series(data)
@@ -251,7 +251,7 @@ def test_fromValue(self):
251251
self.assertEqual(len(dates), len(self.ts))
252252

253253
def test_contains(self):
254-
common.assert_contains_all(self.ts.index, self.ts)
254+
tm.assert_contains_all(self.ts.index, self.ts)
255255

256256
def test_pickle(self):
257257
unp_series = self._pickle_roundtrip(self.series)
@@ -365,7 +365,7 @@ def test_slice(self):
365365

366366
self.assertEqual(numSlice.index[1], self.series.index[11])
367367

368-
self.assert_(common.equalContents(numSliceEnd,
368+
self.assert_(tm.equalContents(numSliceEnd,
369369
np.array(self.series)[-10:]))
370370

371371
# test return view
@@ -383,8 +383,8 @@ def test_setitem(self):
383383
self.assert_(not np.isnan(self.ts[2]))
384384

385385
# caught this bug when writing tests
386-
series = Series(common.makeIntIndex(20).astype(float),
387-
index=common.makeIntIndex(20))
386+
series = Series(tm.makeIntIndex(20).astype(float),
387+
index=tm.makeIntIndex(20))
388388

389389
series[::2] = 0
390390
self.assert_((series[::2] == 0).all())
@@ -469,7 +469,7 @@ def test_repr(self):
469469
str(self.series.astype(int))
470470
str(self.objSeries)
471471

472-
str(Series(common.randn(1000), index=np.arange(1000)))
472+
str(Series(tm.randn(1000), index=np.arange(1000)))
473473

474474
# empty
475475
str(self.empty)
@@ -647,7 +647,7 @@ def _check_op(other, op):
647647
cython_or_numpy = op(series, other)
648648
python = series.combine(other, op)
649649

650-
common.assert_almost_equal(cython_or_numpy, python)
650+
tm.assert_almost_equal(cython_or_numpy, python)
651651

652652
def check(other):
653653
_check_op(other, operator.add)
@@ -706,8 +706,6 @@ def test_operators_corner(self):
706706
self.assert_(np.array_equal(added[:-5], expected))
707707

708708
def test_operators_reverse_object(self):
709-
from pandas.util.testing import rands
710-
711709
# GH 56
712710
arr = Series(np.random.randn(10), index=np.arange(10),
713711
dtype=object)
@@ -723,18 +721,26 @@ def _check_op(arr, op):
723721
_check_op(arr, operator.truediv)
724722
_check_op(arr, operator.floordiv)
725723

724+
def test_series_frame_radd_bug(self):
725+
from pandas.util.testing import rands
726+
726727
# GH 353
727728
vals = Series([rands(5) for _ in xrange(10)])
728729
result = 'foo_' + vals
729730
expected = vals.map(lambda x: 'foo_' + x)
730731
assert_series_equal(result, expected)
731732

733+
frame = DataFrame({'vals' : vals})
734+
result = 'foo_' + frame
735+
expected = DataFrame({'vals' : vals.map(lambda x: 'foo_' + x)})
736+
tm.assert_frame_equal(result, expected)
737+
732738
def test_operators_frame(self):
733739
# rpow does not work with DataFrame
734740
df = DataFrame({'A' : self.ts})
735741

736-
common.assert_almost_equal(self.ts + self.ts, (self.ts + df)['A'])
737-
common.assert_almost_equal(self.ts ** self.ts, (self.ts ** df)['A'])
742+
tm.assert_almost_equal(self.ts + self.ts, (self.ts + df)['A'])
743+
tm.assert_almost_equal(self.ts ** self.ts, (self.ts ** df)['A'])
738744

739745
def test_operators_combine(self):
740746
def _check_fill(meth, op, a, b, fill_value=0):
@@ -782,8 +788,8 @@ def _check_fill(meth, op, a, b, fill_value=0):
782788
_check_fill(op, equiv_op, a, b, fill_value=fv)
783789

784790
def test_combine_first(self):
785-
values = common.makeIntIndex(20).values.astype(float)
786-
series = Series(values, index=common.makeIntIndex(20))
791+
values = tm.makeIntIndex(20).values.astype(float)
792+
series = Series(values, index=tm.makeIntIndex(20))
787793

788794
series_copy = series * 2
789795
series_copy[::2] = np.NaN
@@ -801,14 +807,14 @@ def test_combine_first(self):
801807
self.assert_(np.array_equal(combined[1::2], series_copy[1::2]))
802808

803809
# mixed types
804-
index = common.makeStringIndex(20)
805-
floats = Series(common.randn(20), index=index)
806-
strings = Series(common.makeStringIndex(10), index=index[::2])
810+
index = tm.makeStringIndex(20)
811+
floats = Series(tm.randn(20), index=index)
812+
strings = Series(tm.makeStringIndex(10), index=index[::2])
807813

808814
combined = strings.combine_first(floats)
809815

810-
common.assert_dict_equal(strings, combined, compare_keys=False)
811-
common.assert_dict_equal(floats[1::2], combined, compare_keys=False)
816+
tm.assert_dict_equal(strings, combined, compare_keys=False)
817+
tm.assert_dict_equal(floats[1::2], combined, compare_keys=False)
812818

813819
# corner case
814820
s = Series([1., 2, 3], index=[0, 1, 2])
@@ -939,7 +945,7 @@ def test_valid(self):
939945
result = ts.valid()
940946
self.assertEqual(len(result), ts.count())
941947

942-
common.assert_dict_equal(result, ts, compare_keys=False)
948+
tm.assert_dict_equal(result, ts, compare_keys=False)
943949

944950
def test_isnull(self):
945951
ser = Series([0,5.4,3,nan,-0.001])
@@ -957,7 +963,7 @@ def test_shift(self):
957963
shifted = self.ts.shift(1)
958964
unshifted = shifted.shift(-1)
959965

960-
common.assert_dict_equal(unshifted.valid(), self.ts, compare_keys=False)
966+
tm.assert_dict_equal(unshifted.valid(), self.ts, compare_keys=False)
961967

962968
offset = datetools.bday
963969
shifted = self.ts.shift(1, offset=offset)
@@ -1055,7 +1061,7 @@ def test_asof(self):
10551061
self.assert_(np.isnan(self.ts.asof(d)))
10561062

10571063
def test_map(self):
1058-
index, data = common.getMixedTypeDict()
1064+
index, data = tm.getMixedTypeDict()
10591065

10601066
source = Series(data['B'], index=data['C'])
10611067
target = Series(data['C'][:4], index=data['D'][:4])
@@ -1262,8 +1268,8 @@ def test_preserveRefs(self):
12621268
def test_ne(self):
12631269
ts = TimeSeries([3, 4, 5, 6, 7], [3, 4, 5, 6, 7], dtype=float)
12641270
expected = [True, True, False, True, True]
1265-
self.assert_(common.equalContents(ts.index != 5, expected))
1266-
self.assert_(common.equalContents(~(ts.index == 5), expected))
1271+
self.assert_(tm.equalContents(ts.index != 5, expected))
1272+
self.assert_(tm.equalContents(~(ts.index == 5), expected))
12671273

12681274
def test_pad_nan(self):
12691275
x = TimeSeries([np.nan, 1., np.nan, 3., np.nan],
@@ -1320,7 +1326,7 @@ def test_isin(self):
13201326
# TimeSeries-specific
13211327

13221328
def test_fillna(self):
1323-
ts = Series([0., 1., 2., 3., 4.], index=common.makeDateIndex(5))
1329+
ts = Series([0., 1., 2., 3., 4.], index=tm.makeDateIndex(5))
13241330

13251331
self.assert_(np.array_equal(ts, ts.fillna()))
13261332

0 commit comments

Comments
 (0)