我在系列中尝试过datetime.timedelta,以及pd.DateOffset。两者都不起作用。我知道我可以迭代这个数据帧并手动添加它们,但我一直在寻找一种矢量化的方法。
示例:
d = {pd.Timestamp('2015-01-02'):{'days_delinquent':11}, pd.Timestamp('2015-01-15'):{'days_delinquent':23}}
>>> dataf = pd.DataFrame.from_dict(d,orient='index')
>>> dataf
days_delinquent
2015-01-02 11
2015-01-15 23
只是尝试在下面的行中添加11天和23天。我在现实生活中添加的专栏不是索引,但显然我可以将其作为索引。
我想这不是自我解释,但输出将是一个新的列,其中包含日期(在这种情况下为索引)+ datetime.timedelta(days = dataf [' days_delinquent'])
答案 0 :(得分:3)
您可以将days_delinquent
列转换为timedelta64[D]
(以天为单位的偏移量)并将其添加到索引中,例如:
import pandas as pd
d = {pd.Timestamp('2015-01-02'):{'days_delinquent':11}, pd.Timestamp('2015-01-15'):{'days_delinquent':23}}
df = pd.DataFrame.from_dict(d,orient='index')
df['returned_on'] = df.index + df.days_delinquent.astype('timedelta64[D]')
更好(感谢DSM)是使用pd.to_timedelta
所以如果需要,更容易更改单位:
df['returned_on'] = df.index + pd.to_timedelta(df.days_delinquent, 'D')
给你:
days_delinquent returned_on
2015-01-02 11 2015-01-13
2015-01-15 23 2015-02-07
答案 1 :(得分:1)
import pandas as pd
d = {pd.Timestamp('2015-01-02'):{'days_delinquent':11},
pd.Timestamp('2015-01-15'):{'days_delinquent':23}}
df = pd.DataFrame.from_dict(d,orient='index')
def add_days(x):
return x['index'] + pd.Timedelta(days=x['days_delinquent'])
df.reset_index().apply(add_days,axis=1)
输出:
0 2015-01-13
1 2015-02-07
dtype: datetime64[ns]
答案 2 :(得分:1)
dataf['result'] = [d + datetime.timedelta(delta)
for d, delta in zip(dataf.index, dataf.days_delinquent)]
dataf
Out[56]:
days_delinquent result
2015-01-02 11 2015-01-13
2015-01-15 23 2015-02-07