11"""
22Tests for iloc dictionary assignment bug fix.
33
4- Regression test for GH#62723: Series.iloc assignment with dtype="object"
4+ Regression test for GH#62723: Series.iloc assignment with dtype="object"
55incorrectly converts dictionary values to Series.
66"""
7- import numpy as np
7+
88import pytest
99
1010from pandas import (
1111 DataFrame ,
1212 Series ,
1313)
14- import pandas ._testing as tm
1514
1615
1716class TestIlocDictionaryAssignment :
@@ -22,10 +21,10 @@ def test_iloc_preserves_dict_object_dtype(self):
2221 # GH#62723
2322 s = Series (0 , dtype = "object" )
2423 test_dict = {}
25-
24+
2625 # Assign dictionary via iloc
2726 s .iloc [0 ] = test_dict
28-
27+
2928 # Verify dictionary is preserved
3029 assert s [0 ] == test_dict
3130 assert isinstance (s [0 ], dict )
@@ -34,33 +33,33 @@ def test_iloc_preserves_complex_dict_object_dtype(self):
3433 """Test that iloc preserves complex dictionaries in object dtype Series."""
3534 s = Series (0 , dtype = "object" )
3635 test_dict = {
37- 'a' : 1 ,
38- 'b' : [1 , 2 , 3 ],
39- 'c' : {' nested' : True },
40- 'd' : None ,
41- 'e' : 3.14
36+ "a" : 1 ,
37+ "b" : [1 , 2 , 3 ],
38+ "c" : {" nested" : True },
39+ "d" : None ,
40+ "e" : 3.14 ,
4241 }
43-
42+
4443 s .iloc [0 ] = test_dict
45-
44+
4645 assert s [0 ] == test_dict
4746 assert isinstance (s [0 ], dict )
48- assert s [0 ]['a' ] == 1
49- assert s [0 ]['b' ] == [1 , 2 , 3 ]
50- assert s [0 ]['c' ] == {' nested' : True }
51- assert s [0 ]['d' ] is None
52- assert s [0 ]['e' ] == 3.14
47+ assert s [0 ]["a" ] == 1
48+ assert s [0 ]["b" ] == [1 , 2 , 3 ]
49+ assert s [0 ]["c" ] == {" nested" : True }
50+ assert s [0 ]["d" ] is None
51+ assert s [0 ]["e" ] == 3.14
5352
5453 def test_iloc_vs_loc_dict_assignment_consistency (self ):
5554 """Test that iloc and direct assignment behave consistently."""
5655 # Original bug: s[0] = {} works but s.iloc[0] = {} converts to Series
5756 s = Series (0 , dtype = "object" )
58-
57+
5958 # Direct assignment should work (baseline)
6059 s [0 ] = {}
6160 assert s [0 ] == {}
6261 assert isinstance (s [0 ], dict )
63-
62+
6463 # Reset and test iloc
6564 s = Series (0 , dtype = "object" )
6665 s .iloc [0 ] = {}
@@ -70,12 +69,12 @@ def test_iloc_vs_loc_dict_assignment_consistency(self):
7069 def test_iloc_multiple_dict_assignments (self ):
7170 """Test iloc dictionary assignment to multiple positions."""
7271 s = Series ([0 , 1 , 2 ], dtype = "object" )
73- dict1 = {' first' : 1 }
74- dict2 = {' second' : 2 }
75-
72+ dict1 = {" first" : 1 }
73+ dict2 = {" second" : 2 }
74+
7675 s .iloc [0 ] = dict1
7776 s .iloc [2 ] = dict2
78-
77+
7978 assert s .iloc [0 ] == dict1
8079 assert isinstance (s .iloc [0 ], dict )
8180 assert s .iloc [1 ] == 1 # unchanged
@@ -85,26 +84,26 @@ def test_iloc_multiple_dict_assignments(self):
8584 def test_iloc_dict_assignment_non_object_dtype_fails (self ):
8685 """Test that iloc dict assignment to non-object dtypes fails as expected."""
8786 s = Series ([1 , 2 , 3 ], dtype = "int64" )
88-
87+
8988 # This should fail for non-object dtypes
9089 with pytest .raises ((ValueError , TypeError )):
91- s .iloc [0 ] = {' key' : ' value' }
90+ s .iloc [0 ] = {" key" : " value" }
9291
9392 def test_iloc_preserves_other_object_types (self ):
9493 """Test that iloc preserves other object types correctly."""
9594 s = Series ([None ] * 4 , dtype = "object" )
96-
95+
9796 # Test various object types
9897 test_list = [1 , 2 , 3 ]
9998 test_tuple = (1 , 2 , 3 )
10099 test_set = {1 , 2 , 3 }
101100 test_str = "test string"
102-
101+
103102 s .iloc [0 ] = test_list
104103 s .iloc [1 ] = test_tuple
105104 s .iloc [2 ] = test_set
106105 s .iloc [3 ] = test_str
107-
106+
108107 assert s .iloc [0 ] == test_list
109108 assert isinstance (s .iloc [0 ], list )
110109 assert s .iloc [1 ] == test_tuple
@@ -116,36 +115,36 @@ def test_iloc_preserves_other_object_types(self):
116115
117116 def test_dataframe_iloc_dict_assignment_unaffected (self ):
118117 """Test that the fix doesn't break DataFrame iloc dict assignment."""
119- df = DataFrame ({'A' : [0 , 1 ], 'B' : [2 , 3 ]}, dtype = "object" )
120- test_dict = {' frame' : ' test' }
121-
122- # DataFrame iloc should still work correctly
118+ df = DataFrame ({"A" : [0 , 1 ], "B" : [2 , 3 ]}, dtype = "object" )
119+ test_dict = {" frame" : " test" }
120+
121+ # DataFrame iloc should still work correctly
123122 df .iloc [0 , 0 ] = test_dict
124-
123+
125124 assert df .iloc [0 , 0 ] == test_dict
126125 assert isinstance (df .iloc [0 , 0 ], dict )
127126
128127 def test_nested_dict_assignment (self ):
129128 """Test iloc assignment with deeply nested dictionaries."""
130129 s = Series ([None ], dtype = "object" )
131130 nested_dict = {
132- ' level1' : {
133- ' level2' : {
134- ' level3' : {
135- ' deep' : ' value' ,
136- ' list' : [1 , 2 , {' nested_in_list' : True }]
131+ " level1" : {
132+ " level2" : {
133+ " level3" : {
134+ " deep" : " value" ,
135+ " list" : [1 , 2 , {" nested_in_list" : True }],
137136 }
138137 }
139138 },
140- ' another_key' : [{' dict_in_list' : ' test' }]
139+ " another_key" : [{" dict_in_list" : " test" }],
141140 }
142-
141+
143142 s .iloc [0 ] = nested_dict
144-
143+
145144 assert s .iloc [0 ] == nested_dict
146145 assert isinstance (s .iloc [0 ], dict )
147- assert s .iloc [0 ][' level1' ][ ' level2' ][ ' level3' ][ ' deep' ] == ' value'
148- assert s .iloc [0 ][' another_key' ][0 ][' dict_in_list' ] == ' test'
146+ assert s .iloc [0 ][" level1" ][ " level2" ][ " level3" ][ " deep" ] == " value"
147+ assert s .iloc [0 ][" another_key" ][0 ][" dict_in_list" ] == " test"
149148
150149 def test_original_bug_reproduction (self ):
151150 """Direct test of the original bug report scenario."""
@@ -160,4 +159,4 @@ def test_original_bug_reproduction(self):
160159
161160 # Additional verification
162161 assert isinstance (s [0 ], dict )
163- assert type (s [0 ]) == dict
162+ assert type (s [0 ]) == dict
0 commit comments