Alexa Halford
Alexa Halford

Reputation: 1273

Can't figure out what's wrong with data type in numpy loadtxt command

I'm trying to read in an Ascii file using loadtxt. The file looks like this

UT, L, R, LocT, MLT, MLAT
      240000      1.03033      1.06433      2.73627      2.93244      8.51725
      300000      1.01964      1.05914      3.07449      3.24764      6.54548
      360000      1.01194      1.05747      3.41200      3.56224      4.51283
      420000      1.00746      1.05935      3.74672      3.87489      2.44624
      480000      1.00702      1.06476      4.07669      4.18431     0.373423

However there can be at least 9 characters in any of the rows.

I've been using this code

posdata = np.loadtxt(denfile, dtype={'names':('UT', 'L', 'R', 'loct', 'MLT', 'Mlat'), 'formats':('I9', 'f9', 'f9', 'f9', 'f9', 'f9')} , skiprows = 1)

and I get an error which reads TypeError: data type not understood. When I use a lower case i I get the same error. However in the line above where I read in a different file if the i is lowercase it doesn't work, but if it's upper case it does.

I'm not sure where the error is occurring or how to fix it. Any ideas would be greatly appreciated.

Upvotes: 1

Views: 4295

Answers (1)

Joe Kington
Joe Kington

Reputation: 284552

There's no such thing as a 72-bit float in numpy.

Either specify 'f8'/'I8' or for easier readibility: np.float/np.uint. There's no 'f9' (which would be a 72-bit float).

Have a look at the documentation for defining a dtype in numpy.

For your case you probably don't need to bother with this, though.

If you don't really need things as a structured array, then don't use one. (If you don't know what a structured array is, you probably don't need it in this case.)

Just do data = np.loadtxt("datafile.txt", skiprows=1). If you do need a structured array, then consider doing data = np.genfromtxt("datafile.txt", names=True). For simple cases, it's easier to cast the first column as an unsigned integer later, rather than explicitly defining a dtype.

Upvotes: 4

Related Questions