开发者

How can I combine two fields into one when importing from a file into numpy array?

I have a data file which I am reading into a numpy array which looks like the following.

#RIC,Date[G],Time[G],GMT Offset,Type,Open,High,Low,Last,Volume
ADH0,20100103,22:18:00.000,-6,Intraday 1Min,0.8915,0.8915,0.8915,0.8915,0
ADH0,20100103,22:22:00.000,-6,Intraday 1Min,0.89,0.89,0.89,0.89,0

I am reading it using the np.genfromtxt() function as follows:

a = np.genfromtxt(f, names=True, delimiter=',', dtype="|S8,i4,|S12,f8", usecols=(0, 1, 2, 8), autostrip=True)

All is fine, but I would like to combine the date and time fields into one datetime column in my array instead of separate columns. I can do the individual field conversion using a converter function, but I can't开发者_运维知识库 see a way of combining the two separate date and time fields into one datetime. Can this be done?

Thanks, Jon


Not directly, it might be easier if you just slice'n'dice the csv file before loading, for example with a very stupid script like this:

gawk -F, '{print $1","$2"_"$3","$4","$5","$6","$7","$8","$9","$10}' input.csv

This will combine field 2 and 3 with an underscore, and you can use a timestamp stringparser on it.


Looking at the documentation, I don't think there is a way to do this from within np.genfromtxt. Your best bet is probably to read in the data as you are currently doing, and then create a new array that combines the two columns as a later step.


If you aren't concerned about speed, this is a fairly direct way, albeit an eyeful:

raw_csv = csv.reader(open('file'))
joined_columns = np.array([[[i[0]]+[str(i[1])+'sep_string'+str(i[2])]+i[3:]]\
                 for i in raw_csv])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