How to efficiently add seconds place to date time

Question:

I have a pandas data frame full of 1 second data of the form “10/23/2017 6:00”. Each time appears 60 times in the file, and I’d like to know if there is an easy/efficient/smart way to add seconds to each row such that I get “10/23/2017 6:00:00, 10/23/2017 6:00:01 …”

Asked By: John

||

Answers:

First convert column to_datetime and then add seconds created by cumcount and to_timedelta:

df['time'] = pd.to_datetime(df['time'])
df['time'] += pd.to_timedelta(df.groupby('time').cumcount(), unit='s')

Sample:

df = pd.DataFrame({'time':['10/23/2017 6:00'] * 60}) 

df['time'] = pd.to_datetime(df['time'])
df['time'] += pd.to_timedelta(df.groupby('time').cumcount(), unit='s')
print (df.head(10))
                 time
0 2017-10-23 06:00:00
1 2017-10-23 06:00:01
2 2017-10-23 06:00:02
3 2017-10-23 06:00:03
4 2017-10-23 06:00:04
5 2017-10-23 06:00:05
6 2017-10-23 06:00:06
7 2017-10-23 06:00:07
8 2017-10-23 06:00:08
9 2017-10-23 06:00:09
Answered By: jezrael
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.