<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Jun 26, 2013 at 8:05 AM, Brian Kloppenborg <span dir="ltr"><<a href="mailto:bkloppenborg@gmail.com" target="_blank">bkloppenborg@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
  
    
  
  <div text="#000000" bgcolor="#FFFFFF">
    Greetings,<br>
    <br>
    Today I noticed the astropy.table class (
    
    <a href="http://docs.astropy.org/en/latest/_generated/astropy.table.table.Table.html#astropy.table.table.Table" target="_blank">http://docs.astropy.org/en/latest/_generated/astropy.table.table.Table.html#astropy.table.table.Table</a>)
    implements many functions for manipulating tables:<br>
    add_column<br>
    add_columns<br>
    add_row<br>
    remove_column<br>
    remove_columns<br>
    <br>
    but it lacks similar functionality for row manipulation:<br>
    <br>
    add_rows - although this is easily accomplished by iteration<br>
    remove_row<br>
    remove_rows<br>
    <br>
    I could try manipulating the table._data structure directly, but
    because _data can be a numpy ndarray, dict, list or Table, it is not
    clear which method I should use without inspecting the data
    structure. <br>
    <br>
    I thought about slicing the table, but this would involve splitting
    then reassembling the table just to remove one row. I considered
    creating a mask, but this seems tedious (i.e. something that should
    be implemented via. a remove_row(s) function)<br>
    <br>
    Is a lack of a remove/delete_row(s) missing functionality or am I
    missing something obvious?<br></div></blockquote><div><br></div><div>I think this is missing functionality and just opened a feature request issue: </div><div><a href="https://github.com/astropy/astropy/issues/1205">https://github.com/astropy/astropy/issues/1205</a></div>

<div><br></div><div>In the meantime there are some slightly nicer solutions for doing these operations.  The current dev version of astropy the table package has a vstack function that lets you stack tables together.  So something like the following will work for add_rows, assuming you have a table named `dat` and some `rows`:</div>

<div><br></div><div>>>> from astropy.table import vstack, Table</div><div>>>> dat = vstack(dat, Table(rows))</div><div><br></div><div>In this case the Table() initialization is taking care of converting any valid inputs for `rows` into a nice table, and `vstack` is doing all the logic to ensure that the columns match up, etc.  Note that at this time you cannot directly initialize a Table from a list of row tuples.  This will require some extra keyword arg in the initialization to distinguish a list of rows from a list of columns, and we haven't settled on the best way yet.  The way around this is:</div>

<div><br></div><div><div>>>> rows = [[1, 2.0, 'string'],  # list of rows</div><div>           [2, 3.0, 'values']]</div><div>>>> col_arr = zip(*rows)  # transpose to a list of columns</div>

<div>>>> t = Table(col_arr)<br></div></div><div><br></div><div>For removing rows you just need a couple of extra lines of code:</div><div><br></div><div>>>> mask = np.ones(len(dat), dtype=bool)</div><div>

>>> mask[4] = False  # To remove one row</div><div>>>> mask[4:25] = False  # To remove a slice</div><div>>>> dat = dat[mask]</div><div><br></div><div>Cheers,</div><div>Tom</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<div text="#000000" bgcolor="#FFFFFF">
    <br>
    Thanks,<br>
    Brian<br>
  </div>

<br>_______________________________________________<br>
AstroPy mailing list<br>
<a href="mailto:AstroPy@scipy.org">AstroPy@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/astropy" target="_blank">http://mail.scipy.org/mailman/listinfo/astropy</a><br>
<br></blockquote></div><br></div></div>