0

I had tried to sum multiple values of same format 'hh:mm:ss' using pandas data series and it's data type is datetime.time/object.but it getting error.could you please guide me best way.

Here is the code:

    import pandas as pd  
    school_diesel =pd.read_excel(r'*********************** Diesel Log 23-09-2019 17_59_05.xlsx',heading=[1,2])  
    school_running = pd.read_excel(r'*************Daily Log 23-09-2019 18_09_41.xlsx',0)  
    school_diesel.columns = school_diesel.iloc[0] # replace headings with next row values  
school_running.columns = school_running.iloc[0] # replace headings with next row values  
school_running.columns  
school_diesel.columns  
school_diesel.drop(school_diesel.head(1).index, inplace=True) #drop first row of the table- as this repeated heading  
school_running.drop(school_running.head(1).index, inplace=True) #drop first row of the table- as this repeated heading  

data types of each field is:

input: school_running.info()  

output: <class 'pandas.core.frame.DataFrame'>  

Int64Index: 12469 entries, 1 to 12469  
Data columns (total 25 columns):  
Sno                       12468 non-null object  
City                    12467 non-null object
Zone                    12467 non-null object
Branch                  12467 non-null object
Building Code           12383 non-null object
 Branch Type            12305 non-null object
AC or Non AC            11405 non-null object
 Student Strength       12467 non-null object
Company Name            12381 non-null object
Gen SNo                 12467 non-null object
Capacity KVA            12381 non-null object
Fuel Capacity           12467 non-null object
Last Diesel Purchase    12467 non-null object
Purchase Qty            12467 non-null object
Amount                  12467 non-null object
Last Fuel Filled        12467 non-null object
Filling Qty             12467 non-null object
Diesel Opening Qty      12467 non-null object
Generator On Date       12467 non-null object
Generator Off Date      12467 non-null object
Running Hours           12466 non-null object
Consumed Units          12466 non-null object
Diesel Consumed         12466 non-null object
Diesel Balance Qty      12466 non-null object
Remarks                 6267 non-null object
dtypes: object(25)
memory usage: 1.3+ MB

error occured at line :

school_running['Running Hours'].sum()

error is :

----> 1 school_running['Running Hours'].sum()  
**
TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.time'  

Expected output is to sum of total Running Hours.

**Time format is : **

school_running['Running Hours'].head(10)

1     00:00:00
2     00:00:00
3     00:25:00
4     00:00:00
5     00:00:00
6     00:00:00
7     00:00:00
8     00:00:00
9     00:00:00
10    01:20:00
Name: Running Hours, dtype: object
6
  • Please share your code, your dataframe, expected output, as much as information as requiered to create stackoverflow.com/help/minimal-reproducible-example Commented Sep 24, 2019 at 15:23
  • updated the code ,data types and expected out in above question itself. Commented Sep 24, 2019 at 17:13
  • You cannot sum the column because it is an object, you need to be in a type that supports the sum opperator. Can you try editing your question to show us what does school_running['Running Hours'] look like? IE: print(school_running['Running Hours'].head() That will give me an idea on what to modify in that column so as to make it able to apply sum(). Commented Sep 24, 2019 at 17:18
  • Refer this answer for help : stackoverflow.com/questions/43479551/… Commented Sep 24, 2019 at 18:07
  • Updated output of "print(school_running['Running Hours'].head(10)" in question. Commented Sep 25, 2019 at 5:23

1 Answer 1

0

It worked. As most of the data has special symbols, that's the reason it's not executed.

With the below code I'm getting desired output.

school_running['Running Hours'].sum()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.