Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>By default, <code>np.genfromtxt</code> uses <code>dtype=float</code>: that's why you string columns are converted to NaNs because, after all, they're Not A Number... </p> <p>You can ask <code>np.genfromtxt</code> to try to guess the actual type of your columns by using <code>dtype=None</code>:</p> <pre><code>&gt;&gt;&gt; from StringIO import StringIO &gt;&gt;&gt; test = "a,1,2\nb,3,4" &gt;&gt;&gt; a = np.genfromtxt(StringIO(test), delimiter=",", dtype=None) &gt;&gt;&gt; print a array([('a',1,2),('b',3,4)], dtype=[('f0', '|S1'),('f1', '&lt;i8'),('f2', '&lt;i8')]) </code></pre> <p>You can access the columns by using their name, like <code>a['f0']</code>...</p> <p>Using <code>dtype=None</code> is a good trick if you don't know what your columns should be. If you already know what type they should have, you can give an explicit <code>dtype</code>. For example, in our test, we know that the first column is a string, the second an int, and we want the third to be a float. We would then use</p> <pre><code>&gt;&gt;&gt; np.genfromtxt(StringIO(test), delimiter=",", dtype=("|S10", int, float)) array([('a', 1, 2.0), ('b', 3, 4.0)], dtype=[('f0', '|S10'), ('f1', '&lt;i8'), ('f2', '&lt;f8')]) </code></pre> <p>Using an explicit <code>dtype</code> is much more efficient than using <code>dtype=None</code> and is the recommended way. </p> <p>In both cases (<code>dtype=None</code> or explicit, non-homogeneous <code>dtype</code>), you end up with a structured array. </p> <p>[Note: With <code>dtype=None</code>, the input is parsed a second time and the type of each column is updated to match the larger type possible: first we try a bool, then an int, then a float, then a complex, then we keep a string if all else fails. The implementation is rather clunky, actually. There had been some attempts to make the type guessing more efficient (using regexp), but nothing that stuck so far]</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload