|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
关于读取scv文件会出现第一个空格的问题
我在照着视频写带吗的时候出现这样一个情况# 画图
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.pylab as lab
import mplfinance as mpl
import os
# print(mpf.available_styles())
# 创建画图区域
# fig, axs = plt.subplots(2, 1, sharex=True)
# ax1, ax2 = axs
# 画K线图
code = input("请输入一个股票代码:") # 导入代码code
# 读取文件csv
if os.path.exists(f"C:/Users/Administrator/PycharmProjects/学习库/股票爬虫/{code}.csv"):
# 设置data,并且设置时间格式
data = pd.read_csv(f"C:/Users/Administrator/PycharmProjects/学习库/股票爬虫/{code}.csv").iloc[:30, :]
# k线图数据输入是按"开盘价", "收盘价", "最高价", "最低价"排序
# 数据列名必须为dete
data = data[["日期", "开盘价", "收盘价", "最高价", "最低价", "成交量"]]
data = data.rename(
columns={"日期": "Date","开盘价": "Open",
"收盘价": "Close",
"最高价": "High",
"最低价": "Lower",
"成交量": "Volume"
})
# 设置时间索引(必须)
data = data.set_index(["Date"])
print(data)
# 画蜡烛图
mpl.plot(data)
else:
print("请重新输入")
# 画柱状图
plt.show()
csv文件内容如下日期,股票代码,名称,收盘价,最高价,最低价,开盘价,前收盘,涨跌额,涨跌幅,换手率,成交量,成交金额,总市值,流通市值
2020-05-18,600000,浦发银行,10.32,10.43,10.25,10.28,10.28,0.04,0.3891,0.0558,15675941,162140715.0,3.02913469697e+11,2.90030843438e+11
2020-05-15,600000,浦发银行,10.28,10.38,10.26,10.36,10.3,-0.02,-0.1942,0.0489,13753770,141846602.0,3.01739386481e+11,2.88906692882e+11
2020-05-14,600000,浦发银行,10.3,10.36,10.27,10.32,10.38,-0.08,-0.7707,0.0565,15892376,163558192.0,3.02326428089e+11,2.8946876816e+11
2020-05-13,600000,浦发银行,10.38,10.39,10.28,10.31,10.34,0.04,0.3868,0.0554,15574310,160783648.0,3.04674594521e+11,2.91717069272e+11
2020-05-12,600000,浦发银行,10.34,10.48,10.34,10.44,10.43,-0.09,-0.8629,0.0691,19405945,201474588.0,3.03500511305e+11,2.90592918716e+11
2020-05-11,600000,浦发银行,10.43,10.54,10.42,10.44,10.44,-0.01,-0.0958,0.0764,21472495,224819916.0,3.06142198541e+11,2.93122257467e+11
2020-05-08,600000,浦发银行,10.44,10.49,10.4,10.44,10.39,0.05,0.4812,0.0745,20927670,218698514.0,3.06435719345e+11,2.93403295106e+11
2020-05-07,600000,浦发银行,10.39,10.46,10.37,10.45,10.46,-0.07,-0.6692,0.0933,26211656,272516386.0,3.04968115325e+11,2.91998106911e+11
2020-05-06,600000,浦发银行,10.46,10.49,10.36,10.44,10.63,-0.17,-1.5992,0.1472,41378362,431336740.0,3.07022760953e+11,2.93965370384e+11
2020-04-30,600000,浦发银行,10.63,10.79,10.56,10.58,10.61,0.02,0.1885,0.11,30906971,330157488.0,3.1201261462e+11,2.98743010246e+11
2020-04-29,600000,浦发银行,10.61,10.61,10.33,10.35,10.33,0.28,2.7106,0.1247,35056836,367739389.0,3.11425573012e+11,2.98180934968e+11
2020-04-28,600000,浦发银行,10.33,10.38,10.22,10.36,10.34,-0.01,-0.0967,0.104,29222578,301584635.0,3.03206990501e+11,2.90311881077e+11
2020-04-27,600000,浦发银行,10.34,10.42,10.15,10.15,10.06,0.28,2.7833,0.1791,50342806,520243997.0,3.03500511305e+11,2.90592918716e+11
2020-04-24,600000,浦发银行,10.06,10.09,10.01,10.06,10.06,0.0,0.0,0.0604,16973113,170599170.0,2.95281928794e+11,2.82723864824e+11
2020-04-23,600000,浦发银行,10.06,10.1,10.01,10.1,10.07,-0.01,-0.0993,0.0601,16878643,169579075.0,2.95281928794e+11,2.82723864824e+11
2020-04-22,600000,浦发银行,10.07,10.07,9.98,10.02,10.03,0.04,0.3988,0.0562,15802441,158372535.0,2.95575449598e+11,2.83004902463e+11
2020-04-21,600000,浦发银行,10.03,10.12,10.01,10.07,10.11,-0.08,-0.7913,0.0625,17557525,176508085.0,2.94401366382e+11,2.81880751907e+11
当我读取第一个文件之后出现,date列下沉一个单元格,其他的列保存完整,我不知道这是不是影响我代码运行的原因,请大神帮我解决这个问题,谢谢啦>>>
================ RESTART: C:/Users/Administrator/Desktop/股票爬虫.py ===============
请输入一个股票代码:600000
Open Close High Lower Volume
Date
2020-05-18 10.28 10.32 10.43 10.25 15675941
2020-05-15 10.36 10.28 10.38 10.26 13753770
2020-05-14 10.32 10.30 10.36 10.27 15892376
2020-05-13 10.31 10.38 10.39 10.28 15574310
2020-05-12 10.44 10.34 10.48 10.34 19405945
2020-05-11 10.44 10.43 10.54 10.42 21472495
2020-05-08 10.44 10.44 10.49 10.40 20927670
2020-05-07 10.45 10.39 10.46 10.37 26211656
2020-05-06 10.44 10.46 10.49 10.36 41378362
2020-04-30 10.58 10.63 10.79 10.56 30906971
2020-04-29 10.35 10.61 10.61 10.33 35056836
2020-04-28 10.36 10.33 10.38 10.22 29222578
2020-04-27 10.15 10.34 10.42 10.15 50342806
2020-04-24 10.06 10.06 10.09 10.01 16973113
2020-04-23 10.10 10.06 10.10 10.01 16878643
2020-04-22 10.02 10.07 10.07 9.98 15802441
2020-04-21 10.07 10.03 10.12 10.01 17557525
2020-04-20 10.15 10.11 10.19 10.09 23503715
2020-04-17 10.18 10.20 10.23 10.15 22744411
2020-04-16 10.10 10.16 10.18 10.10 14403197
2020-04-15 10.20 10.18 10.27 10.15 14945608
2020-04-14 10.17 10.25 10.25 10.12 17792926
2020-04-13 10.15 10.11 10.22 10.10 11966986
2020-04-10 10.22 10.21 10.24 10.15 16625528
2020-04-09 10.26 10.20 10.29 10.18 13569586
2020-04-08 10.25 10.24 10.29 10.18 17952255
2020-04-07 10.30 10.28 10.33 10.23 23825611
2020-04-03 10.13 10.15 10.21 10.12 14842382
2020-04-02 10.09 10.20 10.20 10.07 17042135
2020-04-01 10.11 10.09 10.26 10.09 22479192
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/股票爬虫.py", line 32, in <module>
mpl.plot(data)
File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\mplfinance\plotting.py", line 46, in decorator
return func(*args, **kwargs)
File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\mplfinance\plotting.py", line 205, in plot
dates,opens,highs,lows,closes,volumes = _check_and_prepare_data(data, config)
File "C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages\mplfinance\_arg_validators.py", line 27, in _check_and_prepare_data
raise TypeError('Expect data.index as DatetimeIndex')
TypeError: Expect data.index as DatetimeIndex
|
|