<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jun 16, 2015 at 6:07 PM, Gregory Simonian <span dir="ltr"><<a href="mailto:simonian.7@buckeyemail.osu.edu" target="_blank">simonian.7@buckeyemail.osu.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  

    
  
  <div bgcolor="#FFFFFF" text="#000000">
    <font face="Liberation Sans">Hi all,<br>
      <br>
      I have a Table with several columns which were read in as strings
      because they had a combination of numeric and non-numeric
      characters. After cleaning out the non-numeric characters, I'd
      like to convert the dtype of the column from string to int. <br>
      <br>
      <font face="FreeSans">Looking at the astropy documentation for
        modifying tables [0], under the caveats section it states:<br>
        <br>
        "</font></font><font face="Liberation Sans"><font face="FreeSans">The data type for a column cannot be changed in
        place. In order to do this
        you must make a copy of the table with the column type changed
        appropriately."<br>
        <br>
        So I try making a new table (the table is small, so I don't mind
        the temporary memory hit), but I run into the error:<br>
        "TypeError: Cannot set fill value of string with array of dtype
        int64"<br>
        When checking the fill value of the column (which is masked from
        the reader), it's 'N/A', which makes sense that it wouldn't
        work. But when I change it to '0', I still get the same problem.
        I managed to find a minimal working example as:<br>
      </font><br>
      <font face="FreeMono">import numpy as np<br>
        t = Table({'a': ['1']}, masked=True)<br>
        t['a'].set_fill_value('0')<br>
        u = Table(t, names=['a'], dtype=[<a href="http://np.int" target="_blank">np.int</a>])<br>
        > TypeError: Cannot set fill value of string with array of
        dtype int64</font><br>
      <br>
      <font face="FreeSans">When debugging the exception, it seems like
        the Table constructor doesn't convert the fill value of masked
        tables when generating a new table, which seems to defeat the
        ability to change the dtype of masked columns. I was wondering
        if other people have run by this, and if there's a more
        straightforward way of changing the dtype of a column. This is
        with Astropy 1.0.3 on Python 2.7.10 through Anaconda on CentOS
        6.6.<br>
        <br>
        When checking to see if this was reported, I ran into issue
        #3809 [1], which might end up being the ultimate solution to my
        problem. It seems like a workaround would be:</font><br>
      <font face="FreeMono"><br>
        temp_column = np.ma.asanyarray(t['a'], <a href="http://np.int" target="_blank">np.int</a>)<br>
        del(t['a'])<br>
        t['a'] = temp_column</font><br>
      <br></font></div></blockquote><div><br></div><div>Hi Gregory,</div><div><br></div><div>Sorry, your nice bug report fell through the cracks and didn't get a response.  You've basically understood the issues correctly, and I just put in a pull request that fixes this:</div><div><br></div><div><a href="https://github.com/astropy/astropy/pull/4088/files">https://github.com/astropy/astropy/pull/4088/files</a><br></div><div><br></div><div>In your workaround you could have done something slightly simpler:</div><div><br></div><div> temp_column = t['a'].astype(<a href="http://np.int">np.int</a>)</div><div><br></div><div>Cheers,</div><div>Tom</div><div><br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div bgcolor="#FFFFFF" text="#000000"><font face="Liberation Sans">
      <font face="FreeSans">Thanks!<br>
        <br>
                                                    Gregory Simonian<br>
        <br>
        [0]</font></font><font face="FreeSans">
      <a href="http://docs.astropy.org/en/stable/table/modify_table.html#caveats" target="_blank">http://docs.astropy.org/en/stable/table/modify_table.html#caveats</a><br>
      [1] <a href="https://github.com/astropy/astropy/issues/3809" target="_blank">https://github.com/astropy/astropy/issues/3809</a></font>
  </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" rel="noreferrer" target="_blank">http://mail.scipy.org/mailman/listinfo/astropy</a><br>
<br></blockquote></div><br></div></div>