MCPcopy
hub / github.com/pandas-dev/pandas / delete

Method delete

pandas/io/pytables.py:4709–4767  ·  view source on GitHub ↗
(
        self, where=None, start: int | None = None, stop: int | None = None
    )

Source from the content-addressed store, hash-verified

4707 self.table.flush()
4708
4709 def delete(
4710 self, where=None, start: int | None = None, stop: int | None = None
4711 ) -> int | None:
4712 # delete all rows (and return the nrows)
4713 if where is None or not len(where):
4714 if start is None and stop is None:
4715 nrows = self.nrows
4716 self._handle.remove_node(self.group, recursive=True)
4717 else:
4718 # pytables<3.0 would remove a single row with stop=None
4719 if stop is None:
4720 stop = self.nrows
4721 nrows = self.table.remove_rows(start=start, stop=stop)
4722 self.table.flush()
4723 return nrows
4724
4725 # infer the data kind
4726 if not self.infer_axes():
4727 return None
4728
4729 # create the selection
4730 table = self.table
4731 selection = Selection(self, where, start=start, stop=stop)
4732 values = selection.select_coords()
4733
4734 # delete the rows in reverse order
4735 sorted_series = Series(values, copy=False).sort_values()
4736 ln = len(sorted_series)
4737
4738 if ln:
4739 # construct groups of consecutive rows
4740 diff = sorted_series.diff()
4741 groups = list(diff[diff > 1].index)
4742
4743 # 1 group
4744 if not groups:
4745 groups = [0]
4746
4747 # final element
4748 if groups[-1] != ln:
4749 groups.append(ln)
4750
4751 # initial element
4752 if groups[0] != 0:
4753 groups.insert(0, 0)
4754
4755 # we must remove in reverse order!
4756 pg = groups.pop()
4757 for g in reversed(groups):
4758 rows = sorted_series.take(range(g, pg))
4759 table.remove_rows(
4760 start=rows[rows.index[0]], stop=rows[rows.index[-1]] + 1
4761 )
4762 pg = g
4763
4764 self.table.flush()
4765
4766 # return the number of rows removed

Callers

nothing calls this directly

Calls 11

select_coordsMethod · 0.95
SeriesClass · 0.90
SelectionClass · 0.85
infer_axesMethod · 0.80
flushMethod · 0.45
sort_valuesMethod · 0.45
diffMethod · 0.45
appendMethod · 0.45
insertMethod · 0.45
popMethod · 0.45
takeMethod · 0.45

Tested by

no test coverage detected