1

How would I convert the string 2014年05月19日 to a datetime object in Python if I'm reading it from a csv file? Currently when I specify dtype=None, it reads in 2014年05月19日 as a string, not a datetime.

import numpy as np
import datetime
np.genfromtxt(inputFile, dtype=None,delimiter=' ')

File

2014年05月19日 10
2014年05月20日 11
2014年05月21日 12
2014年05月22日 13.29
2014年05月23日 12.1

where the number after the string is a value associated with the date but not included in the datetime object

dataPoints = np.loadtxt(inputFile, dtype=None,delimiter=' ', converters = {0: datetime.datetime.strptime('%Y-%m-%d')})

I receive the following message: TypeError: strptime() takes exactly 2 arguments (1 given)

how do I specify the format without actually stripping the string?

asked May 28, 2014 at 13:32
2
  • what is the number after the space? Commented May 28, 2014 at 13:37
  • Why do you have to use numpy? why not open the csv, split by comma, and strptime? Commented May 28, 2014 at 15:21

2 Answers 2

5

If you don't have missing values you can use numpy.loadtxt().

numpy.genfromtxt() also has the converters param. (thx RickyA)

Using 'coverters' param and lambda function:

from datetime import datetime
datestr2num = lambda x: datetime.strptime(x, '%Y-%m-%d')
np.genfromtxt(inputFile, delimiter=' ', converters = {0: datestr2num})

Where 0 is column index.

There is simpler way - use dtype param:

np.genfromtxt(inputFile, dtype=['datetime64[D]', float], delimiter=' ')

or more human readable using ('column name', type) tuple:

np.genfromtxt(inputFile, dtype=[('Date', 'datetime64[D]'),('Value', float)], delimiter=' ')

BTW IMHO Load string as datetime in numpy would be more accurate title.

answered May 28, 2014 at 13:46
Sign up to request clarification or add additional context in comments.

7 Comments

but will this try to convert my associated value to a date as well?
I just edited. You indicate which column will be converted by index. So the answer is no :-)
BTW. genfromtxt also has the converters param
@PiotrNawrot also, would datestr2num = '%Y-%m-%d' suffice?
@PiotrNawrot I'm still just a little confused by the datestr2num part, thanks.
|
0

You should use the strptime function from the datetime module. I'm not sure what the numbers after the space should be, but this will format the year-month-day correctly into a datetime object.

from datetime import datetime
some_date = '2014-05-28'
parsed_date = datetime.strptime(some_date, '%Y-%m-%d')
print(parsed_date)
answered May 28, 2014 at 13:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.