falo8056
falo8056

Reputation: 85

Convert timestamp to an array of accumulative seconds

Hello I got a time stamp that I converted to this :

x = pd.to_datetime(df['Time'], format='%H:%M:%S')

1900-01-01 09:48:42
1900-01-01 09:48:43
1900-01-01 09:48:44
1900-01-01 09:48:45
1900-01-01 09:48:46
1900-01-01 09:48:47

Now, I want to be able to retrieve and accumulate the seconds in an array, like this:

1 
2 
3 
4 
5
6

More info.

 x.dtype
    dtype('<M8[ns]')

Upvotes: 0

Views: 36

Answers (1)

jezrael
jezrael

Reputation: 862481

You can use to_timedelta with subtract first value with Series.dt.total_seconds and add 1:

x = pd.to_timedelta(df['Time'])
#alternative
#x = pd.to_datetime(df['Time'], format='%H:%M:%S')

a = x.sub(x.iat[0]).dt.total_seconds().astype(int) + 1
print (a)
0    1
1    2
2    3
3    4
4    5
5    6
Name: Time, dtype: int32

Upvotes: 1

Related Questions