How do I export a timedelta operation result to Excel?

Viewed 18

So, I have a column with a start date, and one with a closed date. I want the difference between them so I use:

`df1['Time Resolved'] = abs(df['Closed Datetime']-df['Start Datetime'])`

Then I would like the median value from the Time Resolved column, so I use:

`time_resolved = df1['Time Resolved'].median()`

Which gives me a timedelta result. The issue is that I would like to export this result to Excel but when I use this function:

`df2 = time_resolved`
`df2.to_excel(writer, sheet_name="1", index=True)`

Pandas tells me that timedelta has no attribute 'to_excel'. I've tried converting the result to a string which didn't work either, and I can't find the correct code to convert it to an integer.

Is there a solution to this?

1 Answers

It looks like you're getting an error because the to_excel() function is meant to be called on a DataFrame, not a TimeDelta.

You could create a very basic DataFrame and write it:

df2 = pd.DataFrame({'result': [df1['Time Resolved'].median()]})
df2.to_excel(writer, sheet_name="1")
Related