Skip to content

Commit

Permalink
Merge pull request #466 from krisstee/master
Browse files Browse the repository at this point in the history
Issue #163: Included documentation for to_array method.
  • Loading branch information
adnanhemani committed Oct 3, 2020
2 parents 079150f + 1fc559a commit 1ad29a2
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion datascience/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2662,7 +2662,40 @@ def to_csv(self, filename):
self.to_df().to_csv(filename, index=False)

def to_array(self):
"""Convert the table to a structured NumPy array."""
"""Convert the table to a structured NumPy array.
The resulting array contains a sequence of rows from the table.
Args:
None
Returns:
arr: a NumPy array
The following is an example of calling to_array()
>>> t = Table().with_columns([
... 'letter', ['a','b','c','z'],
... 'count', [9,3,3,1],
... 'points', [1,2,2,10],
... ])
>>> t
letter | count | points
a | 9 | 1
b | 3 | 2
c | 3 | 2
z | 1 | 10
>>> example = t.to_array()
>>> example
array([('a', 9, 1), ('b', 3, 2), ('c', 3, 2), ('z', 1, 10)],
dtype=[('letter', '<U1'), ('count', '<i8'), ('points', '<i8')])
>>> example['letter']
array(['a', 'b', 'c', 'z'],
dtype='<U1')
"""
dt = np.dtype(list(zip(self.labels, (c.dtype for c in self.columns))))
arr = np.empty_like(self.columns[0], dt)
for label in self.labels:
Expand Down

0 comments on commit 1ad29a2

Please sign in to comment.