python读取日期数据的问题
伊谢尔伦
伊谢尔伦 2017-04-18 10:04:41
0
2
580

读取sh600004.csv文件,第二列是时间,格式是年-月-日。下面的代码变量ddate存储读出的时间。

import numpy as np
import matplotlib.dates as mdates

ddate,dopen = np.loadtxt('sh600004.csv', delimiter=',', skiprows=1,converters={0:mdates.strpdate2num('%Y-%m-%d')}, usecols= (1,2), unpack=True)

报错信息:

ValueError: could not convert string to float: b'2014-12-31'
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
刘奇

E.g., if column 0 is a date string: converters = {0: datestr2num}

According to the instructions of the numpy library, if the second column is a date, your parameter should be converters={1:mdates.strpdate2num('%Y-%m-%d')}

迷茫

python3

import numpy as np
import matplotlib.dates as mdates

data = '''code date open high low close change volume money traded_market_value market_value turnover adjust_price report_type report_date PE_TTM PS_TTM PC_TTM PB
sh600004 2014/12/31 10.8 10.94 10.7 10.93 0.013915 19707899 213624420 12569500000 12569500000 0.017137303 12.13 2014/9/30 00:00 2014/10/30 00:00 12.88897481 2.317142523 7.472838539 1.561138066
sh600004 2014/12/30 11.18 11.26 10.65 10.78 -0.041778 25075183 273427007 12397000000 12397000000 0.021804507 11.96 2014/9/30 00:00 2014/10/30 00:00 12.71208613 2.285341989 7.370281078 1.53971296
'''

open('sh600004.csv','w').write(data)
ddate,dopen =  np.loadtxt('sh600004.csv', delimiter=' ', skiprows=1,
                          converters={1:mdates.bytespdate2num('%Y/%m/%d')},
                          usecols= (1,2), unpack=True)

print((ddate,dopen))
(array([ 735598.,  735597.]), array([ 10.8 ,  11.18]))
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template