使用.loc选择DatetimeIndex行的范围(Pandas Python 3)

5

使用带有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)内的行。


1
打印aapl.head()并将输出粘贴到您的问题中? - cs95
@anon,以下解决方案有没有帮助?如果有,请随意接受其中一个(左侧的绿色勾号),或提出进一步的问题。 - jpp
3个回答

5

看起来您需要将索引转换为 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

4

我理解为:

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

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

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


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

成功了!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接