Python 找到相等的时间并递增地添加一个常量
我有一个包含一些时间戳的数据帧Python 找到相等的时间并递增地添加一个常量,python,pandas,datetime,group-by,Python,Pandas,Datetime,Group By,我有一个包含一些时间戳的数据帧df df['Date'].values Out[16]: array(['2015-03-25T14:36:39.199994000', '2015-03-25T14:36:39.199994000', '2015-03-26T10:05:03.699999000', '2015-04-19T16:01:49.680009000', '2015-04-19T16:36:10.040007000', '2015-04-19T16:36:
df
df['Date'].values
Out[16]:
array(['2015-03-25T14:36:39.199994000', '2015-03-25T14:36:39.199994000',
'2015-03-26T10:05:03.699999000', '2015-04-19T16:01:49.680009000',
'2015-04-19T16:36:10.040007000', '2015-04-19T16:36:10.040007000',
'2015-04-19T16:36:10.040007000'], dtype='datetime64[ns]')
正如您所看到的,第一个和第二个时间戳是相等的,但最后3个时间戳也是相等的
我想扫描数据帧,如果有相等的时间戳,则保持第一个时间戳,并以增量方式向其他相等的时间戳添加5秒。
新的数据帧应该如下所示
df['Date'].values
Out[16]:
array(['2015-03-25T14:36:39.199994000', '2015-03-25T14:36:44.199994000',
'2015-03-26T10:05:03.699999000', '2015-04-19T16:01:49.680009000',
'2015-04-19T16:36:10.040007000', '2015-04-19T16:36:15.040007000',
'2015-04-19T16:36:20.040007000'], dtype='datetime64[ns]')
有没有一种不循环的python方法..我想根据时间戳进行分组,但我不知道如何继续…使用groupby cumcount乘以时间增量,即
df = pd.DataFrame({'Date':np.array(['2015-03-25T14:36:39.199994000', '2015-03-25T14:36:39.199994000',
'2015-03-26T10:05:03.699999000', '2015-04-19T16:01:49.680009000',
'2015-04-19T16:36:10.040007000', '2015-04-19T16:36:10.040007000',
'2015-04-19T16:36:10.040007000'], dtype='datetime64[ns]')})
df['Date'] + df.groupby(df['Date']).cumcount()*pd.Timedelta('5 seconds')
输出:
0 2015-03-25 14:36:39.199994
1 2015-03-25 14:36:44.199994
2 2015-03-26 10:05:03.699999
3 2015-04-19 16:01:49.680009
4 2015-04-19 16:36:10.040007
5 2015-04-19 16:36:15.040007
6 2015-04-19 16:36:20.040007
dtype: datetime64[ns]
0 2015-03-25 14:36:39.199994
1 2015-03-25 14:36:44.199994
2 2015-03-26 10:05:03.699999
3 2015-04-19 16:01:49.680009
4 2015-04-19 16:36:10.040007
5 2015-04-19 16:36:15.040007
6 2015-04-19 16:36:20.040007
数据类型:datetime64[ns]