close

How to add/subtract time (hours, minutes, etc.) from a Pandas DataFrame.Index whos objects are of type datetime.time?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to add/subtract time (hours, minutes, etc.) from a Pandas DataFrame.Index whos objects are of type datetime.time in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

How to add/subtract time (hours, minutes, etc.) from a Pandas DataFrame.Index whos objects are of type datetime.time?

  1. How to add/subtract time (hours, minutes, etc.) from a Pandas DataFrame.Index whos objects are of type datetime.time?

    This one worked for me:
    >> print(df) TotalVolume Symbol 2016-04-15 09:00:00 108400 2802.T 2016-04-15 09:05:00 50300 2802.T

  2. add/subtract time (hours, minutes, etc.) from a Pandas DataFrame.Index whos objects are of type datetime.time

    This one worked for me:
    >> print(df) TotalVolume Symbol 2016-04-15 09:00:00 108400 2802.T 2016-04-15 09:05:00 50300 2802.T

Method 1

This one worked for me:

>> print(df)
                          TotalVolume  Symbol
2016-04-15 09:00:00       108400       2802.T
2016-04-15 09:05:00       50300        2802.T

>> print(df.set_index(pd.to_datetime(df.index.values) - datetime(2016, 4, 15)))

             TotalVolume  Symbol
09:00:00     108400       2802.T
09:05:00     50300        2802.T

Method 2

The Philippe solution but cleaner:

My subtraction data is: ‘2018-09-22T11:05:00.000Z’

import datetime
import pandas as pd

df_modified = pd.to_datetime(df_reference.index.values) - datetime.datetime(2018, 9, 22, 11, 5, 0)

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read