test_iat.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import numpy as np
  2. from pandas import (
  3. DataFrame,
  4. Series,
  5. period_range,
  6. )
  7. def test_iat(float_frame):
  8. for i, row in enumerate(float_frame.index):
  9. for j, col in enumerate(float_frame.columns):
  10. result = float_frame.iat[i, j]
  11. expected = float_frame.at[row, col]
  12. assert result == expected
  13. def test_iat_duplicate_columns():
  14. # https://github.com/pandas-dev/pandas/issues/11754
  15. df = DataFrame([[1, 2]], columns=["x", "x"])
  16. assert df.iat[0, 0] == 1
  17. def test_iat_getitem_series_with_period_index():
  18. # GH#4390, iat incorrectly indexing
  19. index = period_range("1/1/2001", periods=10)
  20. ser = Series(np.random.randn(10), index=index)
  21. expected = ser[index[0]]
  22. result = ser.iat[0]
  23. assert expected == result
  24. def test_iat_setitem_item_cache_cleared(indexer_ial, using_copy_on_write):
  25. # GH#45684
  26. data = {"x": np.arange(8, dtype=np.int64), "y": np.int64(0)}
  27. df = DataFrame(data).copy()
  28. ser = df["y"]
  29. # previously this iat setting would split the block and fail to clear
  30. # the item_cache.
  31. indexer_ial(df)[7, 0] = 9999
  32. indexer_ial(df)[7, 1] = 1234
  33. assert df.iat[7, 1] == 1234
  34. if not using_copy_on_write:
  35. assert ser.iloc[-1] == 1234
  36. assert df.iloc[-1, -1] == 1234