Mark
Mark

Reputation: 181

Convert Pandas Time series to Time delta

I am trying to plot a pandas time series but instead of the actual time on the y axis, I want the time since the beginning of the time series as the X axis.

Are there any convenient ways of formatting it or converting my time series to a time delta series?

Upvotes: 2

Views: 656

Answers (1)

jezrael
jezrael

Reputation: 863741

Subtract DatetimeIndex by first value by indexing:

s = pd.Series([1,8,9], index= pd.date_range('2015-01-01', periods=3, freq='H'))
print (s)
2015-01-01 00:00:00    1
2015-01-01 01:00:00    8
2015-01-01 02:00:00    9
Freq: H, dtype: int64

s.index = (s.index - s.index[0])
print (s)
00:00:00    1
01:00:00    8
02:00:00    9
dtype: int64

If necessary convert TimedeltaIndex to seconds use total_seconds or days if no times in DatetimeIndex:

s.index = (s.index - s.index[0]).total_seconds()
print (s)
0.0       1
3600.0    8
7200.0    9
dtype: int64

Upvotes: 2

Related Questions