如何在 pandas Dataframe 中向 TimedeltaIndex 添加偏移量

How to add offset to TimedeltaIndex in pandas Dataframe

如何使用 TimedeltaIndex 向 Panda Dataframe 添加偏移量?

dfN.index

输出:

TimedeltaIndex([       '00:00:00', '00:00:00.100000', '00:00:00.200000',
                '00:00:00.300000', '00:00:00.400000', '00:00:00.500000',
                '00:00:00.600000', '00:00:00.700000', '00:00:00.800000',
                '00:00:00.900000',
                ...
                '01:56:26.700000', '01:56:26.800000', '01:56:26.900000',
                       '01:56:27', '01:56:27.100000', '01:56:27.200000',
                '01:56:27.300000', '01:56:27.400000', '01:56:27.500000',
                '01:56:27.600000'],
               dtype='timedelta64[ns]', name='timestamps', length=69877, freq='100L')

这不起作用:

dfN.index += Offset #Offset = 100

产出

TypeError: Addition/subtraction of integers and integer-arrays with TimedeltaArray is no longer supported.  Instead of adding/subtracting `n`, use `n * obj.freq`

在这种情况下,数据每隔 100 毫秒间隔一次。我想添加和偏移 500 毫秒。

变量Offset不能是整数。您需要创建一个 Timedelta 对象来添加它。试试这个:

dfN.index += pd.Timedelta(100, unit=‘milliseconds’) #This will add 100 miliseconds

dfN.index += pd.Timedelta(milliseconds = 100)