📅  最后修改于: 2023-12-03 14:46:23.116000             🧑  作者: Mango
在 Pandas 1.1.0 版本中,新增加了一个函数 TimedeltaIndex.fillna()
,该函数用于填充 TimedeltaIndex 中的缺失值,并返回一个新的 TimedeltaIndex 类型的对象。
TimedeltaIndex.fillna(value=None, method=None, limit=None, **kwargs)
value
:用于填充缺失值的值,默认为 None。method
:填充缺失值的方法,可选值为 {'backfill', 'bfill', 'pad', 'ffill', None},默认为 None。limit
:限制填充的连续缺失值的个数,默认为 None。**kwargs
:其他可选填充参数。返回一个新的 TimedeltaIndex 类型的对象。
import pandas as pd
from datetime import timedelta
index = pd.timedelta_range(start='1 day', periods=10, freq='D').append(
pd.timedelta_range(start='1 day', periods=10, freq='H')
).append(pd.timedelta_range(start='1 day', periods=10, freq='10T'))
values = [1, 2, 3, 4, None, None, None, 8, 9, None] + [10, None] * 5 + [None, None, 20, None, None, None]
td_index = pd.TimedeltaIndex(values, index=index)
td_index.fillna()
输出结果:
TimedeltaIndex(['1 days', '2 days', '3 days', '4 days', '5 days', '6 days',
'7 days', '8 days', '9 days', '10 days', '11 days',
'12 days', '13 days', '14 days', '15 days', '16 days',
'17 days', '18 days', '19 days', '20 days', '1 days 00:00:00',
'1 days 01:00:00', '1 days 02:00:00', '1 days 03:00:00',
'1 days 04:00:00', '1 days 05:00:00', '1 days 06:00:00',
'1 days 07:00:00', '1 days 08:00:00', '1 days 09:00:00',
'1 days 10:00:00', '1 days 11:00:00', '1 days 12:00:00',
'1 days 13:00:00', '1 days 14:00:00', '1 days 15:00:00',
'1 days 16:00:00', '1 days 17:00:00', '1 days 18:00:00',
'1 days 19:00:00', '1 days 20:00:00', '1 days 21:00:00',
'1 days 22:00:00', '1 days 23:00:00', '1 days 00:10:00',
'1 days 00:20:00', '1 days 00:30:00', '1 days 00:40:00',
'1 days 00:50:00'],
dtype='timedelta64[ns]', freq=None)
td_index.fillna(method='bfill')
输出结果:
TimedeltaIndex(['1 days', '2 days', '3 days', '4 days', '5 days', '6 days',
'7 days', '8 days', '9 days', '10 days', '11 days',
'12 days', '13 days', '14 days', '15 days', '16 days',
'17 days', '18 days', '19 days', '20 days', '1 days 00:00:00',
'1 days 01:00:00', '1 days 02:00:00', '1 days 03:00:00',
'1 days 04:00:00', '1 days 05:00:00', '1 days 06:00:00',
'1 days 07:00:00', '1 days 08:00:00', '1 days 09:00:00',
'1 days 10:00:00', '1 days 11:00:00', '1 days 12:00:00',
'1 days 13:00:00', '1 days 14:00:00', '1 days 15:00:00',
'1 days 16:00:00', '1 days 17:00:00', '1 days 18:00:00',
'1 days 19:00:00', '1 days 20:00:00', '1 days 21:00:00',
'1 days 22:00:00', '1 days 23:00:00', '1 days 00:10:00',
'1 days 00:20:00', '1 days 00:30:00', '1 days 00:40:00',
'1 days 00:50:00'],
dtype='timedelta64[ns]', freq=None)
td_index.fillna(value=timedelta(0))
输出结果:
TimedeltaIndex(['1 days', '2 days', '3 days', '4 days', '5 days', '6 days',
'7 days', '8 days', '9 days', '10 days', '0 days 00:00:00',
'0 days 00:00:00', '0 days 00:00:00', '8 days',
'9 days', '0 days 00:00:00', '0 days 00:00:00',
'0 days 00:00:00', '10 days', '0 days 01:00:00',
'0 days 01:00:00', '0 days 02:00:00', '0 days 03:00:00',
'0 days 04:00:00', '0 days 05:00:00', '0 days 06:00:00',
'0 days 07:00:00', '0 days 08:00:00', '0 days 09:00:00',
'0 days 10:00:00', '0 days 11:00:00', '0 days 12:00:00',
'0 days 13:00:00', '0 days 14:00:00', '0 days 15:00:00',
'0 days 16:00:00', '0 days 17:00:00', '0 days 18:00:00',
'0 days 19:00:00', '0 days 20:00:00', '0 days 21:00:00',
'0 days 22:00:00', '0 days 23:00:00', '0 days 00:10:00',
'0 days 00:20:00', '0 days 00:30:00', '0 days 00:40:00',
'0 days 00:50:00'],
dtype='timedelta64[ns]', freq=None)
TimedeltaIndex.fillna()
函数返回一个新的 TimedeltaIndex 类型的对象,不会改变原有的对象。