如何格式化 Pandas timedelta 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41782920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Do I Format a pandas timedelta object?
提问by psychemedia
I am using pandastimedelta
objects to keep track of split times in a sports related data set using the following sort of construction:
我正在使用Pandastimedelta
对象来跟踪体育相关数据集中的分割时间,使用以下类型的构造:
import pandas as pd
pd.to_timedelta("-0:0:1.0")
This natively reports as:
这本机报告为:
-1 days +23:59:59
-1 days +23:59:59
I can get the raw seconds count using pd.to_timedelta("-0:0:1.0").total_seconds()
but that is unwieldy where the negative amount is in minutes or hours:
我可以使用原始秒数计算,pd.to_timedelta("-0:0:1.0").total_seconds()
但如果负数以分钟或小时为单位,那就很笨拙了:
For the expression:
对于表达式:
pd.to_timedelta("-1:2:3.0")
how can I get the report formatted as"-1:2:3.0
, or -1 hour, 2 minutes, 3 seconds
, from the timedelta
object, rather than in the form -3723.0000000000005
(with a float error) or -1 days +22:57:57
?
如何从对象中获取格式为"-1:2:3.0
, 或的报告,而不是格式(带有浮点错误)或?-1 hour, 2 minutes, 3 seconds
timedelta
-3723.0000000000005
-1 days +22:57:57
采纳答案by psychemedia
See the function strfdelta(tdelta, fmt)
provided as an answer to a related question: https://stackoverflow.com/a/8907269/454773
请参阅strfdelta(tdelta, fmt)
作为相关问题答案提供的函数:https: //stackoverflow.com/a/8907269/454773
回答by MaxU
Is that what you want?
那是你要的吗?
In [223]: df
Out[223]:
delta
0 -1 days +23:59:59
1 -1 days +22:57:57
2 00:00:11
In [224]: df.delta.abs().dt.components
Out[224]:
days hours minutes seconds milliseconds microseconds nanoseconds
0 0 0 0 1 0 0 0
1 0 1 2 3 0 0 0
2 0 0 0 11 0 0 0