如何在Pandas DataFrame中填充1 ns到所有重复的DatetimeIndex?

时间:2017-02-20 09:19:55

标签: pandas dataframe

如何将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

1 个答案:

答案 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)