I have the following data
Duration
0 00:00:00
1 00:00:00
2 00:00:57
3 00:03:16
4 00:00:00
And Duration
is stored as an object. I would like to convert this into an integer having seconds. for eg 00:03:16 gets converted into 196. I tried various things like astype(timedelta64[s])
but no success. I tried extracting the minutes and seconds and tried converting to integer, that also did not yield results. I am unable to convert the extracted string into an integer.
The to_timedelta() function is used to convert argument to datetime. Timedeltas are absolute differences in times, expressed in difference units (e.g. days, hours, minutes, seconds). This method converts an argument from a recognized timedelta format / value into a Timedelta type. The data to be converted to timedelta.
Use the timedelta() constructor and pass the seconds value to it using the seconds argument. The timedelta constructor creates the timedelta object, representing time in days, hours, minutes, and seconds ( days, hh:mm:ss.ms ) format.
You can access various components of the Timedelta or TimedeltaIndex directly using the attributes days,seconds,microseconds,nanoseconds . These are identical to the values returned by datetime. timedelta , in that, for example, the . seconds attribute represents the number of seconds >= 0 and < 1 day.
How do you convert Timedelta to INT? You can use the following methods to convert a timedelta column to an integer column in a pandas DataFrame: Method 1: Convert Timedelta to Integer (Days) df['days'] = df['timedelta_column']. Method 2: Convert Timedelta to Integer (Hours) df['hours'] = df['timedelta_column'] / pd.
in case of series,
s.map(lambda x: pd.to_timedelta(x).seconds)
dataframe,
df.applymap(lambda x: pd.to_timedelta(x).seconds)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With