2

How could I transform the following float:

9.3125

which corresponds to a time, into a proper Pandas timestamp in the likes of:

Timestamp('2017-11-13 10:00:00')

The float belongs to the same day, month, and year as the example timestamp above. I have been trying this:

from datetime import datetime
datetime.fromtimestamp(9.3125).strftime('%Y-%m-%d %H')

but this returns:

OSError: [Errno 22] Invalid argument

2 Answers 2

2

I believe need Timedelta.ceil:

num = 9.3125
a = pd.Timestamp('2017-11-13')  + pd.to_timedelta(num, unit='H').ceil('H')
print (a)
2017-11-13 10:00:00
Sign up to request clarification or add additional context in comments.

2 Comments

But 9.3125 corresponds to about 09:19, so this solution would let me lose those 19 minutes
If I use float(num) instead of int(num) I get: ValueError: time data '2017-11-13 9.3125' doesn't match format specified.
1

Inspired by the answers above, I have verified that the following works for me:

num = 9.3125
a = pd.Timestamp('2017-11-13')  + pd.to_timedelta(num, unit='H').ceil('min')
print (a)
Timestamp('2017-11-13 09:19:00')

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.