pandas.to_timedelta¶
-
pandas.
to_timedelta
(*args, **kwargs)[source]¶ 将参数转换为timedelta
参数: arg:string,timedelta,list,tuple,1-d array或Series
unit:arg(D,h,m,s,ms,us,ns)的单位表示单位,
整数/浮点数
框:boolean,默认值为True
- 如果True返回结果的Timedelta / TimedeltaIndex
- 如果False返回一个np.timedelta64或narray的值类型timedelta64 [ns]
错误:{'ignore','raise','coerce'},默认'raise'
- 如果'raise',那么无效的解析将引发异常
- 如果'coerce',那么无效的解析将被设置为NaT
- 如果'ignore',那么无效的解析将返回输入
返回: ret:timedelta64 / timedelta64的数组,如果解析成功
例子
将单个字符串解析为Timedelta:
>>> pd.to_timedelta('1 days 06:05:01.00003') Timedelta('1 days 06:05:01.000030') >>> pd.to_timedelta('15.5us') Timedelta('0 days 00:00:00.000015')
解析字符串列表或数组:
>>> pd.to_timedelta(['1 days 06:05:01.00003', '15.5us', 'nan']) TimedeltaIndex(['1 days 06:05:01.000030', '0 days 00:00:00.000015', NaT], dtype='timedelta64[ns]', freq=None)
通过指定单位关键字参数来转换数字:
>>> pd.to_timedelta(np.arange(5), unit='s') TimedeltaIndex(['00:00:00', '00:00:01', '00:00:02', '00:00:03', '00:00:04'], dtype='timedelta64[ns]', freq=None) >>> pd.to_timedelta(np.arange(5), unit='d') TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'], dtype='timedelta64[ns]', freq=None)