【问题标题】:How to set an index within multiindex for datetime?如何在多索引中为日期时间设置索引?
【发布时间】:2019-02-21 04:56:14
【问题描述】:

我有这个 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: '日期'

知道如何保留 2 个索引,日期 + 符号,但要更改其中一个(日期)tp 日期时间,以便我可以得到星期几?

【问题讨论】:

  • 试试 df.index.levels[0] = pd.to_datetime(df.index.levels[0])
  • TypeError: 'FrozenList' 不支持可变操作。

标签: pandas


【解决方案1】:

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

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 2016-10-03
    • 2019-09-16
    • 2023-01-13
    • 1970-01-01
    • 2022-01-16
    • 2016-10-05
    相关资源
    最近更新 更多