Skip to content

Commit

Permalink
Merge branch 'master' into geojson_url
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwagner committed Jul 4, 2019
2 parents 85b2b52 + 96b4a50 commit 1b74371
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 43 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,16 @@ pip install datascience

This project adheres to [Semantic Versioning](http://semver.org/).

### v0.13.1
### v0.13.4
* Adds support to `Map#read_geojson` for reading in GeoJSON from links.

### v0.13.2
* Changes default formatting of numbers in printed output to 12345 instead of 12,345.

### v0.13.1
* Allows for the following notations ("floating arguments") to be used with
`Table#take` and `Table#exclude`: ex.`t.take(0, 1, 2, 3)` and `t.exclude(0, 2, 4)`.

### v0.13.0
* Removes deprecated argument for Table#__init__.

Expand Down
2 changes: 1 addition & 1 deletion datascience/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def format_value(value):
if isinstance(value, (bool, np.bool_)):
return str(value)
elif isinstance(value, (int, np.integer)):
return '{:,d}'.format(value)
return '{:d}'.format(value)
elif isinstance(value, (float, np.floating)):
return '{:g}'.format(value)
else:
Expand Down
46 changes: 31 additions & 15 deletions datascience/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,15 +524,15 @@ def relabel(self, column_label, new_label):
... 'id', make_array(12345, 123, 5123))
>>> table.relabel('id', 'yolo')
points | yolo
1 | 12,345
1 | 12345
2 | 123
3 | 5,123
3 | 5123
>>> table.relabel(make_array('points', 'yolo'),
... make_array('red', 'blue'))
red | blue
1 | 12,345
1 | 12345
2 | 123
3 | 5,123
3 | 5123
>>> table.relabel(make_array('red', 'green', 'blue'),
... make_array('cyan', 'magenta', 'yellow', 'key'))
Traceback (most recent call last):
Expand Down Expand Up @@ -1710,21 +1710,21 @@ def with_columns(self, *labels_and_values, **formatter):
>>> players = Table().with_columns('player_id',
... make_array(110234, 110235), 'wOBA', make_array(.354, .236))
>>> players
player_id | wOBA
110,234 | 0.354
110,235 | 0.236
player_id | wOBA
110234 | 0.354
110235 | 0.236
>>> players = players.with_columns('salaries', 'N/A', 'season', 2016)
>>> players
player_id | wOBA | salaries | season
110,234 | 0.354 | N/A | 2,016
110,235 | 0.236 | N/A | 2,016
player_id | wOBA | salaries | season
110234 | 0.354 | N/A | 2016
110235 | 0.236 | N/A | 2016
>>> salaries = Table().with_column('salary',
... make_array(500000, 15500000))
>>> players.with_columns('salaries', salaries.column('salary'),
... 'bonus', make_array(6, 1), formatter=_formats.CurrencyFormatter)
player_id | wOBA | salaries | season | bonus
110,234 | 0.354 | $500,000 | 2,016 | $6
110,235 | 0.236 | $15,500,000 | 2,016 | $1
player_id | wOBA | salaries | season | bonus
110234 | 0.354 | $500,000 | 2016 | $6
110235 | 0.236 | $15,500,000 | 2016 | $1
>>> players.with_columns(2, make_array('$600,000', '$20,000,000'))
Traceback (most recent call last):
...
Expand Down Expand Up @@ -3002,8 +3002,14 @@ class _RowSelector(metaclass=abc.ABCMeta):
def __init__(self, table):
self._table = table

def __call__(self, row_numbers_or_slice):
return self[row_numbers_or_slice]
def __call__(self, row_numbers_or_slice, *args):
if args:
all_args = list(args)
all_args.insert(0, row_numbers_or_slice)
all_args = np.array(all_args)
else:
all_args = row_numbers_or_slice
return self.__getitem__(all_args)

