从 pandas 的日期时间列中减去一年 [英] Subtract a year from a datetime column in pandas
本文介绍了从 pandas 的日期时间列中减去一年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个datetime列,如下所示-
I have a datetime column as below -
>>> df['ACC_DATE'].head(2)
538 2006-04-07
550 2006-04-12
Name: ACC_DATE, dtype: datetime64[ns]
现在,我想从此列的每一行中减去一年.我如何才能达到相同的&我可以使用哪个图书馆?
Now, I want to subtract an year from each row of this column. How can I achieve the same & which library can I use?
预期字段-
ACC_DATE NEW_DATE
538 2006-04-07 2005-04-07
549 2006-04-12 2005-04-12
推荐答案
You can use DateOffset
to achieve this:
In[88]:
df['NEW_DATE'] = df['ACC_DATE'] - pd.DateOffset(years=1)
df
Out[88]:
ACC_DATE NEW_DATE
index
538 2006-04-07 2005-04-07
550 2006-04-12 2005-04-12
这篇关于从 pandas 的日期时间列中减去一年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文