Pandas 如何向数据帧中所有重复的DatetimeIndex填充1ns?
如何在Pandas 如何向数据帧中所有重复的DatetimeIndex填充1ns?,pandas,dataframe,Pandas,Dataframe,如何在Pandas数据帧中向所有重复的DatetimeIndex填充1ns 例如,从以下方面: 2016-11-13 20:00:10.617989120 2016-11-13 20:00:10.617989120 2016-11-13 20:00:10.617989120 2016-11-13 20:00:10.123945353 2016-11-13 20:00:14.565989314 2016-11-13 20:00:18.565989315 2016-11-13 20:00:18.56
Pandas数据帧
中向所有重复的DatetimeIndex
填充1ns
例如,从以下方面:
2016-11-13 20:00:10.617989120
2016-11-13 20:00:10.617989120
2016-11-13 20:00:10.617989120
2016-11-13 20:00:10.123945353
2016-11-13 20:00:14.565989314
2016-11-13 20:00:18.565989315
2016-11-13 20:00:18.565989315
2016-11-13 20:00:18.565989315
为此:
2016-11-13 20:00:10.617989120
2016-11-13 20:00:10.617989121
2016-11-13 20:00:10.617989122
2016-11-13 20:00:10.123945353
2016-11-13 20:00:14.565989314
2016-11-13 20:00:18.565989315
2016-11-13 20:00:18.565989316
2016-11-13 20:00:18.565989317
您可以与convert一起使用:
您可以与convert一起使用:
嗯,我用cumsum试着得到同样的结果,但它当然不起作用。。我不知道,谢谢你!!嗯,我用cumsum试着得到同样的结果,但它当然不起作用。。我不知道,谢谢你!!
print (df.groupby(level=0).cumcount())
2016-11-13 20:00:10.617989120 0
2016-11-13 20:00:10.617989120 1
2016-11-13 20:00:10.617989120 2
2016-11-13 20:00:10.123945353 0
2016-11-13 20:00:14.565989314 0
2016-11-13 20:00:18.565989315 0
2016-11-13 20:00:18.565989315 1
2016-11-13 20:00:18.565989315 2
dtype: int64
df.index = df.index + pd.to_timedelta(df.groupby(level=0).cumcount())
print (df.index)
DatetimeIndex(['2016-11-13 20:00:10.617989120',
'2016-11-13 20:00:10.617989121',
'2016-11-13 20:00:10.617989122',
'2016-11-13 20:00:10.123945353',
'2016-11-13 20:00:14.565989314',
'2016-11-13 20:00:18.565989315',
'2016-11-13 20:00:18.565989316',
'2016-11-13 20:00:18.565989317'],
dtype='datetime64[ns]', freq=None)