首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >df_dates = df.loc[:,'Date'] (来自雅虎Fin的数据)无法读取“日期”列

df_dates = df.loc[:,'Date'] (来自雅虎Fin的数据)无法读取“日期”列
EN

Stack Overflow用户
提问于 2019-11-08 15:12:49
回答 2查看 2K关注 0票数 2

我目前正在从雅虎金融获得数据,为了我的代码目的,我需要公开日期和开放变量。

代码语言:javascript
复制
df_dates = df.loc[:,'Date'] # Get all of the rows from the Date column
df_open = df.loc[:,'Open'] #Get all of the rows from the Open column

然而,据我所知,日期并不像一列,但我们可以看到有一列叫做“日期”。

我是这样从雅虎得到更新的;

代码语言:javascript
复制
df = pdr.get_data_yahoo("AMD", start ='2019-10-01')

在我处决之后;

代码语言:javascript
复制
df_dates = df.loc[:,'Date'] # Get all of the rows from the Date column
df_open = df.loc[:,'Open'] #Get all of the rows from the Open column

这就是我所犯的错误。

代码语言:javascript
复制
--------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2896             try:
-> 2897                 return self._engine.get_loc(key)
   2898             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Date'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
8 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2897                 return self._engine.get_loc(key)
   2898             except KeyError:
-> 2899                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2900         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2901         if indexer.ndim > 1 or indexer.size > 1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Date'

如何定位列“日期”?

最好的

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-11-08 15:20:57

代码语言:javascript
复制
from pandas_datareader import data as pdr
df = pdr.get_data_yahoo("AMD", start ='2019-10-01')

检查列和索引:

代码语言:javascript
复制
df.columns

输出:

代码语言:javascript
复制
Index(['High', 'Low', 'Open', 'Close', 'Volume', 'Adj Close'], dtype='object')

df.index

输出

代码语言:javascript
复制
DatetimeIndex(['2019-10-01', '2019-10-02', '2019-10-03', '2019-10-04',
               '2019-10-07', '2019-10-08', '2019-10-09', '2019-10-10',
               '2019-10-11', '2019-10-14', '2019-10-15', '2019-10-16',
               '2019-10-17', '2019-10-18', '2019-10-21', '2019-10-22',
               '2019-10-23', '2019-10-24', '2019-10-25', '2019-10-28',
               '2019-10-29', '2019-10-30', '2019-10-31', '2019-11-01',
               '2019-11-04', '2019-11-05', '2019-11-06', '2019-11-07',
               '2019-11-08'],
              dtype='datetime64[ns]', name='Date', freq=None)

如您所见,Date属于索引,因此不能通过它的标签来选择它。直接在读取数据帧上使用columns可能不方便,因为的其余部分不再具有 Date 索引。使用Index.to_frame,然后使用DataFrame.reset_index

代码语言:javascript
复制
df_Open=df[['Open']] #this returns a DataFrame, you don't need loc
df_dates=df.index.to_frame().reset_index(drop=True)
print(df_dates)

         Date
0  2019-10-01
1  2019-10-02
2  2019-10-03
3  2019-10-04
4  2019-10-07
5  2019-10-08
6  2019-10-09
7  2019-10-10
8  2019-10-11
9  2019-10-14
10 2019-10-15
11 2019-10-16
12 2019-10-17
13 2019-10-18
14 2019-10-21
15 2019-10-22
16 2019-10-23
17 2019-10-24
18 2019-10-25
19 2019-10-28
20 2019-10-29
21 2019-10-30
22 2019-10-31
23 2019-11-01
24 2019-11-04
25 2019-11-05
26 2019-11-06
27 2019-11-07
28 2019-11-08
票数 3
EN

Stack Overflow用户

发布于 2019-11-08 15:18:16

这是因为Date是数据的索引,而不是列中的索引。使用df.index访问它。

此外,您还可以执行一个df.reset_index(),然后访问它。所以用你的代码:

代码语言:javascript
复制
df = pdr.get_data_yahoo("AMD", start ='2019-10-01').reset_index()

df_dates = df.loc[:,'Date'] # Get all of the rows from the Date column
df_open = df.loc[:,'Open'] #Get all of the rows from the Open column
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58769202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档