2 回答
TA贡献1868条经验 获得超4个赞
datetime.timdelta
对象将天,秒和微秒存储为属性。我们可以在访问它们pandas.DataFrame
有dt
:
设置一些虚拟数据
import datetime as dt
import pandas as pd
df = pd.DataFrame(
data=(
dt.timedelta(days=17711, hours=i, minutes=i, seconds=i) for i in range(0, 10)
),
columns=['Duration']
)
print(df['Duration'])
Duration
0 17711 days 00:00:00
1 17711 days 01:01:01
2 17711 days 02:02:02
3 17711 days 03:03:03
4 17711 days 04:04:04
5 17711 days 05:05:05
6 17711 days 06:06:06
7 17711 days 07:07:07
8 17711 days 08:08:08
9 17711 days 09:09:09
Name: Duration, dtype: timedelta64[ns]
加快秒数,将其转化为小时
print(df['Duration'].dt.seconds / 3600)
0 0.000000
1 1.016944
2 2.033889
3 3.050833
4 4.067778
5 5.084722
6 6.101667
7 7.118611
8 8.135556
9 9.152500
Name: Duration, dtype: float64
只有几个小时
print(df['Duration'].dt.seconds // 3600)
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
Name: Duration, dtype: int64
添加回答
举报