【问题标题】:R-Python: Getting Monthly, weekly index pointsR-Python:获取每月、每周索引点
【发布时间】:2013-11-18 05:56:25
【问题描述】:

在 R 的 xts 包中,有一个名为 endpoints 的函数,它给定一个 xts 对象,将返回一个给定月份、星期或任何用户指定频率返回的索引。如何使用 python 在 pandas 中做到这一点?

R:

endpoints(xts.object, "frequency")

Python:

from matplotlib.pylab import *
from pandas.io.data import DataReader
from datetime import datetime
symbols = ["SPY","IEF"]
data_holder = DataReader(symbols,  "yahoo",datetime(2001,1,1))
adj_close = data_holder["Adj Close"] #adjusted close data
adj_close = adj_close.dropna() #drop NAs
adj_close.head() #inspect elements

我知道在 python 中使用"M" 作为参数的重采样函数将为我提供每月数据。但是有没有办法获取一个索引数组,使得这些索引中的每一个都引用数据框中的一行,即月末日期?

所以一个具体的例子,我使用的是伪代码:

month_ends = adj_close.someFunction("months") #gives me the index of each month ends
month_ends.head()

[22,41,62..etc]

adj_close[month_ends,] #should give me the same thing as resampled("M")

【问题讨论】:

  • 您能否提供一个具体示例来说明您正在寻找什么?我猜你想要resampling
  • 为什么不通过rpy2 使用xts 包?你接受这个作为解决方案吗?
  • xts 相当慢,因此我正在使用 python。
  • 如果我使用 XTS 的端点,你会怎么做?我已经安装了 rpy2 并导入了 xts。如何将 pandas 数据帧传递给 xts.endpoints 函数?

标签: python r pandas xts


【解决方案1】:

创建一个以[0, 1, ...]为值的系列,然后调用resample

s = pd.Series(np.arange(adj_close.shape[0]), index=adj_close.index)
locs = s.resample("M", how="max")
print locs

输出是:

Date
2002-07-31      0
2002-08-31     22
2002-09-30     42
2002-10-31     65
2002-11-30     85
2002-12-31    106
2003-01-31    127
2003-02-28    146
2003-03-31    167
2003-04-30    188
2003-05-31    209
2003-06-30    230
2003-07-31    252
2003-08-31    273
2003-09-30    294
...
2012-09-30    2561
2012-10-31    2582
2012-11-30    2603
2012-12-31    2623
2013-01-31    2644
2013-02-28    2663
2013-03-31    2683
2013-04-30    2705
2013-05-31    2727
2013-06-30    2747
2013-07-31    2769
2013-08-31    2791
2013-09-30    2811
2013-10-31    2834
2013-11-30    2844
Freq: M, Length: 137, dtype: int64

获取行:

print adj_close.iloc[locs, :].head(10)

输出:

             IEF    SPY
Date                    
2002-07-31  55.49  73.01
2002-08-30  56.89  73.51
2002-09-30  59.08  65.80
2002-10-31  58.34  71.22
2002-11-29  56.93  75.61
2002-12-31  58.95  71.33
2003-01-31  58.50  69.58
2003-02-28  59.79  68.64
2003-03-31  59.56  68.79
2003-04-30  59.64  74.61

【讨论】:

【解决方案2】:

如果我理解正确,您正在寻找熊猫的DateOffset

http://pandas.pydata.org/pandas-docs/dev/timeseries.html#dateoffset-objects

那里有一些很好的例子,但是为了让您了解如何使用它:

import datetime
from pandas.tseries.offsets import *

a=datetime.datetime(2013,11,5)
print a + BMonthEnd() #Last Business day of the month, 
OUT:datetime.datetime(2013, 11, 29, 0, 0)

print a + MonthEnd()
OUT: datetime.datetime(2013, 11, 30, 0, 0)

print a.weekday() # is 1, i.e. Tuesday
print a + Week(weekday=4) # Looking for Friday
OUT: 2013-11-08 00:00:00

以上内容应该为您提供正确的日期时间对象,然后您可以使用它来查询数据。

编辑:可能有更简单的方法可以做到这一点,但喝了几杯啤酒后,我用我的 'df' DataFrame 以这种方式获得了索引:

a=list(df.index.values) # This copies the index into a list and allows you to do:
print a.index(np.datetime64(dt.datetime(2013,11,5) + Week(weekday=4))
OUT: The row number of the end of the week

df.index.values 返回一个没有index() 方法的ndarray,因此您需要将其转换为一个确实有此方法的列表。

注意,我使用pd.data_range 生成了我的索引,它使用了numpy.datetime64 对象。

因此,在您使用 dt.datetime(yyyy,mm,dd)+Week(weekday=4) 找到周末的最后一天后,您可以将其转换为 numpy.datetime64 对象,然后在您的列表中搜索它的索引。

【讨论】:

  • 我正在寻找更多的索引。那么日期框架中的哪一行等于月末、周末等
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-17
  • 2012-09-22
  • 2017-09-26
  • 2021-06-07
  • 2011-05-30
  • 2014-08-17
  • 1970-01-01
相关资源
最近更新 更多