找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 3847|回复: 1

[求助] 萌新提问,为什么我copy来的代码一跑就出错?

1

主题

1

帖子

1

积分

贫民

积分
1
Plinius 发表于 2017-10-16 16:29:20 | 显示全部楼层 |阅读模式
小白想自学python做量化,看到网络上有位老师的现成代码如下:
stock_data = pd.read_csv('/Python/data/code_data/data/stock_data_sample.csv',
                         encoding='gbk')  # 此处填入数据在本地的路径

stock_data.columns = [i.encode("utf8") for i in stock_data.columns]

stock_data['交易日期'] = pd.to_datetime(stock_data['交易日期'])
....后面部分省略
我复制一模一样的代码
一跑程序,出现下列错误,求高手给出原因!!!
C:\Python\Python36\python.exe C:/Python/data/code_data/code/select_stocks.py
Traceback (most recent call last):
  File "C:\Python\Python36\lib\site-packages\pandas\core\indexes\base.py", line 2442, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5280)
  File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5126)
  File "pandas\_libs\hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20523)
  File "pandas\_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20477)
KeyError: '交易日期'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Python/data/code_data/code/select_stocks.py", line 12, in <module>
    stock_data['交易日期'] = pd.to_datetime(stock_data['交易日期'])
  File "C:\Python\Python36\lib\site-packages\pandas\core\frame.py", line 1964, in __getitem__
    return self._getitem_column(key)
  File "C:\Python\Python36\lib\site-packages\pandas\core\frame.py", line 1971, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Python\Python36\lib\site-packages\pandas\core\generic.py", line 1645, in _get_item_cache
    values = self._data.get(item)
  File "C:\Python\Python36\lib\site-packages\pandas\core\internals.py", line 3590, in get
    loc = self.items.get_loc(item)
  File "C:\Python\Python36\lib\site-packages\pandas\core\indexes\base.py", line 2444, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5280)
  File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5126)
  File "pandas\_libs\hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20523)
  File "pandas\_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20477)
KeyError: '交易日期'

回复

使用道具 举报

0

主题

1

帖子

1

积分

贫民

积分
1
我是张学友 发表于 2020-2-2 12:59:55 | 显示全部楼层
楼主有没有解决啊
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表