我想将<img>
划分为任意时间段,然后创建列以计算每一行直到下一个第n个时间段结束为止的时间。
例如此输入:
dataframe
将产生以下输出:
df = pd.DataFrame({'dates':pd.date_range('2017-01-01',periods=8,freq='1d')}).set_index('dates')
# the inclusive ends of the time periods
rolling_dates = ['2017-01-02', '2017-01-05', '2017-01-07', '2017-01-08']
periods_offests = [0, 1, 2] # the remaining time periods columns
答案 0 :(得分:0)
一种方法是创建一个小的函数以应用于数据帧索引,并为每个滚动日期计算偏移量(连续)。
def map_offsets(x):
'''Calculate the day offsets'''
days = [x for x in (pd_rolling - x).days if x >= 0]
days += [np.nan] * (len(pd_rolling) - len(days))
return [days[i] for i in periods_offsets]
df = pd.DataFrame({'dates': pd.date_range('2017-01-01', periods=8, freq='1d')}).set_index('dates')
# the inclusive ends of the time periods
rolling_dates = ['2017-01-02', '2017-01-05', '2017-01-07', '2017-01-08']
# the remaining time periods columns
periods_offsets = [0, 1, 2]
# Added: Casting to datetime will make offset calculation easier
pd_rolling = pd.to_datetime(rolling_dates)
# Create new column names for periods
fmt = 'periods_expiry_days_{}'
columns = [fmt.format(x) for x in periods_offsets]
# Subtract index from rolling date values, and add to dataframe
df[columns] = pd.DataFrame(df.index.map(map_offsets).tolist(), index=df.index)