How Do I Format a pandas timedelta object?

psychemedia picture psychemedia · Jan 21, 2017 · Viewed 7.4k times · Source

I am using pandas timedelta objects to keep track of split times in a sports related data set using the following sort of construction:

import pandas as pd
pd.to_timedelta("-0:0:1.0")

This natively reports as:

-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:

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?

Answer

psychemedia picture psychemedia · Mar 14, 2017

See the function strfdelta(tdelta, fmt) provided as an answer to a related question: https://stackoverflow.com/a/8907269/454773