1

I am new to Python (converting from Matlab) and I am having trouble reading a CSV file. Basically, I want create an array with the sequence of date/times in one variable and the corresponding data values (humidity measurements) in another variable (I want to skip the 2nd column). The file format is as follows:

07-24-14 01:00:01 PM,%RH,36.988
07-24-14 02:00:01 PM,%RH,40.832
...

I am using the numpy loadtxt function as follows (note: there are 21 headerlines in the file):

def datestr2num(s):
    return datetime.strptime(s,'%m-%d-%y %I:%M:%S %p')

dates,vals = np.loadtxt('File.csv',usecols=(0,2),skiprows=21,converters={0:datestr2num},delimiter=',',unpack=True)

I am getting the following error:

TypeError: float() argument must be a string or a number

Thanks for your help in advance!

1 Answer 1

1

An alternate approach would be to use a float time representation. I often use matplotlib time stamps:

from matplotlib.dates import date2num

def datestr2num(s):
    return date2num(datetime.strptime(s, '%m-%d-%y %I:%M:%S %p'))

Or you could use the python built-in 'time':

import time

def datestr2num(s):
    return time.mktime(datetime.strptime(s, '%m-%d-%y %I:%M:%S %p').timetuple())

If you'll be using one of these representations for your time anyway, this might work well. If you want datetimes, use dano's solution.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.