Столбец «Дата» не может быть прочитан df_dates = df.loc [:, 'Date'] (данные получены из yahoo Fin) - PullRequest
1 голос
/ 08 ноября 2019

В настоящее время я получаю данные от Yahoo Finance, и для моего кода мне нужно найти переменные Date и Open.

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

Однако, как я понимаю, Date не похож на столбец, но мы можемсм. есть столбец с именем «Дата»

Я получаю обновление от Yahoo, как это;

df = pdr.get_data_yahoo("AMD", start ='2019-10-01')

и после выполнения;

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

Это ошибкаЯ получил.

--------------------------------------------------------------------------
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'

Как найти столбец «Дата»?

Best,

Ответы [ 2 ]

0 голосов
/ 08 ноября 2019
from pandas_datareader import data as pdr
df = pdr.get_data_yahoo("AMD", start ='2019-10-01')

Проверка столбцов и индекса:

df.columns

Вывод:

Index(['High', 'Low', 'Open', 'Close', 'Volume', 'Adj Close'], dtype='object')

df.index

Вывод

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 относится к индексу, и поэтому вы не можете выбрать его по его метке. Использование reset_index непосредственно на фрейме считанных данных может быть неудобным, поскольку остальные columns больше не имеют Date index . Используйте Index.to_frame, а затем DataFrame.reset_index

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
0 голосов
/ 08 ноября 2019

Это потому, что Date является индексом кадра данных, а не столбца. Получите доступ к нему, используя df.index.

Кроме того, вы можете сделать df.reset_index() и затем получить к нему доступ. Так и с вашим кодом:

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
...