test_pipe.py 1023 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import pytest
  2. from pandas import (
  3. DataFrame,
  4. Series,
  5. )
  6. import pandas._testing as tm
  7. class TestPipe:
  8. def test_pipe(self, frame_or_series):
  9. obj = DataFrame({"A": [1, 2, 3]})
  10. expected = DataFrame({"A": [1, 4, 9]})
  11. if frame_or_series is Series:
  12. obj = obj["A"]
  13. expected = expected["A"]
  14. f = lambda x, y: x**y
  15. result = obj.pipe(f, 2)
  16. tm.assert_equal(result, expected)
  17. def test_pipe_tuple(self, frame_or_series):
  18. obj = DataFrame({"A": [1, 2, 3]})
  19. obj = tm.get_obj(obj, frame_or_series)
  20. f = lambda x, y: y
  21. result = obj.pipe((f, "y"), 0)
  22. tm.assert_equal(result, obj)
  23. def test_pipe_tuple_error(self, frame_or_series):
  24. obj = DataFrame({"A": [1, 2, 3]})
  25. obj = tm.get_obj(obj, frame_or_series)
  26. f = lambda x, y: y
  27. msg = "y is both the pipe target and a keyword argument"
  28. with pytest.raises(ValueError, match=msg):
  29. obj.pipe((f, "y"), x=1, y=0)