Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - How to convert datetime data using toordinal considering the time

Let's assume that I have the following data:

25/01/2000 05:50

When I convert it using datetime.toordinal, it returns this value:

730144

That's nice, but this value just considers the date itself. I also want it to consider the hour and minutes (05:50). How can I do it using datetime?

EDIT:

I want to convert a whole Pandas Series.

like image 954
Paulo Henrique Vasconcellos Avatar asked Nov 17 '25 01:11

Paulo Henrique Vasconcellos


1 Answers

An ordinal date is by definition only considering the year and day of year, i.e. its resolution is 1 day.

You can get the microseconds / milliseconds (depending on your platform) from epoch using

datetime.datetime.strptime('25/01/2000 05:50', '%d/%m/%Y %H:%M').timestamp()

for a pandas series you can do

s = pd.Series(['25/01/2000 05:50', '25/01/2000 05:50', '25/01/2000 05:50'])
s = pd.to_datetime(s) # make sure you're dealing with datetime instances
s.apply(lambda v: v.timestamp())
like image 68
Matti Lyra Avatar answered Nov 18 '25 16:11

Matti Lyra