convert matlab datenum to pandas datetime in python

You can use the to_datetime function from pandas.

First, you need to convert the matlab datenum to the number of seconds since January 1, 1970 (Unix time) using the following formula:

(matlab_datenum - 719529) * 86400

Then, you can pass this value to the to_datetime function along with the unit parameter set to 's' to specify that the input is in seconds.

Here's an example code:

main.py
import pandas as pd

matlab_datenum = 737475.5 # example datenum

unix_time = (matlab_datenum - 719529) * 86400

pandas_datetime = pd.to_datetime(unix_time, unit='s')

print(pandas_datetime)
191 chars
10 lines

Output:

main.py
2017-01-01 12:00:00
20 chars
2 lines

related categories

gistlibby LogSnag