What's the simplest way to subtract a month from a date in Python?

前端 未结 21 1881
[愿得一人]
[愿得一人] 2020-12-02 09:12

If only timedelta had a month argument in it\'s constructor. So what\'s the simplest way to do this?

EDIT: I wasn\'t thinking too hard about this as was poin

21条回答
  •  情深已故
    2020-12-02 09:37

    I think the simple way is to use DateOffset from Pandas like so:

    import pandas as pd
    date_1 = pd.to_datetime("2013-03-31", format="%Y-%m-%d") - pd.DateOffset(months=1)
    

    The result will be a datetime object

提交回复
热议问题