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 86
86
function as nv ,
87
87
np_percentile_argname ,
88
88
)
89
+ from pandas .errors import InvalidIndexError
89
90
from pandas .util ._decorators import (
90
91
Appender ,
91
92
Substitution ,
@@ -4220,6 +4221,13 @@ def _set_value(
4220
4221
self .loc [index , col ] = value
4221
4222
self ._item_cache .pop (col , None )
4222
4223
4224
+ except InvalidIndexError as ii_err :
4225
+ # GH48729: Seems like you are trying to assign a value to a
4226
+ # row when only scalar options are permitted
4227
+ raise InvalidIndexError (
4228
+ f"You can only assign a scalar value not a { type (value )} "
4229
+ ) from ii_err
4230
+
4223
4231
def _ensure_valid_index (self , value ) -> None :
4224
4232
"""
4225
4233
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 @@ -197,8 +197,12 @@ def test_at_frame_raises_key_error2(self, indexer_al):
197
197
def test_at_frame_multiple_columns (self ):
198
198
# GH#48296 - at shouldn't modify multiple columns
199
199
df = DataFrame ({"a" : [1 , 2 ], "b" : [3 , 4 ]})
200
- with pytest .raises (InvalidIndexError , match = r"slice\(None, None, None\)" ):
201
- df .at [5 ] = [6 , 7 ]
200
+ new_row = [6 , 7 ]
201
+ with pytest .raises (
202
+ InvalidIndexError ,
203
+ match = f"You can only assign a scalar value not a \\ { type (new_row )} " ,
204
+ ):
205
+ df .at [5 ] = new_row
202
206
203
207
def test_at_getitem_mixed_index_no_fallback (self ):
204
208
# GH#19860
@@ -220,3 +224,13 @@ def test_at_categorical_integers(self):
220
224
for key in [0 , 1 ]:
221
225
with pytest .raises (KeyError , match = str (key )):
222
226
df .at [key , key ]
227
+
228
+ def test_at_applied_for_rows (self ):
229
+ # GH#48729 .at should raise InvalidIndexError when assigning rows
230
+ df = DataFrame (index = ["a" ], columns = ["col1" , "col2" ])
231
+ new_row = [123 , 15 ]
232
+ with pytest .raises (
233
+ InvalidIndexError ,
234
+ match = f"You can only assign a scalar value not a \\ { type (new_row )} " ,
235
+ ):
236
+ df .at ["a" ] = new_row
You can’t perform that action at this time.
0 commit comments