Skip to content

Commit 968d0ae

Browse files
committed
API: catch target kwarg clobbering
1 parent ce04b5b commit 968d0ae

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

pandas/core/generic.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,9 @@ def sample(self, n=None, frac=None, replace=False, weights=None, random_state=No
20892089
def pipe(self, func, *args, **kwargs):
20902090
if isinstance(func, tuple):
20912091
func, target = func
2092+
if target in kwargs:
2093+
msg = '%s is both the pipe target and a keyword argument' % target
2094+
raise ValueError(msg)
20922095
kwargs[target] = self
20932096
return func(*args, **kwargs)
20942097
else:

pandas/tests/test_generic.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,15 @@ def test_pipe_tuple(self):
16681668
result = df.A.pipe((f, 'y'), 0)
16691669
self.assert_series_equal(result, df.A)
16701670

1671+
def test_pipe_tuple_error(self):
1672+
df = DataFrame({"A": [1, 2, 3]})
1673+
f = lambda x, y: y
1674+
with tm.assertRaises(ValueError):
1675+
result = df.pipe((f, 'y'), x=1, y=0)
1676+
1677+
with tm.assertRaises(ValueError):
1678+
result = df.A.pipe((f, 'y'), x=1, y=0)
1679+
16711680
if __name__ == '__main__':
16721681
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
16731682
exit=False)

0 commit comments

Comments
 (0)