Skip to content

Commit 3001af2

Browse files
y-pChang She
y-p
authored and
Chang She
committed
TST: getting column from and applying op to a df should commute
analogue to test_panel.test_arith which does the same for panel/frame as this does for frame/series.
1 parent 4dae1b5 commit 3001af2

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

pandas/tests/test_frame.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3114,6 +3114,39 @@ def test_arith_mixed(self):
31143114
'B': [2, 4, 6]})
31153115
assert_frame_equal(result, expected)
31163116

3117+
3118+
def test_arith_getitem_commute(self):
3119+
df = DataFrame({'A' : [1.1,3.3],'B' : [2.5,-3.9]})
3120+
3121+
self._test_op(df, operator.add)
3122+
self._test_op(df, operator.sub)
3123+
self._test_op(df, operator.mul)
3124+
self._test_op(df, operator.truediv)
3125+
self._test_op(df, operator.floordiv)
3126+
self._test_op(df, operator.pow)
3127+
3128+
self._test_op(df, lambda x, y: y + x)
3129+
self._test_op(df, lambda x, y: y - x)
3130+
self._test_op(df, lambda x, y: y * x)
3131+
self._test_op(df, lambda x, y: y / x)
3132+
self._test_op(df, lambda x, y: y ** x)
3133+
3134+
self._test_op(df, lambda x, y: x + y)
3135+
self._test_op(df, lambda x, y: x - y)
3136+
self._test_op(df, lambda x, y: x * y)
3137+
self._test_op(df, lambda x, y: x / y)
3138+
self._test_op(df, lambda x, y: x ** y)
3139+
3140+
@staticmethod
3141+
def _test_op(df, op):
3142+
result = op(df, 1)
3143+
3144+
if not df.columns.is_unique:
3145+
raise ValueError("Only unique columns supported by this test")
3146+
3147+
for col in result.columns:
3148+
assert_series_equal(result[col], op(df[col], 1))
3149+
31173150
def test_bool_flex_frame(self):
31183151
data = np.random.randn(5, 3)
31193152
other_data = np.random.randn(5, 3)

0 commit comments

Comments
 (0)