我有一个dataFrame
[in] MyDates
[out]
2017-04-04 -5.0
2017-04-03 -5.0
2017-03-31 -4.0
2017-03-30 -6.0
2017-03-29 -5.0
2017-03-28 -5.0
每个数字对应我应该在相应日期添加或删除的天数。我想创建一个新列,索引日期减去第一列中的天数。我知道我可以用DateOffset做到但我无法弄清楚如何......
谢谢!
答案 0 :(得分:4)
您可以将列转换为TimedeltaIndex
或to_timedelta
和add
(+
)或减去(-)
值:
df['new'] = df.index - pd.TimedeltaIndex(df['col'], unit='d')
print (df)
col new
2017-04-04 -5.0 2017-04-09
2017-04-03 -5.0 2017-04-08
2017-03-31 -4.0 2017-04-04
2017-03-30 -6.0 2017-04-05
2017-03-29 -5.0 2017-04-03
2017-03-28 -5.0 2017-04-02
或者:
df['new'] = df.index + pd.to_timedelta(df['col'], unit='d')
print (df)
col new
2017-04-04 -5.0 2017-03-30
2017-04-03 -5.0 2017-03-29
2017-03-31 -4.0 2017-03-27
2017-03-30 -6.0 2017-03-24
2017-03-29 -5.0 2017-03-24
2017-03-28 -5.0 2017-03-23
如果Series
为input
添加to_frame
:
df = s.to_frame('date')
df['new'] = df.index - pd.TimedeltaIndex(df['date'], unit='d')
print (df)
date new
2017-04-04 -5.0 2017-04-09
2017-04-03 -5.0 2017-04-08
2017-03-31 -4.0 2017-04-04
2017-03-30 -6.0 2017-04-05
2017-03-29 -5.0 2017-04-03
2017-03-28 -5.0 2017-04-02
答案 1 :(得分:3)
IIUC你要构建一个TimedeltaIndex
并添加它:
In [173]:
df.index + pd.TimedeltaIndex(df['days'], unit='d')
Out[173]:
DatetimeIndex(['2017-03-30', '2017-03-29', '2017-03-27', '2017-03-24',
'2017-03-24', '2017-03-23'],
dtype='datetime64[ns]', freq=None)
如果是专栏,您只需执行df['Dates'] + pd.TimedeltaIndex(df['days'], unit='d')
In [176]:
df['offset_date'] = df['Dates'] + pd.TimedeltaIndex(df['days'], unit='d')
df
Out[176]:
Dates days offset_date
0 2017-04-04 -5.0 2017-03-30
1 2017-04-03 -5.0 2017-03-29
2 2017-03-31 -4.0 2017-03-27
3 2017-03-30 -6.0 2017-03-24
4 2017-03-29 -5.0 2017-03-24
5 2017-03-28 -5.0 2017-03-23
如果它是索引并且您想要添加为列,那么它几乎是相同的操作:
In [180]:
df['offset_date'] = df.index + pd.TimedeltaIndex(df['days'], unit='d')
df
Out[180]:
days offset_date
Dates
2017-04-04 -5.0 2017-03-30
2017-04-03 -5.0 2017-03-29
2017-03-31 -4.0 2017-03-27
2017-03-30 -6.0 2017-03-24
2017-03-29 -5.0 2017-03-24
2017-03-28 -5.0 2017-03-23