Skip to content

Commit 27244c4

Browse files
authored
Merge pull request #7536 from parejkoj/create-empty-table
Add example on creating empty table and filling it
2 parents b99bf74 + bfad750 commit 27244c4

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

‎docs/table/construct_table.rst‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,37 @@ To create a copy of an existing table that is empty (has no rows)::
471471
float64 float64
472472
------- -------
473473

474+
Empty array of a known size
475+
---------------------------
476+
477+
If you do know the size your table will be, but don't know the values in
478+
advance, you can create a zeroed numpy array and build the astropy table from
479+
it::
480+
481+
>>> N = 3
482+
>>> dtype = [('a', 'i4'), ('b', 'f8'), ('c', 'bool')]
483+
>>> t = Table(data=np.zeros(N, dtype=dtype))
484+
>>> t
485+
<Table length=3>
486+
a b c
487+
int32 float64 bool
488+
----- ------- -----
489+
0 0.0 False
490+
0 0.0 False
491+
0 0.0 False
492+
493+
For example, you can then fill in this table row-by-row from extracted from another table, or generated on the fly::
494+
495+
>>> for i in range(len(t)):
496+
... t[i] = (i, 2.5*i, i % 2)
497+
>>> t
498+
<Table length=3>
499+
a b c
500+
int32 float64 bool
501+
----- ------- -----
502+
0 0.0 False
503+
1 2.5 True
504+
2 5.0 False
474505

475506
Initialization Details
476507
======================

0 commit comments

Comments
 (0)