1

I've got .mat file with datenum values (i.e. 735724.3229166666, 735725.6756944444 and etc.).

My goal is transform it into datetime format without losing precision, in other words it should work as precise as it would've worked with matlab.

I am perfectly aware of those answers: Converting Matlab's datenum format to Python

But none of them work as precise, because python rounds everything up after decimal in almost every numerical operation.

For example, those functions I wrote are based on the answers in the link above:

def datenum_to_datetime1(datenum):

    days = datenum % 1
    return datetime.fromordinal(int(datenum)) \
           + timedelta(days=days) \
           - timedelta(days=366)

def datenum_to_datetime2(datenum):

    days = np.float_(datenum) % 1
    return datetime.fromordinal(int(datenum)) \
           + timedelta(days=days) \
           - timedelta(days=366)

def datenum_to_datetime3(datenum):

    return pd.to_datetime(datenum-719529, unit='D')

def datenum_to_datetime4(datenum):

    origin = np.datetime64('0000-01-01', 'D') - np.timedelta64(1, 'D')
    date = datenum * np.timedelta64(1, 'D') + origin
    return date

If I try them on 735724.3229166666 , I get:

  • 2014-05-05 07:44:59.999997
  • 2014-05-05 07:44:59.999997
  • 2014-05-05 07:45:00.000028800
  • 2014-05-05

Third option is the most precise but with matlab it's still differs. Can someone help?

1 Answers1

0

I assume your input date was 2014-05-05 07:45:00. Let's take a look at the precision in which MATLAB stores the date. It uses a 64bit floating point. At the values you have, the precision in seconds is eps(735724)*24*60*60 or 1.0058e-05. As long as you see errors within (half of) this number, they are already present in your input data and not caused by your conversion. If you need a higher resolution, you need input data with a higher accuracy.

Daniel
  • 36,610
  • 3
  • 36
  • 69