@@ -1211,19 +1211,15 @@ def test_scientific_notation():
1211
1211
1212
1212
def test_drop ():
1213
1213
"""Test that we can drop nested columns from a NestedFrame"""
1214
-
1215
1214
base = NestedFrame (data = {"a" : [1 , 2 , 3 ], "b" : [2 , 4 , 6 ]}, index = [0 , 1 , 2 ])
1216
-
1217
1215
nested = pd .DataFrame (
1218
1216
data = {"c" : [0 , 2 , 4 , 1 , 4 , 3 , 1 , 4 , 1 ], "d" : [5 , 4 , 7 , 5 , 3 , 1 , 9 , 3 , 4 ]},
1219
1217
index = [0 , 0 , 0 , 1 , 1 , 1 , 2 , 2 , 2 ],
1220
1218
)
1221
-
1222
1219
nested2 = pd .DataFrame (
1223
1220
data = {"e" : [0 , 2 , 4 , 1 , 4 , 3 , 1 , 4 , 1 ], "f" : [5 , 4 , 7 , 5 , 3 , 1 , 9 , 3 , 4 ]},
1224
1221
index = [0 , 0 , 0 , 1 , 1 , 1 , 2 , 2 , 2 ],
1225
1222
)
1226
-
1227
1223
base = base .add_nested (nested , "nested" ).add_nested (nested2 , "nested2" )
1228
1224
1229
1225
# test axis=0 drop
@@ -1256,6 +1252,31 @@ def test_drop():
1256
1252
assert "c" not in dropped_multiple .nested .nest .fields
1257
1253
assert "f" not in dropped_multiple .nested2 .nest .fields
1258
1254
1255
+ # Test inplace=True for both base and nested columns
1256
+ base2 = base .copy ()
1257
+ base2 .drop (["a" , "nested.c" ], axis = 1 , inplace = True )
1258
+ assert "a" not in base2 .columns
1259
+ assert "c" not in base2 ["nested" ].nest .fields
1260
+ assert "b" in base2 .columns
1261
+ assert "d" in base2 ["nested" ].nest .fields
1262
+
1263
+ # Test inplace=False for both base and nested columns
1264
+ base3 = base .copy ()
1265
+ dropped = base3 .drop (["a" , "nested.c" ], axis = 1 , inplace = False )
1266
+ assert "a" not in dropped .columns
1267
+ assert "c" not in dropped ["nested" ].nest .fields
1268
+ assert "b" in dropped .columns
1269
+ assert "d" in dropped ["nested" ].nest .fields
1270
+ # Original is unchanged
1271
+ assert "a" in base3 .columns
1272
+ assert "c" in base3 ["nested" ].nest .fields
1273
+
1274
+ # Test error for missing columns in multi-drop
1275
+ with pytest .raises (KeyError ):
1276
+ base .drop (["not_a_column" , "nested.c" ], axis = 1 )
1277
+ with pytest .raises (KeyError ):
1278
+ base .drop (["a" , "nested.not_a_field" ], axis = 1 )
1279
+
1259
1280
1260
1281
def test_eval ():
1261
1282
"""
@@ -1496,3 +1517,25 @@ def test_nest_lists():
1496
1517
# and that we raise an error if we try to do so.
1497
1518
with pytest .raises (ValueError ):
1498
1519
ndf .nest_lists (columns = ["c" , "d" ], name = "nested" )
1520
+
1521
+
1522
+ def test_delitem_base_and_nested ():
1523
+ """Test that __delitem__ works for both base and nested columns."""
1524
+ base = NestedFrame (data = {"a" : [1 , 2 , 3 ], "b" : [2 , 4 , 6 ]}, index = [0 , 1 , 2 ])
1525
+ nested = pd .DataFrame (
1526
+ data = {"c" : [0 , 2 , 4 , 1 , 4 , 3 , 1 , 4 , 1 ], "d" : [5 , 4 , 7 , 5 , 3 , 1 , 9 , 3 , 4 ]},
1527
+ index = [0 , 0 , 0 , 1 , 1 , 1 , 2 , 2 , 2 ],
1528
+ )
1529
+ base = base .add_nested (nested , "nested" )
1530
+
1531
+ # Delete a nested field
1532
+ del base ["nested.c" ]
1533
+ assert "c" not in base ["nested" ].nest .fields
1534
+ # Delete a base column
1535
+ del base ["a" ]
1536
+ assert "a" not in base .columns
1537
+ # Deleting a missing column should raise KeyError
1538
+ with pytest .raises (KeyError ):
1539
+ del base ["not_a_column" ]
1540
+ with pytest .raises (KeyError ):
1541
+ del base ["nested.not_a_field" ]
0 commit comments