Python – The Numpy.genfromtxt method works for Windows but not Linux

The Numpy.genfromtxt method works for Windows but not Linux… here is a solution to the problem.

The Numpy.genfromtxt method works for Windows but not Linux

I’m doing some data processing, I’ve built a program for Python in Windows, and now I want to run it on my Linux machine so it can be processed when I go home for beer etc.

A piece of code (an important one) extracts some column values from a CSV file through Numpy’s genfromtxt method. The code snippet in question is:

rfd_values = np.genfromtxt(file_in, delimiter=',',
             skip_header=1,
             invalid_raise=0,
             usecols = cols)

So the idea here is, skip the header, the

separator is a comma, and just give me the columns in the list named cols. This works fine on my Windows laptop (same versions of Python and Numpy, 2.6 and 1.5, respectively), but when I run it in Linux, it tells me:

*TypeError: genfromtxt() gets an unexpected keyword argument ‘skip_header’*

I tried putting everything in one line and changing the quotes around the delimiter keyword, but that doesn’t seem to work. It might be a little silly, but I can’t seem to understand it. I went through a bunch of forums and numpy docs, but didn’t see anything that sounded close to what I saw. I wonder what I’m missing.

I would appreciate any insights.

Thanks in advance!

– Jeff

Solution

You say you’re actually using version 1.3 on Linux. It has one parameter, skiprows, which is the same as skip_header. Considering numpy.genfromtxt isn’t even in the documentation for 1.3, I’m guessing it’s only being tested in 1.3 and the final signature hasn’t been fully determined yet. With that said, there is a workaround for your situation. You can use the names=True keyword argument instead. In this case, the first row will not be used for the data, but for determining the column name (which you can then use instead of the column number in the list passed as usecols).

But there is one more problem. invalid_raise parameter is also not in 1.3.

Related Problems and Solutions