test_isetitem.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import pytest
  2. from pandas import (
  3. DataFrame,
  4. Series,
  5. )
  6. import pandas._testing as tm
  7. class TestDataFrameSetItem:
  8. def test_isetitem_ea_df(self):
  9. # GH#49922
  10. df = DataFrame([[1, 2, 3], [4, 5, 6]])
  11. rhs = DataFrame([[11, 12], [13, 14]], dtype="Int64")
  12. df.isetitem([0, 1], rhs)
  13. expected = DataFrame(
  14. {
  15. 0: Series([11, 13], dtype="Int64"),
  16. 1: Series([12, 14], dtype="Int64"),
  17. 2: [3, 6],
  18. }
  19. )
  20. tm.assert_frame_equal(df, expected)
  21. def test_isetitem_ea_df_scalar_indexer(self):
  22. # GH#49922
  23. df = DataFrame([[1, 2, 3], [4, 5, 6]])
  24. rhs = DataFrame([[11], [13]], dtype="Int64")
  25. df.isetitem(2, rhs)
  26. expected = DataFrame(
  27. {
  28. 0: [1, 4],
  29. 1: [2, 5],
  30. 2: Series([11, 13], dtype="Int64"),
  31. }
  32. )
  33. tm.assert_frame_equal(df, expected)
  34. def test_isetitem_dimension_mismatch(self):
  35. # GH#51701
  36. df = DataFrame({"a": [1, 2], "b": [3, 4], "c": [5, 6]})
  37. value = df.copy()
  38. with pytest.raises(ValueError, match="Got 2 positions but value has 3 columns"):
  39. df.isetitem([1, 2], value)
  40. value = df.copy()
  41. with pytest.raises(ValueError, match="Got 2 positions but value has 1 columns"):
  42. df.isetitem([1, 2], value[["a"]])