My dataframe looks like this:

1      2019-04-22 00:01:00
2      2019-04-22 00:01:00
3      2019-04-22 00:01:00
4      2019-04-22 00:01:00
5      2019-04-22 00:02:00
6      2019-04-22 00:02:00
7      2019-04-22 00:02:00
8      2019-04-22 00:02:00
9      2019-04-22 00:03:00
10     2019-04-22 00:03:00
11     2019-04-22 00:03:00
12     2019-04-22 00:03:00

As you can see there are four rows for each minute, what I would need is to add 15 secondes to each row so that it looks like this:

1      2019-04-22 00:01:00
2      2019-04-22 00:01:15
3      2019-04-22 00:01:30
4      2019-04-22 00:01:45
5      2019-04-22 00:02:00
6      2019-04-22 00:02:15
7      2019-04-22 00:02:30
8      2019-04-22 00:02:45
9      2019-04-22 00:03:00
10     2019-04-22 00:03:15
11     2019-04-22 00:03:30
12     2019-04-22 00:03:45

Any idea on how to proceed? I am not really good at datetime object so I am a bit stuck on that one... thank you in advance!

1 Answers

3
jezrael On Best Solutions

You can add timedeltas to datetimes column:

df['date'] += pd.to_timedelta(df.groupby('date').cumcount() * 15, unit='s')

print (df)
                  date
1  2019-04-22 00:01:00
2  2019-04-22 00:01:15
3  2019-04-22 00:01:30
4  2019-04-22 00:01:45
5  2019-04-22 00:02:00
6  2019-04-22 00:02:15
7  2019-04-22 00:02:30
8  2019-04-22 00:02:45
9  2019-04-22 00:03:00
10 2019-04-22 00:03:15
11 2019-04-22 00:03:30
12 2019-04-22 00:03:45

Detail:

First create counter Series by GroupBy.cumcount:

print (df.groupby('date').cumcount())
1     0
2     1
3     2
4     3
5     0
6     1
7     2
8     3
9     0
10    1
11    2
12    3
dtype: int64

Multiple by 15 and convert to seconds timedeltas by to_timedelta:

print (pd.to_timedelta(df.groupby('date').cumcount() * 15, unit='s'))
1    00:00:00
2    00:00:15
3    00:00:30
4    00:00:45
5    00:00:00
6    00:00:15
7    00:00:30
8    00:00:45
9    00:00:00
10   00:00:15
11   00:00:30
12   00:00:45
dtype: timedelta64[ns]