如何在datein的multiindex中设置索引?

问题描述 投票:0回答:1

我有这个df:

                      open     high      low    close    volume
date       symbol                                              
2014-02-20 AAPL    69.9986  70.5252  69.4746  69.7569  76529103
           MSFT    33.5650  33.8331  33.4087  33.7259  27541038
2014-02-21 AAPL    69.9727  70.2061  68.8967  68.9821  69757247
           MSFT    33.8956  34.2619  33.8241  33.9313  38030656
2014-02-24 AAPL    68.7063  69.5954  68.6104  69.2841  72364950
           MSFT    33.6723  33.9269  33.5382  33.6723  32143395

从这里返回:

from datetime import datetime
from iexfinance.stocks import get_historical_data
from pandas_datareader import data
import matplotlib.pyplot as plt
import pandas as pd

start =  '2014-01-01'
end = datetime.today().utcnow()
symbol = ['AAPL', 'MSFT']

prices = pd.DataFrame()
datasets_test = []
for d in symbol:
    data_original = data.DataReader(d, 'iex', start, end)
    data_original['symbol'] = d
    prices = pd.concat([prices,data_original],axis=0)
prices = prices.set_index(['symbol'], append=True)
prices.sort_index(inplace=True)

当试图获得一周的一天:

A['day_of_week'] = features.index.get_level_values('date').weekday

我收到错误:

AttributeError:'Index'对象没有属性'weekday'

我试着将日期索引更改为日期时间

prices['date'] = pd.to_datetime(prices['date'])

但得到了这个错误:

KeyError:'date'

任何想法如何保留2个索引,日期+符号,但将其中一个(日期)更改为日期时间,以便我可以获得一周中的哪一天?

pandas
1个回答
1
投票

看起来索引的date级别包含字符串,而不是日期时间对象。一种解决方案是将所有MultiIndex级别重置为列,将date列转换为datetime,然后重新设置MultiIndex。然后你可以按常规方式继续使用像.weekday这样的pandas datetime访问器。

prices = prices.reset_index()
prices['date'] = pd.to_datetime(prices['date'])
prices = prices.set_index(['date', 'symbol'])

prices.index.get_level_values('date').weekday
Int64Index([3, 3, 4, 4, 0, 0, 1, 1, 2, 2,
            ...
            1, 1, 2, 2, 3, 3, 4, 4, 1, 1],
           dtype='int64', name='date', length=2516)
© www.soinside.com 2019 - 2024. All rights reserved.