Skip to content

Commit 78de4c5

Browse files
committed
add bin tests
1 parent cf8e30b commit 78de4c5

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

datascience/tables.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ def with_relabeling(self, *args):
10941094
warnings.warn("with_relabeling is deprecated; use relabeled", FutureWarning)
10951095
return self.relabeled(*args)
10961096

1097-
def bin(self, **vargs):
1097+
def bin(self, select=None, **vargs):
10981098
"""Group values by bin and compute counts per bin by column.
10991099
11001100
By default, bins are chosen to contain all values in all columns. The
@@ -1105,6 +1105,9 @@ def bin(self, **vargs):
11051105
n+1 columns, where column 0 contains the lower bound of each bin.
11061106
11071107
Args:
1108+
``select`` (columns): Columns to be binned. If None, all columns
1109+
are binned.
1110+
11081111
``bins`` (int or sequence of scalars): If bins is an int,
11091112
it defines the number of equal-width bins in the given range
11101113
(10, by default). If bins is a sequence, it defines the bin
@@ -1122,6 +1125,8 @@ def bin(self, **vargs):
11221125
histogram values will not be equal to 1 unless bins of unity
11231126
width are chosen; it is not a probability mass function.
11241127
"""
1128+
if select is not None:
1129+
self = self.select(select)
11251130
if 'normed' in vargs:
11261131
vargs.setdefault('density', vargs.pop('normed'))
11271132
density = vargs.get('density', False)

tests/test_tables.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,26 @@ def test_with_relabeling(table):
674674
z | 1 | 10
675675
""")
676676

677+
def test_bin(table):
678+
binned = table.bin('count')
679+
assert_equal(binned.take(np.arange(5)), """
680+
bin | count count
681+
1 | 1
682+
1.8 | 0
683+
2.6 | 2
684+
3.4 | 0
685+
4.2 | 0
686+
""")
687+
binned = table.select([1, 2]).bin(bins=4)
688+
assert_equal(binned, """
689+
bin | count count | points count
690+
1 | 3 | 3
691+
3.25 | 0 | 0
692+
5.5 | 0 | 0
693+
7.75 | 1 | 1
694+
10 | 0 | 0
695+
""")
696+
677697

678698
##########
679699
# Create #

0 commit comments

Comments
 (0)