Home > Enterprise >  How to substract 15 minutes from a pandas timestamp that has 'ms' units on Python?
How to substract 15 minutes from a pandas timestamp that has 'ms' units on Python?

Time:06-09

Suppose that you have the following df:

          End Date         Close Price

2022-05-12 09:59:59.999000     0.00711
2022-05-12 10:14:59.999000     0.00704
2022-05-12 10:29:59.999000     0.00712

The ['End Date'] was set as the index column of the df

When typing:

In [1]: df.index[0]

You get:

Out[1]: Timestamp('2022-05-12 09:59:59.999000')

So, how could you substract 15 minutes from the df.index[0] value in order to get the following output?

Out[2]: Timestamp('2022-05-12 09:44:59.999000')

CodePudding user response:

Figured it out already, it was actually very straight forward:

In [1]: df.index[0] - pd.DateOffset(minutes=15)

Out [1]: Timestamp('2022-05-12 09:44:59.999000')

Source: https://thispointer.com/how-to-add-minutes-to-datetime-in-python/

  • Related