Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Converting an object into timedelta

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.

like image 836
venkateshwarlu Sonathi Avatar asked Mar 25 '16 04:03

venkateshwarlu Sonathi


People also ask

How do I convert to Timedelta?

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.

How do you convert seconds to HH MM SS in pandas?

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.

How do you use pandas Timedelta?

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 I convert Timedelta to INT panda?

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.


1 Answers

in case of series,

s.map(lambda x: pd.to_timedelta(x).seconds)

dataframe,

df.applymap(lambda x: pd.to_timedelta(x).seconds)
like image 191
su79eu7k Avatar answered Sep 24 '22 09:09

su79eu7k