Add months to a date in Pandas

后端 未结 1 1661
囚心锁ツ
囚心锁ツ 2020-11-28 13:10

I\'m trying to figure out how to add 3 months to a date in a Pandas dataframe, while keeping it in the date format, so I can use it to lookup a range.

This is what I

相关标签:
1条回答
  • 2020-11-28 14:09

    You could use pd.DateOffset

    In [1756]: df.date + pd.DateOffset(months=plus_month_period)
    Out[1756]:
    0   2017-01-11
    1   2017-02-01
    Name: date, dtype: datetime64[ns]
    

    Another way using pd.offsets.MonthOffset

    In [1785]: df.date + pd.offsets.MonthOffset(plus_month_period)
    Out[1785]:
    0   2016-10-14
    1   2016-11-04
    Name: date, dtype: datetime64[ns]
    

    Details

    In [1757]: df
    Out[1757]:
            date
    0 2016-10-11
    1 2016-11-01
    
    In [1758]: plus_month_period
    Out[1758]: 3
    
    0 讨论(0)
提交回复
热议问题