@@ -1315,11 +1315,48 @@ def quantile(
1315
1315
# ---------------------------------------------------------------------
1316
1316
# Abstract Methods Overridden By EABackedBlock and NumpyBlock
1317
1317
1318
- def delete (self , loc ) -> Block :
1319
- """
1320
- Return a new Block with the given loc(s) deleted.
1318
+ def delete (self , loc ) -> list [Block ]:
1319
+ """Deletes the locs from the block.
1320
+
1321
+ We split the block to avoid copying the underlying data. We create new
1322
+ blocks for every connected segment of the initial block that is not deleted.
1323
+ The new blocks point to the initial array.
1321
1324
"""
1322
- raise AbstractMethodError (self )
1325
+ if not is_list_like (loc ):
1326
+ loc = [loc ]
1327
+
1328
+ if self .ndim == 1 :
1329
+ values = cast (np .ndarray , self .values )
1330
+ values = np .delete (values , loc )
1331
+ mgr_locs = self ._mgr_locs .delete (loc )
1332
+ return [type (self )(values , placement = mgr_locs , ndim = self .ndim )]
1333
+
1334
+ if np .max (loc ) >= self .values .shape [0 ]:
1335
+ raise IndexError
1336
+
1337
+ # Add one out-of-bounds indexer as maximum to collect
1338
+ # all columns after our last indexer if any
1339
+ loc = np .concatenate ([loc , [self .values .shape [0 ]]])
1340
+ mgr_locs_arr = self ._mgr_locs .as_array
1341
+ new_blocks : list [Block ] = []
1342
+
1343
+ previous_loc = - 1
1344
+ for idx in loc :
1345
+
1346
+ if idx == previous_loc + 1 :
1347
+ # There is no column between current and last idx
1348
+ pass
1349
+ else :
1350
+ # No overload variant of "__getitem__" of "ExtensionArray" matches
1351
+ # argument type "Tuple[slice, slice]"
1352
+ values = self .values [previous_loc + 1 : idx , :] # type: ignore[call-overload] # noqa
1353
+ locs = mgr_locs_arr [previous_loc + 1 : idx ]
1354
+ nb = type (self )(values , placement = BlockPlacement (locs ), ndim = self .ndim )
1355
+ new_blocks .append (nb )
1356
+
1357
+ previous_loc = idx
1358
+
1359
+ return new_blocks
1323
1360
1324
1361
@property
1325
1362
def is_view (self ) -> bool :
@@ -1532,11 +1569,13 @@ def putmask(self, mask, new) -> list[Block]:
1532
1569
1533
1570
return [self ]
1534
1571
1535
- def delete (self , loc ) -> Block :
1572
+ def delete (self , loc ) -> list [ Block ] :
1536
1573
# This will be unnecessary if/when __array_function__ is implemented
1537
- values = self .values .delete (loc )
1538
- mgr_locs = self ._mgr_locs .delete (loc )
1539
- return type (self )(values , placement = mgr_locs , ndim = self .ndim )
1574
+ if self .ndim == 1 :
1575
+ values = self .values .delete (loc )
1576
+ mgr_locs = self ._mgr_locs .delete (loc )
1577
+ return [type (self )(values , placement = mgr_locs , ndim = self .ndim )]
1578
+ return super ().delete (loc )
1540
1579
1541
1580
@cache_readonly
1542
1581
def array_values (self ) -> ExtensionArray :
@@ -1851,11 +1890,6 @@ def get_values(self, dtype: DtypeObj | None = None) -> np.ndarray:
1851
1890
def values_for_json (self ) -> np .ndarray :
1852
1891
return self .values
1853
1892
1854
- def delete (self , loc ) -> Block :
1855
- values = np .delete (self .values , loc , 0 )
1856
- mgr_locs = self ._mgr_locs .delete (loc )
1857
- return type (self )(values , placement = mgr_locs , ndim = self .ndim )
1858
-
1859
1893
1860
1894
class NumericBlock (NumpyBlock ):
1861
1895
__slots__ = ()
0 commit comments