test_combine.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import numpy as np
  2. import pytest
  3. import pandas as pd
  4. import pandas._testing as tm
  5. class TestCombine:
  6. @pytest.mark.parametrize(
  7. "data",
  8. [
  9. pd.date_range("2000", periods=4),
  10. pd.date_range("2000", periods=4, tz="US/Central"),
  11. pd.period_range("2000", periods=4),
  12. pd.timedelta_range(0, periods=4),
  13. ],
  14. )
  15. def test_combine_datetlike_udf(self, data):
  16. # GH#23079
  17. df = pd.DataFrame({"A": data})
  18. other = df.copy()
  19. df.iloc[1, 0] = None
  20. def combiner(a, b):
  21. return b
  22. result = df.combine(other, combiner)
  23. tm.assert_frame_equal(result, other)
  24. def test_combine_generic(self, float_frame):
  25. df1 = float_frame
  26. df2 = float_frame.loc[float_frame.index[:-5], ["A", "B", "C"]]
  27. combined = df1.combine(df2, np.add)
  28. combined2 = df2.combine(df1, np.add)
  29. assert combined["D"].isna().all()
  30. assert combined2["D"].isna().all()
  31. chunk = combined.loc[combined.index[:-5], ["A", "B", "C"]]
  32. chunk2 = combined2.loc[combined2.index[:-5], ["A", "B", "C"]]
  33. exp = (
  34. float_frame.loc[float_frame.index[:-5], ["A", "B", "C"]].reindex_like(chunk)
  35. * 2
  36. )
  37. tm.assert_frame_equal(chunk, exp)
  38. tm.assert_frame_equal(chunk2, exp)