107

I've got a DataFrame whose index is just datetime.time and there's no method in DataFrame.Index and datetime.time to shift the time. datetime.time has replace but that'll only work on individual items of the Series?

Here's an example of the index used:

In[526]:  dfa.index[:5]
Out[526]: Index([21:12:19, 21:12:20, 21:12:21, 21:12:21, 21:12:22], dtype='object')

In[527]:  type(dfa.index[0])
Out[527]: datetime.time
danronmoon
  • 3,814
  • 5
  • 34
  • 56
Cameron Stark
  • 1,262
  • 2
  • 8
  • 9

3 Answers3

149

Liam's link looks great, but also check out pandas.Timedelta - looks like it plays nicely with NumPy's and Python's time deltas.

https://pandas.pydata.org/pandas-docs/stable/timedeltas.html

pd.date_range('2014-01-01', periods=10) + pd.Timedelta(days=1)
Alex
  • 18,484
  • 8
  • 60
  • 80
  • 3
    Using df.index + pd.Timedelta(hours=12) gives me the error "TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'Timedelta'". The index itself is pandas.core.index.Index but each value is a datetime.time which somehow interferes with the tshift() method as well as Timedelta additions. – Cameron Stark Mar 10 '15 at 13:20
  • See Mostafa's comment on your original question re. conversion to `Timestamps`. – Alex Mar 10 '15 at 14:14
  • @MostafaMahmoud are you guys really able to take a series or index of datetime.time values and convert directly with pandas.Timestamp() or even with the method .to_timestamp() ? These only throw errors for me. I'm considering joining the datetime.time to a date and forming a DatetimeIndex outright but that's not optimal since my dataset only logs time. – Cameron Stark Mar 10 '15 at 16:45
  • Please post an example of the index you are using. – Alex Mar 10 '15 at 16:55
  • Posted. Let me know if you need more detail. Thanks for the help! – Cameron Stark Mar 10 '15 at 17:22
  • 2
    df_series.index = (df_series.index + pd.Timedelta(days=1)) – jjcf89 Mar 01 '17 at 14:52
  • what is the periods for? – thentangler Apr 01 '22 at 02:12
3

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
edesz
  • 11,756
  • 22
  • 75
  • 123
Philippe Remy
  • 1,801
  • 3
  • 15
  • 20
0

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)
Adam Kuzański
  • 509
  • 4
  • 12