Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a datetime format to minutes - pandas

I have a data frame which has a column usage_duration (which is the difference of two another columns in datetime format). It looks like below:

processid, userid, usage_duration 
17613,root,0 days 23:41:03.000000000
17641,root,2 days 04:05:26.000000000
13848,acs,0 days 00:00:50.000000000
3912,acs,0 days 06:07:38.000000000
6156,acs,0 days 17:22:43.000000000

Now I wanted to convert the same into minutes. It should look like as below:

processid, userid, usage_duration_min
17613,root,1421
17641,root,3125
13848,acs,0
3912,acs,367
6156,acs,1042

Can someone let me know how is it possible?

Highly appreciate your support

like image 935
Jithesh Erancheri Avatar asked May 17 '18 13:05

Jithesh Erancheri


People also ask

How do I change pandas datetime format?

Pandas Convert Date to String Format – To change/convert the pandas datetime ( datetime64[ns] ) from default format to String/Object or custom format use pandas. Series. dt. strftime() method.


2 Answers

Use total_seconds or seconds and divide by 60, last cast to integers:

#if necessary converting to timedelta
#df['usage_duration'] = pd.to_timedelta(df['usage_duration'])

df['new'] = df['usage_duration'].dt.total_seconds().div(60).astype(int)

Or:

 df['new'] = (df['usage_duration'].dt.seconds.div(60).astype(int) 
             + df['usage_duration'].dt.days.multiply(1440).astype(int) )

print (df)
   processid userid  usage_duration   new
0      17613   root 0 days 23:41:03  1421
1      17641   root 2 days 04:05:26  3125
2      13848    acs 0 days 00:00:50     0
3       3912    acs 0 days 06:07:38   367
4       6156    acs 0 days 17:22:43  1042
like image 116
jezrael Avatar answered Oct 11 '22 19:10

jezrael


This is one way:

s = pd.Series(['0 days 23:41:03.000000000', '2 days 04:05:26.000000000',
               '0 days 00:00:50.000000000', '0 days 06:07:38.000000000',
               '0 days 17:22:43.000000000'])

s = pd.to_timedelta(s).astype('timedelta64[m]').astype(int)

print(s)

0    1421
1    3125
2       0
3     367
4    1042
dtype: int32
like image 42
jpp Avatar answered Oct 11 '22 17:10

jpp