Skip to content

Commit

Permalink
Merge branch 'master' into issue204
Browse files Browse the repository at this point in the history
  • Loading branch information
adnanhemani committed Jan 14, 2017
2 parents 3cf2a59 + 7a92fff commit d066cc2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -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`.
Expand Down
43 changes: 27 additions & 16 deletions datascience/tables.py
Expand Up @@ -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


##################
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1995,9 +2005,9 @@ def annotate(axis, ticks):
tick_labels = [ticks[int(l)] if 0<=l<len(ticks) else '' for l in axis.get_xticks()]
axis.set_xticklabels(tick_labels, stretch='ultra-condensed')

self._visualize(column_for_categories, labels, xticks, overlay, draw, annotate)
self._visualize(column_for_categories, labels, xticks, overlay, draw, annotate, width=width, height=height)

def barh(self, column_for_categories=None, select=None, overlay=True, **vargs):
def barh(self, column_for_categories=None, select=None, overlay=True, width=6, **vargs):
"""Plot horizontal bar charts for the table.
Args:
Expand Down Expand Up @@ -2049,17 +2059,22 @@ def barh(self, column_for_categories=None, select=None, overlay=True, **vargs):

index = np.arange(self.num_rows)
margin = 0.1
width = 1 - 2 * margin
bwidth = 1 - 2 * margin
if overlay:
width /= len(labels)
bwidth /= len(labels)

if 'height' in options:
height = options.pop('height')
else:
height = max(4, len(index)/2)

def draw(axis, label, color):
if overlay:
ypos = index + margin + (1-2*margin)*(n - 1 - labels.index(label))/n
else:
ypos = index
# barh plots entries in reverse order from bottom to top
axis.barh(ypos, self[label][::-1], width, color=color, **options)
axis.barh(ypos, self[label][::-1], bwidth, color=color, **options)

def annotate(axis, ticks):
axis.set_yticks(index+0.5) # Center labels on bars
Expand All @@ -2069,14 +2084,10 @@ def annotate(axis, ticks):
if isinstance(column_for_categories, str):
axis.set_ylabel(column_for_categories)

height = max(4, len(index)/2)
if 'height' in vargs:
height = vargs.pop('height')

self._visualize('', labels, yticks, overlay, draw, annotate, height=height)
self._visualize('', labels, yticks, overlay, draw, annotate, width=width, height=height)

def scatter(self, column_for_x, select=None, overlay=True, fit_line=False,
colors=None, labels=None, **vargs):
colors=None, labels=None, width=5, height=5, **vargs):
"""Creates scatterplots, optionally adding a line of best fit.
Args:
Expand Down Expand Up @@ -2156,7 +2167,7 @@ def draw(axis, label, color):
arrowprops = dict(arrowstyle = '->', 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.
Expand Down
17 changes: 17 additions & 0 deletions tests/test_tables.py
Expand Up @@ -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 #
Expand Down

0 comments on commit d066cc2

Please sign in to comment.