python3.6----datetime.timedelta

时间:2023-03-08 19:51:05

学习利用python进行数据分析---时间序列分析的时候发现python2.7版本的timedelta模块跟python3.6模块区别
python2.7:
in:delta= datetime(2017,8,10)-datetime(2017,8,1)
in:delta
out:datetime.timedelta(9,777600)

python3.6:
in:delta= datetime(2017,8,10)-datetime(2017,8,1)
in:delta
out:datetime.timedelta(9)
in:delta.days
out:9
in:delta.seconds
out:0
in:delta.hours
out:datetime.timedelta' object has no attribute 'hours'

in:delta.total_seconds()
Out: 777600.0

有网友遇到跟我同样的问题,网友链接如下
https://www.reddit.com/r/learnpython/comments/23yu5j/how_come_my_datetimetimedelta_object_has_no_hours/

根据网友的回答,Why? Because the timedelta only stores days, seconds and microseconds internally. Getting the hours would be a function which operates on the class attribute(s).
修改
from datetime import timedelta
from datetime import datetime
import time
detal = datetime(2017,8,10)-datetime(2017,8,1)
delta.days
delta_seconds = delta.total_seconds()
delta_hours =delta_seconds/3600