File tree 2 files changed +24
-2
lines changed
2 files changed +24
-2
lines changed Original file line number Diff line number Diff line change 91
91
function as nv ,
92
92
np_percentile_argname ,
93
93
)
94
+ from pandas .errors import InvalidIndexError
94
95
from pandas .util ._decorators import (
95
96
Appender ,
96
97
Substitution ,
@@ -4199,6 +4200,13 @@ def _set_value(
4199
4200
self .loc [index , col ] = value
4200
4201
self ._item_cache .pop (col , None )
4201
4202
4203
+ except InvalidIndexError as ii_err :
4204
+ # GH48729: Seems like you are trying to assign a value to a
4205
+ # row when only scalar options are permitted
4206
+ raise InvalidIndexError (
4207
+ f"You can only assign a scalar value not a { type (value )} "
4208
+ ) from ii_err
4209
+
4202
4210
def _ensure_valid_index (self , value ) -> None :
4203
4211
"""
4204
4212
Ensure that if we don't have an index, that we can create one from the
Original file line number Diff line number Diff line change @@ -211,8 +211,12 @@ def test_at_frame_raises_key_error2(self, indexer_al):
211
211
def test_at_frame_multiple_columns (self ):
212
212
# GH#48296 - at shouldn't modify multiple columns
213
213
df = DataFrame ({"a" : [1 , 2 ], "b" : [3 , 4 ]})
214
- with pytest .raises (InvalidIndexError , match = r"slice\(None, None, None\)" ):
215
- df .at [5 ] = [6 , 7 ]
214
+ new_row = [6 , 7 ]
215
+ with pytest .raises (
216
+ InvalidIndexError ,
217
+ match = f"You can only assign a scalar value not a \\ { type (new_row )} " ,
218
+ ):
219
+ df .at [5 ] = new_row
216
220
217
221
def test_at_getitem_mixed_index_no_fallback (self ):
218
222
# GH#19860
@@ -234,3 +238,13 @@ def test_at_categorical_integers(self):
234
238
for key in [0 , 1 ]:
235
239
with pytest .raises (KeyError , match = str (key )):
236
240
df .at [key , key ]
241
+
242
+ def test_at_applied_for_rows (self ):
243
+ # GH#48729 .at should raise InvalidIndexError when assigning rows
244
+ df = DataFrame (index = ["a" ], columns = ["col1" , "col2" ])
245
+ new_row = [123 , 15 ]
246
+ with pytest .raises (
247
+ InvalidIndexError ,
248
+ match = f"You can only assign a scalar value not a \\ { type (new_row )} " ,
249
+ ):
250
+ df .at ["a" ] = new_row
You can’t perform that action at this time.
0 commit comments