test_reindex_like.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import numpy as np
  2. import pytest
  3. from pandas import DataFrame
  4. import pandas._testing as tm
  5. class TestDataFrameReindexLike:
  6. def test_reindex_like(self, float_frame):
  7. other = float_frame.reindex(index=float_frame.index[:10], columns=["C", "B"])
  8. tm.assert_frame_equal(other, float_frame.reindex_like(other))
  9. @pytest.mark.parametrize(
  10. "method,expected_values",
  11. [
  12. ("nearest", [0, 1, 1, 2]),
  13. ("pad", [np.nan, 0, 1, 1]),
  14. ("backfill", [0, 1, 2, 2]),
  15. ],
  16. )
  17. def test_reindex_like_methods(self, method, expected_values):
  18. df = DataFrame({"x": list(range(5))})
  19. result = df.reindex_like(df, method=method, tolerance=0)
  20. tm.assert_frame_equal(df, result)
  21. result = df.reindex_like(df, method=method, tolerance=[0, 0, 0, 0])
  22. tm.assert_frame_equal(df, result)
  23. def test_reindex_like_subclass(self):
  24. # https://github.com/pandas-dev/pandas/issues/31925
  25. class MyDataFrame(DataFrame):
  26. pass
  27. expected = DataFrame()
  28. df = MyDataFrame()
  29. result = df.reindex_like(expected)
  30. tm.assert_frame_equal(result, expected)