test_at.py 7.9 KB

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