Equivalent function of datenum(datestring) of Matlab in Python
Solution 1
I would use the datetime module and the toordinal() function
from datetime import date
print date.toordinal(date(1970,1,1))
719163
To get the date you got you would use
print date.toordinal(date(1971,1,2))
719529
or for easier conversion
print date.toordinal(date(1970,1,1))+366
719529
I believe the reason the date is off is due to the fact datenum starts its counting from january 0, 0000 which this doesn't recognize as a valid date. You will have to counteract the change in the starting date by adding one to the year and day. The month doesn't matter because the first month in datetime is equal to 0 in datenum
Solution 2
The previous answers return an integer. MATLAB's datenum does not necessarily return an integer. The following code retuns the same answer as MATLAB's datenum:
from datetime import datetime as dt
def datenum(d):
return 366 + d.toordinal() + (d - dt.fromordinal(d.toordinal())).total_seconds()/(24*60*60)
d = dt.strptime('2019-2-1 12:24','%Y-%m-%d %H:%M')
dn = datenum(d)
Solution 3
You can substract date
objects in Python:
>>> date(2015, 10, 7) - date(1, 1, 1)
datetime.timedelta(735877)
>>> (date(2015, 10, 7) - date(1, 1, 1)).days
735877
Just take care to use an epoch that is useful to your needs.
Related videos on Youtube
yusuf
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life...
Updated on September 15, 2022Comments
-
yusuf over 1 year
In Matlab, when I run "datenum" function as the following;
datenum(1970, 1, 1);
I get the following output:
719529
I'm trying to find the equivalent function or script which is gonna give me the same output. But, unfortunately I couldn't find an enough explanation on the internet to do this.
I have looked at this tutorial: https://docs.python.org/2/library/datetime.html, but it didn't help.
Could you tell me, how can I get the same output in python?
Thanks,
-
yusuf over 8 yearsSo, will I be able to add datestring as a parameter in "toordinal()" function?
-
horns over 8 years
date.toordinal(date(1970, 1, 1))
gives a result of719163
, which does not match the result in the question -
Psytho over 8 yearsPlease show how do you get output
719529
with input1970, 1, 1
using your command. -
yusuf over 8 yearsHello Kay, do you know why do I get "'datetime.date' object has no attribute 'days'" error? :)
-
yusuf over 8 yearsThanks for your answers, horns and Alex.S. Currently I don't have Matlab in my computer, that's why I'm using online matlab compiler on this link: octave-online.net And I get this result on that page.
-
kay over 8 yearsProbably you missed the parentheses.
date(...) - date(...) → timedelta
, andtimedelta
has adays
attribute. -
SirParselot over 8 yearsThis gives almost the same answer as mine which isn't right either. Any idea as to why?
-
yusuf over 8 yearsThanks for your answer @SirParselot. Your answer is true. There is a 366 difference between MATLAB and Python.
-
kay over 8 years@SirParselot, yes, we both use
0001-01-01
as epoch, but Matlab uses0000-00-00
which I assume is December 31th 1 BC. That's why I said "Just take care to use an epoch that is useful to your needs", because Matlab's epoch is strange. :) -
MeatBALL about 2 yearsJust wanted to say that your solution really helped me