6
6
import numpy as np
7
7
import pytest
8
8
9
- from pandas .errors import InvalidIndexError
10
-
11
9
from pandas import (
12
10
CategoricalDtype ,
13
11
CategoricalIndex ,
@@ -105,12 +103,15 @@ def test_at_setitem_multiindex(self):
105
103
np .zeros ((3 , 2 ), dtype = "int64" ),
106
104
columns = MultiIndex .from_tuples ([("a" , 0 ), ("a" , 1 )]),
107
105
)
108
- df .at [0 , "a" ] = 10
106
+ df .at [0 , ("a" , 0 )] = 10
107
+ df .at [0 , ("a" , 1 )] = 10
109
108
expected = DataFrame (
110
109
[[10 , 10 ], [0 , 0 ], [0 , 0 ]],
111
110
columns = MultiIndex .from_tuples ([("a" , 0 ), ("a" , 1 )]),
112
111
)
113
112
tm .assert_frame_equal (df , expected )
113
+ with pytest .raises (TypeError , match = "" ):
114
+ df .at [0 , "a" ] = 11
114
115
115
116
@pytest .mark .parametrize ("row" , (Timestamp ("2019-01-01" ), "2019-01-01" ))
116
117
def test_at_datetime_index (self , row ):
@@ -126,11 +127,13 @@ def test_at_datetime_index(self, row):
126
127
tm .assert_frame_equal (df , expected )
127
128
128
129
129
- class TestAtSetItemWithExpansion :
130
- def test_at_setitem_expansion_series_dt64tz_value (self , tz_naive_fixture ):
130
+ class TestAtSetTzItem :
131
+ def test_at_setitem_series_dt64tz_value (self , tz_naive_fixture ):
131
132
# GH#25506
133
+ # Modified in GH#48323 due to .at change
132
134
ts = Timestamp ("2017-08-05 00:00:00+0100" , tz = tz_naive_fixture )
133
- result = Series (ts )
135
+ ts2 = Timestamp ("2017-09-05 00:00:00+0100" , tz = tz_naive_fixture )
136
+ result = Series ([ts , ts2 ])
134
137
result .at [1 ] = ts
135
138
expected = Series ([ts , ts ])
136
139
tm .assert_series_equal (result , expected )
@@ -211,7 +214,7 @@ def test_at_frame_raises_key_error2(self, indexer_al):
211
214
def test_at_frame_multiple_columns (self ):
212
215
# GH#48296 - at shouldn't modify multiple columns
213
216
df = DataFrame ({"a" : [1 , 2 ], "b" : [3 , 4 ]})
214
- with pytest .raises (InvalidIndexError , match = r"slice\(None, None, None\) " ):
217
+ with pytest .raises (TypeError , match = "col " ):
215
218
df .at [5 ] = [6 , 7 ]
216
219
217
220
def test_at_getitem_mixed_index_no_fallback (self ):
@@ -234,3 +237,9 @@ def test_at_categorical_integers(self):
234
237
for key in [0 , 1 ]:
235
238
with pytest .raises (KeyError , match = str (key )):
236
239
df .at [key , key ]
240
+
241
+ def test_at_does_not_expand (self ):
242
+ # GH#48323
243
+ frame = DataFrame ({"a" : [1 , 2 ]})
244
+ with pytest .raises (KeyError , match = "b" ):
245
+ frame .at [2 , "b" ] = 9
0 commit comments