test_iat.py 1.5 KB

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