@@ -88,6 +88,63 @@ def test_dataframe(self):
8888 index_cols = pdf .columns [column_mask ]
8989 self .assert_eq (kdf [index_cols ], pdf [index_cols ])
9090
91+ def test_insert (self ):
92+ #
93+ # Basic DataFrame
94+ #
95+ pdf = pd .DataFrame ([1 , 2 , 3 ])
96+ kdf = ks .from_pandas (pdf )
97+
98+ kdf .insert (1 , "b" , 10 )
99+ pdf .insert (1 , "b" , 10 )
100+ self .assert_eq (kdf .sort_index (), pdf .sort_index (), almost = True )
101+ kdf .insert (2 , "c" , 0.1 )
102+ pdf .insert (2 , "c" , 0.1 )
103+ self .assert_eq (kdf .sort_index (), pdf .sort_index (), almost = True )
104+ kdf .insert (3 , "d" , kdf .b + 1 )
105+ pdf .insert (3 , "d" , pdf .b + 1 )
106+ self .assert_eq (kdf .sort_index (), pdf .sort_index (), almost = True )
107+
108+ kser = ks .Series ([4 , 5 , 6 ])
109+ self .assertRaises (ValueError , lambda : kdf .insert (0 , "y" , kser ))
110+ self .assertRaisesRegex (
111+ ValueError , "cannot insert b, already exists" , lambda : kdf .insert (1 , "b" , 10 )
112+ )
113+ self .assertRaisesRegex (
114+ ValueError ,
115+ '"column" should be a scalar value or tuple that contains scalar values' ,
116+ lambda : kdf .insert (0 , list ("abc" ), kser ),
117+ )
118+ self .assertRaises (ValueError , lambda : kdf .insert (0 , "e" , [7 , 8 , 9 , 10 ]))
119+ self .assertRaises (ValueError , lambda : kdf .insert (0 , "f" , ks .Series ([7 , 8 ])))
120+ self .assertRaises (AssertionError , lambda : kdf .insert (100 , "y" , kser ))
121+ self .assertRaises (AssertionError , lambda : kdf .insert (1 , "y" , kser , allow_duplicates = True ))
122+
123+ #
124+ # DataFrame with MultiIndex as columns
125+ #
126+ pdf = pd .DataFrame ({("x" , "a" , "b" ): [1 , 2 , 3 ]})
127+ kdf = ks .from_pandas (pdf )
128+
129+ kdf .insert (1 , "b" , 10 )
130+ pdf .insert (1 , "b" , 10 )
131+ self .assert_eq (kdf .sort_index (), pdf .sort_index (), almost = True )
132+ kdf .insert (2 , "c" , 0.1 )
133+ pdf .insert (2 , "c" , 0.1 )
134+ self .assert_eq (kdf .sort_index (), pdf .sort_index (), almost = True )
135+ kdf .insert (3 , "d" , kdf .b + 1 )
136+ pdf .insert (3 , "d" , pdf .b + 1 )
137+ self .assert_eq (kdf .sort_index (), pdf .sort_index (), almost = True )
138+
139+ self .assertRaisesRegex (
140+ ValueError , "cannot insert d, already exists" , lambda : kdf .insert (4 , "d" , 11 )
141+ )
142+ self .assertRaisesRegex (
143+ ValueError ,
144+ '"column" must have length equal to number of column levels.' ,
145+ lambda : kdf .insert (4 , ("e" ,), 11 ),
146+ )
147+
91148 def test_inplace (self ):
92149 pdf , kdf = self .df_pair
93150
0 commit comments