diff --git a/README.md b/README.md index 5e0f0c1b..d56ff398 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,9 @@ pip install datascience This project adheres to [Semantic Versioning](http://semver.org/). +### v0.9.0 +- Added "Table.remove" + ### v0.8.2 - Added `proportions_from_distribution` method to `datascience.util`. diff --git a/datascience/tables.py b/datascience/tables.py index c9278ef2..952cf249 100644 --- a/datascience/tables.py +++ b/datascience/tables.py @@ -552,7 +552,17 @@ def relabel(self, column_label, new_label): self._formats[old_to_new[label]] = formatter return self - + def remove(self, row_or_row_indices): + """Removes a row or multiple rows of a table in place.""" + if not row_or_row_indices: + return + if isinstance(row_or_row_indices, int): + rows_remove = [row_or_row_indices] + else: + rows_remove = row_or_row_indices + for col in self._columns: + self._columns[col] = [elem for i, elem in enumerate(self[col]) if i not in rows_remove] + return self ################## @@ -1894,7 +1904,7 @@ def to_array(self): 'alpha': 0.7, } - def plot(self, column_for_xticks=None, select=None, overlay=True, **vargs): + def plot(self, column_for_xticks=None, select=None, overlay=True, width=6, height=4, **vargs): """Plot line charts for the table. Args: @@ -1956,9 +1966,9 @@ def draw(axis, label, color): else: axis.plot(x_data, self[label], color=color, **options) - self._visualize(x_label, y_labels, None, overlay, draw, _vertical_x) + self._visualize(x_label, y_labels, None, overlay, draw, _vertical_x, width=width, height=height) - def bar(self, column_for_categories=None, select=None, overlay=True, **vargs): + def bar(self, column_for_categories=None, select=None, overlay=True, width=6, height=4, **vargs): """Plot bar charts for the table. Each plot is labeled using the values in `column_for_categories` and @@ -1995,9 +2005,9 @@ def annotate(axis, ticks): tick_labels = [ticks[int(l)] if 0<=l', connectionstyle = 'arc3,rad=0', color='black')) x_label = self._as_label(column_for_x) - self._visualize(x_label, y_labels, None, overlay, draw, _vertical_x, width=5, height=5) + self._visualize(x_label, y_labels, None, overlay, draw, _vertical_x, width=width, height=height) def _visualize(self, x_label, y_labels, ticks, overlay, draw, annotate, width=6, height=4): """Generic visualization that overlays or separates the draw function. diff --git a/tests/test_tables.py b/tests/test_tables.py index f4fcc68b..88ba3a65 100644 --- a/tests/test_tables.py +++ b/tests/test_tables.py @@ -740,6 +740,23 @@ def test_bin(table): 13 | 0 """) +def test_remove_multiple(table): + table.remove([1, 3]) + assert_equal(table, """ + letter | count | points + a | 9 | 1 + c | 3 | 2 + """) + +def test_remove_single(table): + table.remove(1) + assert_equal(table, """ + letter | count | points + a | 9 | 1 + c | 3 | 2 + z | 1 | 10 + """) + ########## # Create #