test_transpose.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import numpy as np
  2. import pytest
  3. from pandas import (
  4. CategoricalDtype,
  5. DataFrame,
  6. )
  7. import pandas._testing as tm
  8. def test_transpose(index_or_series_obj):
  9. obj = index_or_series_obj
  10. tm.assert_equal(obj.transpose(), obj)
  11. def test_transpose_non_default_axes(index_or_series_obj):
  12. msg = "the 'axes' parameter is not supported"
  13. obj = index_or_series_obj
  14. with pytest.raises(ValueError, match=msg):
  15. obj.transpose(1)
  16. with pytest.raises(ValueError, match=msg):
  17. obj.transpose(axes=1)
  18. def test_numpy_transpose(index_or_series_obj):
  19. msg = "the 'axes' parameter is not supported"
  20. obj = index_or_series_obj
  21. tm.assert_equal(np.transpose(obj), obj)
  22. with pytest.raises(ValueError, match=msg):
  23. np.transpose(obj, axes=1)
  24. @pytest.mark.parametrize(
  25. "data, transposed_data, index, columns, dtype",
  26. [
  27. ([[1], [2]], [[1, 2]], ["a", "a"], ["b"], int),
  28. ([[1], [2]], [[1, 2]], ["a", "a"], ["b"], CategoricalDtype([1, 2])),
  29. ([[1, 2]], [[1], [2]], ["b"], ["a", "a"], int),
  30. ([[1, 2]], [[1], [2]], ["b"], ["a", "a"], CategoricalDtype([1, 2])),
  31. ([[1, 2], [3, 4]], [[1, 3], [2, 4]], ["a", "a"], ["b", "b"], int),
  32. (
  33. [[1, 2], [3, 4]],
  34. [[1, 3], [2, 4]],
  35. ["a", "a"],
  36. ["b", "b"],
  37. CategoricalDtype([1, 2, 3, 4]),
  38. ),
  39. ],
  40. )
  41. def test_duplicate_labels(data, transposed_data, index, columns, dtype):
  42. # GH 42380
  43. df = DataFrame(data, index=index, columns=columns, dtype=dtype)
  44. result = df.T
  45. expected = DataFrame(transposed_data, index=columns, columns=index, dtype=dtype)
  46. tm.assert_frame_equal(result, expected)