test_add_prefix_suffix.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import pytest
  2. from pandas import Index
  3. import pandas._testing as tm
  4. def test_add_prefix_suffix(float_frame):
  5. with_prefix = float_frame.add_prefix("foo#")
  6. expected = Index([f"foo#{c}" for c in float_frame.columns])
  7. tm.assert_index_equal(with_prefix.columns, expected)
  8. with_suffix = float_frame.add_suffix("#foo")
  9. expected = Index([f"{c}#foo" for c in float_frame.columns])
  10. tm.assert_index_equal(with_suffix.columns, expected)
  11. with_pct_prefix = float_frame.add_prefix("%")
  12. expected = Index([f"%{c}" for c in float_frame.columns])
  13. tm.assert_index_equal(with_pct_prefix.columns, expected)
  14. with_pct_suffix = float_frame.add_suffix("%")
  15. expected = Index([f"{c}%" for c in float_frame.columns])
  16. tm.assert_index_equal(with_pct_suffix.columns, expected)
  17. def test_add_prefix_suffix_axis(float_frame):
  18. # GH 47819
  19. with_prefix = float_frame.add_prefix("foo#", axis=0)
  20. expected = Index([f"foo#{c}" for c in float_frame.index])
  21. tm.assert_index_equal(with_prefix.index, expected)
  22. with_prefix = float_frame.add_prefix("foo#", axis=1)
  23. expected = Index([f"foo#{c}" for c in float_frame.columns])
  24. tm.assert_index_equal(with_prefix.columns, expected)
  25. with_pct_suffix = float_frame.add_suffix("#foo", axis=0)
  26. expected = Index([f"{c}#foo" for c in float_frame.index])
  27. tm.assert_index_equal(with_pct_suffix.index, expected)
  28. with_pct_suffix = float_frame.add_suffix("#foo", axis=1)
  29. expected = Index([f"{c}#foo" for c in float_frame.columns])
  30. tm.assert_index_equal(with_pct_suffix.columns, expected)
  31. def test_add_prefix_suffix_invalid_axis(float_frame):
  32. with pytest.raises(ValueError, match="No axis named 2 for object type DataFrame"):
  33. float_frame.add_prefix("foo#", axis=2)
  34. with pytest.raises(ValueError, match="No axis named 2 for object type DataFrame"):
  35. float_frame.add_suffix("foo#", axis=2)