如何将1 ns填充到DatetimeIndex
中所有重复的Pandas DataFrame
?
例如,来自:
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
答案 0 :(得分:0)
您可以将cumcount
与转化to_timedelta
:
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)