Discussion:
[Numpy-discussion] Parsing a file with dates to datetim64
Florian Lindner
2016-09-26 15:17:37 UTC
Permalink
Hey,

I have a file:

;;;;;;;;;Eintrittsdatum;;;
;;;;;;;;;04.03.16;;10,00 €;genehmigt
;;;;;;;;;04.03.16;;10,00 €;genehmigt


which I try to parse using

def dateToNumpyDate(s):
s = s.decode("utf-8")
ret = datetime.datetime.strptime(s, "%d.%m.%y").isoformat()
return ret

def generateMembers():
members = np.genfromtxt("test_CSC_Mitglieder.csv",
dtype = { "names" : ["EntryDate"],
"formats" : ['datetime64[D]'] },
converters = { 9 : dateToNumpyDate },
skip_header = 1,
delimiter = ";",
usecols = (9))


count = members.shape[0]
y = np.linspace(1, count, count)
print(members)
print(members.dtype)
plt.plot(members["EntryDate"], y)
plt.show()


but the datatype was ignored homehow, <U19 instead of the datetime64

['2016-03-04T00:00:00' '2016-03-04T00:00:00']
<U19

CSC.py:76: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
plt.plot(members["EntryDate"], y)
Traceback (most recent call last):
File "CSC.py", line 82, in <module>
generateMembers()
File "CSC.py", line 76, in generateMembers
plt.plot(members["EntryDate"], y)
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are
valid indices


I also tried to print the return of dateToNumpbyData:

2016-03-04T00:00:00
2016-03-04T00:00:00
2016-03-04T00:00:00

Is there a problem with the dtype argument?

Thanks,
Florian

Loading...