我有两个pandas DataFrame,它们都按日期索引,每天一个按季度索引。一个是每日价格,另一个是季度价格平减指数。
我想映射价格,以便它们全部缩小。我需要通过适当的平减指数将每个值除以当天所属的季度。我做了一个比较日期和季度的函数,但我是大熊猫的新手,我不知道如何使用它来映射我的DataFrame
def find_quarter(x):
date = x.index.to_pydatetime
y = date.year
q1 = datetime(y,1,1)
q2 = datetime(y,4,1)
q3 = datetime(y,7,1)
q4 = datetime(y,10,1)
if date <= q2:
return q1
elif date <= q3:
return q2
elif date <= q4:
return q3
else:
return q4
这些是我的DataFrames:
price_deflator = Quandl.get("FRED/USAGDPDEFQISMEI")
deflator_values = price_deflator['VALUE']
prices_series = Quandl.get("OFDP/FUTURE_W1", trim_start="1995-12-31", trim_end="2014-12-31")
prices = price_series['Settle']
我需要这样的东西:
prices['Settle'].map(lambda x: ##find the right deflator VALUE for x and divide x by it)
我这应该是一个直接的任务,但我搜索文档几个小时,找不到任何东西。我是大熊猫的新手,所以也许我错过了一些东西。
我感谢你的帮助!
答案 0 :(得分:0)
如果我理解正确,我认为你可以通过重新索引你的平减指数来解决这个问题,如下所示:
import numpy as np
import pandas as pd
# simulate the data for 10 years in DataFrames
qtr = pd.date_range('2000-1-1', periods=40, freq='Q')
days = pd.date_range('2000-1-1', periods=3653, freq='D')
prices = pd.DataFrame(np.random.rand(3653)*10, index=days, columns=['price'])
deflate = pd.DataFrame(np.arange(1,41), index=qtr, columns=['deflator'])
# reindex the deflator from qtrs to days than divide to get deflated price
df2 = deflate.reindex(index=days, method='bfill')
prices['deflator'] = df2['deflator']
prices['deflated price'] = prices['price']/prices['deflator']
# show head and tail
print(prices.head())
print(prices.tail())
price deflator deflated price
2000-01-01 5.111764 1 5.111764
2000-01-02 9.266700 1 9.266700
2000-01-03 5.581109 1 5.581109
2000-01-04 2.962819 1 2.962819
2000-01-05 2.110148 1 2.110148
price deflator deflated price
2009-12-27 6.845248 40 0.171131
2009-12-28 6.032179 40 0.150804
2009-12-29 2.438561 40 0.060964
2009-12-30 4.090140 40 0.102253
2009-12-31 6.058384 40 0.151460
您可能需要使用&fffill&#39;或者&#39; bfill&#39;根据您的平减指数时间序列重新索引。