@abc.abstractmethod
def __getitem__(self, item):
Expand Down Expand Up @@ -3059,6 +3065,10 @@ def __getitem__(self, row_indices_or_slice):
A+ | 4
A | 4
A- | 3.7
>>> grades.take(0, 2)
letter grade | gpa
A+ | 4
A- | 3.7
>>> grades.take(10)
Traceback (most recent call last):
...
Expand Down Expand Up @@ -3122,6 +3132,12 @@ def __getitem__(self, row_indices_or_slice):
B+ | 3.3
B | 3
B- | 2.7
>>> t.exclude(0, 2)
letter grade | gpa
A | 4
B+ | 3.3
B | 3
B- | 2.7
Note that ``exclude`` also supports NumPy-like indexing and slicing:
Expand Down
2 changes: 1 addition & 1 deletion datascience/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.13.1'
__version__ = '0.13.4'
4 changes: 2 additions & 2 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def test_doctests():
def test_default_format():
fmt = ds.default_formatter.format_value
assert_equal(fmt(1.23456789), '1.23457')
assert_equal(fmt(123456789), '123,456,789')
assert_equal(fmt(123456789**5), '28,679,718,602,997,181,072,337,614,380,936,720,482,949')
assert_equal(fmt(123456789), '123456789')
assert_equal(fmt(123456789**5), '28679718602997181072337614380936720482949')
assert_equal(fmt(123.456789**5), '2.86797e+10')
assert_equal(fmt(True), 'True')
assert_equal(fmt(False), 'False')
Expand Down
64 changes: 41 additions & 23 deletions tests/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,15 @@ def test_take_iterable(table):
c | 3 | 2
""")

def test_take_floating_args(table):
t = table
test = t.take(0, 2)
assert_equal(test, """
letter | count | points
a | 9 | 1
c | 3 | 2
""")


def test_exclude(table):
t = table
Expand Down Expand Up @@ -245,6 +254,15 @@ def test_exclude_iterable(table):
z | 1 | 10
""")

def test_exclude_floating_args(table):
t = table
test = t.exclude(1, 3)
assert_equal(test, """
letter | count | points
a | 9 | 1
c | 3 | 2
""")


def test_stats(table):
t = table
Expand Down Expand Up @@ -823,43 +841,43 @@ def test_with_column_with_formatter(table):
def test_with_columns():
players = Table().with_columns('player_id', make_array(110234, 110235), 'wOBA', make_array(.354, .236))
assert_equal(players, """
player_id | wOBA
110,234 | 0.354
110,235 | 0.236
player_id | wOBA
110234 | 0.354
110235 | 0.236
""")
players = players.with_columns('salaries', 'N/A', 'season', 2016)
assert_equal(players, """
player_id | wOBA | salaries | season
110,234 | 0.354 | N/A | 2,016
110,235 | 0.236 | N/A | 2,016
player_id | wOBA | salaries | season
110234 | 0.354 | N/A | 2016
110235 | 0.236 | N/A | 2016
""")
salaries = Table().with_column('salary', make_array('$500,000', '$15,500,000'))
players = players.with_columns('salaries', salaries.column('salary'), 'years', make_array(6, 1))
assert_equal(players, """
player_id | wOBA | salaries | season | years
110,234 | 0.354 | $500,000 | 2,016 | 6
110,235 | 0.236 | $15,500,000 | 2,016 | 1
player_id | wOBA | salaries | season | years
110234 | 0.354 | $500,000 | 2016 | 6
110235 | 0.236 | $15,500,000 | 2016 | 1
""")

def test_with_columns_with_formats():
players = Table().with_columns('player_id', make_array(110234, 110235), 'wOBA', make_array(.354, .236))
assert_equal(players, """
player_id | wOBA
110,234 | 0.354
110,235 | 0.236
110234 | 0.354
110235 | 0.236
""")
players = players.with_columns('salaries', 'N/A', 'season', 2016)
assert_equal(players, """
player_id | wOBA | salaries | season
110,234 | 0.354 | N/A | 2,016
110,235 | 0.236 | N/A | 2,016
player_id | wOBA | salaries | season
110234 | 0.354 | N/A | 2016
110235 | 0.236 | N/A | 2016
""")
salaries = Table().with_column('salary', make_array(500000, 15500000))
players2 = players.with_columns('salaries', salaries.column('salary'), 'years', make_array(6, 1), formatter=CurrencyFormatter)
assert_equal(players2, """
player_id | wOBA | salaries | season | years
110,234 | 0.354 | $500,000 | 2,016 | $6
110,235 | 0.236 | $15,500,000 | 2,016 | $1
player_id | wOBA | salaries | season | years
110234 | 0.354 | $500,000 | 2016 | $6
110235 | 0.236 | $15,500,000 | 2016 | $1
""")

with(pytest.raises(Exception)):
Expand Down Expand Up @@ -920,23 +938,23 @@ def test_relabel():
table.relabel('id', 'todo')
assert_equal(table, """
points | todo
1 | 12,345
1 | 12345
2 | 123
3 | 5,123
3 | 5123
""")
table.relabel(1, 'yolo')
assert_equal(table, """
points | yolo
1 | 12,345
1 | 12345
2 | 123
3 | 5,123
3 | 5123
""")
table.relabel(['points', 'yolo'], ['red', 'blue'])
assert_equal(table, """
red | blue
1 | 12,345
1 | 12345
2 | 123
3 | 5,123
3 | 5123
""")
with(pytest.raises(ValueError)):
table.relabel(['red', 'blue'], ['magenta', 'cyan', 'yellow'])
Expand Down

0 comments on commit 1b74371

Please sign in to comment.