2

使用 DatetimeIndex 处理 pandas 系列。期望的结果是一个数据框,其中包含 .loc[] 函数中指定的范围内的所有行。

当我尝试以下代码时:

aapl.index = pd.to_datetime(aapl.index)
print(aapl.loc[pd.Timestamp('2010-11-01'):pd.Timestamp('2010-12-30')])

我回来了:

Empty DataFrame
Columns: [Open, High, Low, Close, Volume, ExDividend, SplitRatio, 
AdjOpen, AdjHigh, AdjLow, AdjClose, AdjVolume]
Index: []

重申一下,我想要的结果是数据框的一个子集,包含范围内的所有行 (2010-11-01):(2010-12-30)。

4

3 回答 3

3

国际大学联盟:

import pandas_datareader as web
aapl = web.get_data_yahoo('aapl')

aapl.loc['2010-11-01':'2010-12-30']

使用部分字符串索引和切片。

于 2018-03-09T15:47:25.727 回答
1

似乎您需要将索引转换为datetime,然后使用标准索引/切片表示法。

import pandas as pd, numpy as np

df = pd.DataFrame(list(range(365)))

# these lines are for demonstration purposes only
df['date'] = pd.date_range('2010-1-1', periods=365, freq='D').astype(str)
df = df.set_index('date')

df.index = pd.to_datetime(df.index)

res = df[pd.Timestamp('2010-11-01'):pd.Timestamp('2010-11-10')]

#               0
# date           
# 2010-11-01  304
# 2010-11-02  305
# 2010-11-03  306
# 2010-11-04  307
# 2010-11-05  308
# 2010-11-06  309
# 2010-11-07  310
# 2010-11-08  311
# 2010-11-09  312
# 2010-11-10  313
于 2018-03-09T16:02:39.160 回答
0

出于好奇,我尝试将最近的日期作为选择的开始,将最近的日期作为结束。令我惊讶的是,这有效,但时间序列数据的顺序相反。

在:

aapl.loc[pd.Timestamp('2010-12-30'):pd.Timestamp('2010-11-01')]

所以......呃,我意识到我的时间序列数据必须是相反的顺序。现在的问题是,如何将 DatetimeIndex df 排序为正确的顺序?

所需的订单将第 n 个日期作为最后一行,最早的日期作为第一行。

******编辑******

aapl.index = pd.to_datetime(aapl.index)
aapl =  aapl.sort_index(ascending=True)

aaplrange = aapl.loc[pd.Timestamp('2010-11-01'):pd.Timestamp('2010-12-30')]

作品!

于 2018-03-09T17:40:12.117 回答