我有一个时间序列csv文件,包含时间戳和财务数据,如下所示:
guard let userChoice = ImageChoice(rawValue: someInt) else { return //Default Image }
let image = userChoice.image
现在,我想将其放入20140804:10:00:13.281486,782.83,443355
20140804:10:00:13.400113,955.71,348603
,并在我阅读pandas.DataFrame
时将日期解析为yyyymmddhhmmss
。我搜索了线程,我看到有人使用csv
模块,但我对Python很新,所以我不确定如何使用该模块来解析上述数据,并且在我阅读datetime
的同时完成这一切。
如何最好地解决这个问题?
答案 0 :(得分:2)
你需要:
没有csv标题:
import pandas as pd
from pandas.compat import StringIO
temp=u"""
20140804:10:00:13.281486,782.83,443355
20140804:10:00:13.400113,955.71,348603"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp),
#parse first columns
parse_dates=[0],
#custom parse function
date_parser = lambda x: pd.datetime.strptime(x, '%Y%m%d:%H:%M:%S.%f'),
#no header of csv
header=None)
print (df)
0 1 2
0 2014-08-04 10:00:13.281486 782.83 443355
1 2014-08-04 10:00:13.400113 955.71 348603
print (df.dtypes)
0 datetime64[ns]
1 float64
2 int64
dtype: object
csv的标题
import pandas as pd
from pandas.compat import StringIO
temp=u"""dates,a,b
20140804:10:00:13.281486,782.83,443355
20140804:10:00:13.400113,955.71,348603"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp),
parse_dates=[0],
date_parser = lambda x: pd.datetime.strptime(x, '%Y%m%d:%H:%M:%S.%f'))
print (df)
dates a b
0 2014-08-04 10:00:13.281486 782.83 443355
1 2014-08-04 10:00:13.400113 955.71 348603
print (df.dtypes)
dates datetime64[ns]
a float64
b int64
dtype: object