1

I am new to Pandas and python, I have a CSV file which contains a duration column ( Time ) when I read the file it's read-only as a string and I can't sum it:

agents['Avg Handle Time'][1:10]
Out[12]: 
1          -
2    0:05:07
3          -
4          -
5          -
6    0:05:03
7          -
8          -
9          -
Name: Avg Handle Time, dtype: object
Ahmed
  • 11
  • 2
  • You can look into the module `datetime`. You can convert strings to datetime objects and then you can do calculations on datetime objects such as summing(substracting dates. The first step is described here https://stackoverflow.com/questions/466345/converting-string-into-datetime – v.tralala Nov 27 '19 at 21:22

3 Answers3

1

I think you just need to make it a timedelta column

pandas.to_timedelta(df['Avg Handle Time'])

pandas.to_timedelta(df['Avg Handle Time']).to_series().sum()
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
1

when you read your csv file, you need to parse the dates so they can be read as such. For example:

df = pd.read_csv('your_path.csv', parse_dates=['Avg Handle Time'])

After doing this, the type of your column will be date-type. Once you have done this try to sum it.

0

You need to convert the column from string/object to timedelta type and then just sum it up

    df = pd.DataFrame({"AvgHandleTime": ['-', '0:05:07', '-', '-', '-', '0:05:03', '-', '-']})
    pd.to_timedelta(df['AvgHandleTime']).sum()
exan
  • 3,236
  • 5
  • 24
  • 38