Python Pandas – 如何使用秒频率舍入TimeDeltaIndex

Python Pandas – 如何使用秒频率舍入TimeDeltaIndex

要使用秒频率舍入TimeDeltaIndex,请使用 TimeDeltaIndex.round() 方法。对于秒频率,请使用值为 “S”freq 参数。

首先,导入所需的库 –

import pandas as pd

创建一个TimeDeltaIndex对象。我们使用’data’参数设置timedelta-like数据 –

tdIndex = pd.TimedeltaIndex(data =['10天5小时2分钟3us 10ns',' +22:39:19.999999',
'2天4小时03:08:02.000045',' +21: 15:45.999999 ')

显示TimedeltaIndex-

print("TimedeltaIndex...\n", tdIndex)

对TimeDeltaIndex日期进行秒频率舍入操作。对于秒频率,我们使用’S’ –

print("\nPerforming round operation with seconds frequency...\n",
tdIndex.round(freq='S'))

示例

以下是代码 –

import pandas as pd

#创建一个TimeDeltaIndex对象
#我们使用'data'参数设置timedelta-like数据
tdIndex = pd.TimedeltaIndex(data =['10天5小时2分钟3us 10ns',' +22:39:19.999999',
'2天4小时03:08:02.000045',' +21: 15:45.999999 ')

# 显示TimedeltaIndex
print("TimedeltaIndex...\n", tdIndex)

#返回TimeDeltas的组件的Dataframe
print("\nThe Dataframe of the components of TimeDeltas...\n", tdIndex.components)

#对TimeDeltaIndex日期进行秒频率舍入操作
#对于秒频率,我们使用'S'
print("\nPerforming round operation with seconds frequency...\n",
tdIndex.round(freq='S'))

输出

这将生成以下代码 –

TimedeltaIndex...
TimedeltaIndex(['10 days 05:02:00.000003010', '0 days 22:39:19.999999',
'2 days 07:08:02.000045', '0 days 21:15:45.999999'],
dtype='timedelta64[ns]', freq=None)

The Dataframe of the components of TimeDeltas...
   days hours minutes seconds milliseconds microseconds nanoseconds
0    10    5      2       0           0            3           10
1     0   22     39      19         999          999            0
2     2    7      8       2           0           45            0
3     0   21     15      45         999          999            0

Performing round operation with seconds frequency...
TimedeltaIndex(['10 days 05:02:00', '0 days 22:39:20', '2 days 07:08:02',
'0 days 21:15:46'],
dtype='timedelta64[ns]', freq=None)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程