test_add_prefix_suffix.py 1.5 KB

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