python - python有没有办法减去时间戳?
问题描述
我有两个列的数据框。它们都是时间戳,但是一个一直到 fff 到秒之外的地方。有没有办法在分钟和秒之间获得差异?
col1 col2
2:21:40.756 2:21:41
2:22:30.343 2:22:31
2:24:43.342 2:24:44
i have tried following:
col1= pd.todatetime(df.col1,format='%H:%M:%S.%f').dt.time
col2 = pd.todatetime(df.col2,format = '%H:%M:%S').dt.time
diff = col1-col2
how ever im getting error -: not supported from datetime.date to datetime.date
解决方案
正如@CoryKramer 所说,您可以使用.sub:
分钟:
col1.sub(col2).dt.seconds/60
0 1439.983333
1 1439.983333
2 1439.983333
dtype: float64
如果您想要更精确,以微秒为单位:
col1.sub(col2).dt.microseconds
0 756000
1 343000
2 342000
dtype: int64
推荐阅读
- c# - Unity DateTime 在 JsonUtility.FromJsonOverwrite 之后为空
- r - 将数据从 R 汇总对象提取到表中
- groovy - 为什么使用 MOP 的 Groovy metaClass .static 更改未按预期运行
- pine-script - Pinescript,如何设置和指标值仅在进入新仓位而不在指标变化时修改?
- artifactory - Artifactory 到 Artifactory 远程仓库
- svg - 用本地 SVG 替换标题中的 Font Awesome
- mysql - 错误 #1064 MySQL ''accesslogh' 附近的语法有问题
- php - 选择选项后如何获取 CHtml::dropDownList 值
- arm - 编程外部 QSPI 闪存 Raspberry Pi Pico
- sql - 选择按日期排序时显示值更改的查询