Python Pandas - 使用字符串输入从 Timedelta 对象获取秒数
要从Timedelta对象返回秒数,请使用timedelta.seconds属性。首先,导入所需的库-
import pandas as pd
TimeDeltas是Python的标准日期时间库,使用不同的表示形式timedelta。使用单位“s”设置秒的字符串输入。创建一个Timedelta对象
timedelta = pd.Timedelta('1 min 30 s')
显示时间增量
print("Timedelta...\n", timedelta)
返回秒值
timedelta.seconds
示例
以下是代码
import pandas as pd #TimeDeltas是Python的标准日期时间库,使用不同的表示形式timedelta's # set string input for seconds using unit 's' #创建一个Timedelta对象 timedelta = pd.Timedelta('1 min 30 s') #显示时间增量 print("Timedelta...\n", timedelta) #返回秒值 res = timedelta.seconds #显示秒 print("\nTimedelta (seconds value)...\n", res)输出结果
这将产生以下代码
Timedelta... 0 days 00:01:30 Timedelta (seconds value)... 90