Python 如何使用一个日期索引数据框映射到另一个日期索引数据框?

Python 如何使用一个日期索引数据框映射到另一个日期索引数据框?,python,pandas,Python,Pandas,我有两个熊猫数据帧,都是按日期索引的,一个按天,另一个按季度。一个有每日价格,另一个有季度价格平减指数 我想把价格标出来,这样它们就都平减了。我需要将每个值除以该日所属季度的相应平减指数。我做了一个比较日和季度的函数,但我对熊猫还不熟悉,我不知道如何使用它来映射我的数据帧 def find_quarter(x): date = x.index.to_pydatetime y = date.year q1 = datetime(y,1,1) q2 = datetim

我有两个熊猫数据帧,都是按日期索引的,一个按天,另一个按季度。一个有每日价格,另一个有季度价格平减指数

我想把价格标出来,这样它们就都平减了。我需要将每个值除以该日所属季度的相应平减指数。我做了一个比较日和季度的函数,但我对熊猫还不熟悉,我不知道如何使用它来映射我的数据帧

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
我需要这样的东西:

prices['Settle'].map(lambda x: ##find the right deflator VALUE for x and divide x by it)
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
我认为这应该是一项直截了当的任务,但我在文档中搜索了几个小时,什么也没找到。我刚接触熊猫,所以我可能错过了什么


谢谢你的帮助

如果我理解正确,我想你可以通过重新编制平减指数来处理这个问题,比如:

prices['Settle'].map(lambda x: ##find the right deflator VALUE for x and divide x by it)
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
根据您的平减指数时间序列,您可能需要对重新指数使用'ffill'或'bfill'