test_at.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. from datetime import (
  2. datetime,
  3. timezone,
  4. )
  5. import numpy as np
  6. import pytest
  7. from pandas.errors import InvalidIndexError
  8. from pandas import (
  9. CategoricalDtype,
  10. CategoricalIndex,
  11. DataFrame,
  12. DatetimeIndex,
  13. MultiIndex,
  14. Series,
  15. Timestamp,
  16. )
  17. import pandas._testing as tm
  18. def test_at_timezone():
  19. # https://github.com/pandas-dev/pandas/issues/33544
  20. result = DataFrame({"foo": [datetime(2000, 1, 1)]})
  21. result.at[0, "foo"] = datetime(2000, 1, 2, tzinfo=timezone.utc)
  22. expected = DataFrame(
  23. {"foo": [datetime(2000, 1, 2, tzinfo=timezone.utc)]}, dtype=object
  24. )
  25. tm.assert_frame_equal(result, expected)
  26. def test_selection_methods_of_assigned_col():
  27. # GH 29282
  28. df = DataFrame(data={"a": [1, 2, 3], "b": [4, 5, 6]})
  29. df2 = DataFrame(data={"c": [7, 8, 9]}, index=[2, 1, 0])
  30. df["c"] = df2["c"]
  31. df.at[1, "c"] = 11
  32. result = df
  33. expected = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [9, 11, 7]})
  34. tm.assert_frame_equal(result, expected)
  35. result = df.at[1, "c"]
  36. assert result == 11
  37. result = df["c"]
  38. expected = Series([9, 11, 7], name="c")
  39. tm.assert_series_equal(result, expected)
  40. result = df[["c"]]
  41. expected = DataFrame({"c": [9, 11, 7]})
  42. tm.assert_frame_equal(result, expected)
  43. class TestAtSetItem:
  44. def test_at_setitem_item_cache_cleared(self):
  45. # GH#22372 Note the multi-step construction is necessary to trigger
  46. # the original bug. pandas/issues/22372#issuecomment-413345309
  47. df = DataFrame(index=[0])
  48. df["x"] = 1
  49. df["cost"] = 2
  50. # accessing df["cost"] adds "cost" to the _item_cache
  51. df["cost"]
  52. # This loc[[0]] lookup used to call _consolidate_inplace at the
  53. # BlockManager level, which failed to clear the _item_cache
  54. df.loc[[0]]
  55. df.at[0, "x"] = 4
  56. df.at[0, "cost"] = 789
  57. expected = DataFrame({"x": [4], "cost": 789}, index=[0])
  58. tm.assert_frame_equal(df, expected)
  59. # And in particular, check that the _item_cache has updated correctly.
  60. tm.assert_series_equal(df["cost"], expected["cost"])
  61. def test_at_setitem_mixed_index_assignment(self):
  62. # GH#19860
  63. ser = Series([1, 2, 3, 4, 5], index=["a", "b", "c", 1, 2])
  64. ser.at["a"] = 11
  65. assert ser.iat[0] == 11
  66. ser.at[1] = 22
  67. assert ser.iat[3] == 22
  68. def test_at_setitem_categorical_missing(self):
  69. df = DataFrame(
  70. index=range(3), columns=range(3), dtype=CategoricalDtype(["foo", "bar"])
  71. )
  72. df.at[1, 1] = "foo"
  73. expected = DataFrame(
  74. [
  75. [np.nan, np.nan, np.nan],
  76. [np.nan, "foo", np.nan],
  77. [np.nan, np.nan, np.nan],
  78. ],
  79. dtype=CategoricalDtype(["foo", "bar"]),
  80. )
  81. tm.assert_frame_equal(df, expected)
  82. def test_at_setitem_multiindex(self):
  83. df = DataFrame(
  84. np.zeros((3, 2), dtype="int64"),
  85. columns=MultiIndex.from_tuples([("a", 0), ("a", 1)]),
  86. )
  87. df.at[0, "a"] = 10
  88. expected = DataFrame(
  89. [[10, 10], [0, 0], [0, 0]],
  90. columns=MultiIndex.from_tuples([("a", 0), ("a", 1)]),
  91. )
  92. tm.assert_frame_equal(df, expected)
  93. @pytest.mark.parametrize("row", (Timestamp("2019-01-01"), "2019-01-01"))
  94. def test_at_datetime_index(self, row):
  95. df = DataFrame(
  96. data=[[1] * 2], index=DatetimeIndex(data=["2019-01-01", "2019-01-02"])
  97. )
  98. expected = DataFrame(
  99. data=[[0.5, 1], [1.0, 1]],
  100. index=DatetimeIndex(data=["2019-01-01", "2019-01-02"]),
  101. )
  102. df.at[row, 0] = 0.5
  103. tm.assert_frame_equal(df, expected)
  104. class TestAtSetItemWithExpansion:
  105. def test_at_setitem_expansion_series_dt64tz_value(self, tz_naive_fixture):
  106. # GH#25506
  107. ts = Timestamp("2017-08-05 00:00:00+0100", tz=tz_naive_fixture)
  108. result = Series(ts)
  109. result.at[1] = ts
  110. expected = Series([ts, ts])
  111. tm.assert_series_equal(result, expected)
  112. class TestAtWithDuplicates:
  113. def test_at_with_duplicate_axes_requires_scalar_lookup(self):
  114. # GH#33041 check that falling back to loc doesn't allow non-scalar
  115. # args to slip in
  116. arr = np.random.randn(6).reshape(3, 2)
  117. df = DataFrame(arr, columns=["A", "A"])
  118. msg = "Invalid call for scalar access"
  119. with pytest.raises(ValueError, match=msg):
  120. df.at[[1, 2]]
  121. with pytest.raises(ValueError, match=msg):
  122. df.at[1, ["A"]]
  123. with pytest.raises(ValueError, match=msg):
  124. df.at[:, "A"]
  125. with pytest.raises(ValueError, match=msg):
  126. df.at[[1, 2]] = 1
  127. with pytest.raises(ValueError, match=msg):
  128. df.at[1, ["A"]] = 1
  129. with pytest.raises(ValueError, match=msg):
  130. df.at[:, "A"] = 1
  131. class TestAtErrors:
  132. # TODO: De-duplicate/parametrize
  133. # test_at_series_raises_key_error2, test_at_frame_raises_key_error2
  134. def test_at_series_raises_key_error(self, indexer_al):
  135. # GH#31724 .at should match .loc
  136. ser = Series([1, 2, 3], index=[3, 2, 1])
  137. result = indexer_al(ser)[1]
  138. assert result == 3
  139. with pytest.raises(KeyError, match="a"):
  140. indexer_al(ser)["a"]
  141. def test_at_frame_raises_key_error(self, indexer_al):
  142. # GH#31724 .at should match .loc
  143. df = DataFrame({0: [1, 2, 3]}, index=[3, 2, 1])
  144. result = indexer_al(df)[1, 0]
  145. assert result == 3
  146. with pytest.raises(KeyError, match="a"):
  147. indexer_al(df)["a", 0]
  148. with pytest.raises(KeyError, match="a"):
  149. indexer_al(df)[1, "a"]
  150. def test_at_series_raises_key_error2(self, indexer_al):
  151. # at should not fallback
  152. # GH#7814
  153. # GH#31724 .at should match .loc
  154. ser = Series([1, 2, 3], index=list("abc"))
  155. result = indexer_al(ser)["a"]
  156. assert result == 1
  157. with pytest.raises(KeyError, match="^0$"):
  158. indexer_al(ser)[0]
  159. def test_at_frame_raises_key_error2(self, indexer_al):
  160. # GH#31724 .at should match .loc
  161. df = DataFrame({"A": [1, 2, 3]}, index=list("abc"))
  162. result = indexer_al(df)["a", "A"]
  163. assert result == 1
  164. with pytest.raises(KeyError, match="^0$"):
  165. indexer_al(df)["a", 0]
  166. def test_at_frame_multiple_columns(self):
  167. # GH#48296 - at shouldn't modify multiple columns
  168. df = DataFrame({"a": [1, 2], "b": [3, 4]})
  169. new_row = [6, 7]
  170. with pytest.raises(
  171. InvalidIndexError,
  172. match=f"You can only assign a scalar value not a \\{type(new_row)}",
  173. ):
  174. df.at[5] = new_row
  175. def test_at_getitem_mixed_index_no_fallback(self):
  176. # GH#19860
  177. ser = Series([1, 2, 3, 4, 5], index=["a", "b", "c", 1, 2])
  178. with pytest.raises(KeyError, match="^0$"):
  179. ser.at[0]
  180. with pytest.raises(KeyError, match="^4$"):
  181. ser.at[4]
  182. def test_at_categorical_integers(self):
  183. # CategoricalIndex with integer categories that don't happen to match
  184. # the Categorical's codes
  185. ci = CategoricalIndex([3, 4])
  186. arr = np.arange(4).reshape(2, 2)
  187. frame = DataFrame(arr, index=ci)
  188. for df in [frame, frame.T]:
  189. for key in [0, 1]:
  190. with pytest.raises(KeyError, match=str(key)):
  191. df.at[key, key]
  192. def test_at_applied_for_rows(self):
  193. # GH#48729 .at should raise InvalidIndexError when assigning rows
  194. df = DataFrame(index=["a"], columns=["col1", "col2"])
  195. new_row = [123, 15]
  196. with pytest.raises(
  197. InvalidIndexError,
  198. match=f"You can only assign a scalar value not a \\{type(new_row)}",
  199. ):
  200. df.at["a"] = new_row