现有一个csv格式的数据文件,有两列,一列为日期,一列为数据,在网上搜了好久,形成以下一段代码。现在问题是运行时候报错,invalid literal for float(): 2016-07-06,问题应该在dtype='str'这个地方,不过实在不知道该怎么改,已经困扰两天了,还请大神指点!
import pandas as pd
import numpy as np
import pylab as pl
import matplotlib.dates as dt
data=np.loadtxt(r'd:\7000F.csv',delimiter=',',dtype='str' )
pl.plot(data)
pl.xlabel('x')
pl.ylabel('y')
#pl.xlim(0.0,2016-07-06)
pl.ylim(8000,10000)
pl.show()
4 个解决方案
#1
补充一下,我把原始CSV拆成2个后就可以了,不过怎么可以在不拆的情况下也运行呢?
import pandas as pd
import numpy as np
import pylab as pl
import matplotlib.dates as dt
from numpy import dtype
from numpy import datetime64
x=np.loadtxt(r'd:\7000F-X.csv',delimiter=',',dtype='datetime64[ns]' )
y=np.loadtxt(r'd:\7000F-Y.csv',delimiter=',',dtype='str' )
pl.plot(x,y)
pl.xlabel('time')
pl.ylabel('price')
#pl.xlim(0.0,2016-07-06)
pl.ylim(8000,10000)
pl.show()
import pandas as pd
import numpy as np
import pylab as pl
import matplotlib.dates as dt
from numpy import dtype
from numpy import datetime64
x=np.loadtxt(r'd:\7000F-X.csv',delimiter=',',dtype='datetime64[ns]' )
y=np.loadtxt(r'd:\7000F-Y.csv',delimiter=',',dtype='str' )
pl.plot(x,y)
pl.xlabel('time')
pl.ylabel('price')
#pl.xlim(0.0,2016-07-06)
pl.ylim(8000,10000)
pl.show()
#2
顶顶~~~~~~
#3
x = np.loadtxt(r'x1.csv',delimiter=',',usecols=(0,),dtype='datetime64[D]')
y = np.loadtxt(r'x1.csv',delimiter=',',usecols=(1,),dtype='str' )
#4
好像可以了,谢谢!
#1
补充一下,我把原始CSV拆成2个后就可以了,不过怎么可以在不拆的情况下也运行呢?
import pandas as pd
import numpy as np
import pylab as pl
import matplotlib.dates as dt
from numpy import dtype
from numpy import datetime64
x=np.loadtxt(r'd:\7000F-X.csv',delimiter=',',dtype='datetime64[ns]' )
y=np.loadtxt(r'd:\7000F-Y.csv',delimiter=',',dtype='str' )
pl.plot(x,y)
pl.xlabel('time')
pl.ylabel('price')
#pl.xlim(0.0,2016-07-06)
pl.ylim(8000,10000)
pl.show()
import pandas as pd
import numpy as np
import pylab as pl
import matplotlib.dates as dt
from numpy import dtype
from numpy import datetime64
x=np.loadtxt(r'd:\7000F-X.csv',delimiter=',',dtype='datetime64[ns]' )
y=np.loadtxt(r'd:\7000F-Y.csv',delimiter=',',dtype='str' )
pl.plot(x,y)
pl.xlabel('time')
pl.ylabel('price')
#pl.xlim(0.0,2016-07-06)
pl.ylim(8000,10000)
pl.show()
#2
顶顶~~~~~~
#3
x = np.loadtxt(r'x1.csv',delimiter=',',usecols=(0,),dtype='datetime64[D]')
y = np.loadtxt(r'x1.csv',delimiter=',',usecols=(1,),dtype='str' )
#4
好像可以了,谢谢!