0

I am performing a data analysis with python. I need to convert a data type from datatime.time to float, something like what makes Excel when we change the cell format from "time" to "number".

enter image description here

I could load the data on excel, change the columns format, export them again to CSV and finally load again into a dataframe, but I don't want to perform all that process again.

Thanks for your help!

3
  • 1
    How do you want to convert it to float? Do you want the representation of time as seconds from the epoch? As the fraction of the day that has passed? Or what? Commented Sep 16, 2016 at 16:17
  • Also: which day? synodic day? Sidereal day? Commented Sep 16, 2016 at 16:23
  • @Bakuriu it seems clear from the question that they want the same decimal number that Excel displays, however it calculates it. Commented Sep 16, 2016 at 16:27

1 Answer 1

2

The decimal number used by Excel is simply the fraction of a day that a time represents, with midnight being 0.0. You simply take the hours, minutes, and seconds in the time and divide by the fraction of a day they represent:

def excel_time(time):
    return time.hour / 24.0 + time.minute / (24.0*60.0) + time.second / (24.0*60.0*60.0) + time.microsecond / (24.0*60.0*60.0*1000000.0)
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.