0

Python summing up time - In this link, the answers are long coded and also using a small list of time values. However, I would like to learn a pythonic way to sum the time values in a data-frame but i can't seem to figure out yet. Can someone enlighten me? please. By the way, I am using Python3.7 on a Jupyter Notebook. Thanks!

Below are the steps i have performed before trying summarizing:

code i tried to get the difference

Note:

  'difference' is non-null object 

  'in' is non-null datetime64[ns] 

  'out' is non-null datetime64[ns] 

Here is how the input data looks:

Input Dataframe

And here is how I would want the output:

Output Dataframe

1 Answers1

0

I don't see why your solution wouldn't work. Maybe you haven't converted the datetime to the proper type:

df['in'] = pd.to_datetime(df['in'], format="%Y/%m/%d %H:%M") # format needs adjustment

If it still doesnt work, please provide the rest of the code or some raw data.

missurunha
  • 108
  • 6
  • I did convert `df['in]` and `df['out']` into datetime with proper format. But after the difference I end up with `timedelta[ns]` datatype, then i convert it using `df['difference'].dt.strftime('%H:%M:%S')`. Neither of those datatypes give me the result that i wanted, when i apply `sum` using a `groupby` nor a `pivot_table` – Vikram Reddy Oct 10 '19 at 17:07
  • Yes, you get timedelta and it does work with the sum function. `df["difference"].sum()` gives you the sum of time deltas. (it doesnt work with python's built in `sum()` though) Another option is using the seconds property `df["difference"].seconds` and eventually convert it back to datetime if you want to have that format later on. – missurunha Oct 11 '19 at 12:30
  • I get the output like this `1 6 days 14:12:00 2 7 days 06:26:00 3 6 days 03:15:00` when i try `df.groupby('employee')['difference'].sum()`. But I am looking for the output format in `hh:mm:ss` as asked in the original question. – Vikram Reddy Oct 12 '19 at 18:46