MATLAB datenum日期轉換為Python日期


摘要

MATLAB datenum時間格式參數眾多,本文只簡單關注 units 參數,即基准年份和計時度量(天、小時)。

命令行演示在 ipython 和 Octave 中進行。

示例1:小時制,基准年份1800-1-1

Time Attributes:
                     units              = 'hours since 1800-1-1 00:00:0.0'
                     long_name          = 'Time'
                     axis               = 'T'
                     standard_name      = 'time'
                     coordinate_defines = 'start'
                     delta_t            = '0000-00-01 00:00:00'
                     actual_range       = [1.74e+06 1.75e+06]
                     avg_period         = '0000-00-01 00:00:00'
time(1:5):
     1744392
     1744416
     1744440
     1744464
     1744488
#來源: 參考1

# python
import numpy as np
origin = np.datetime64('1800-01-01', 'D')
date1 = 1744392/24 * np.timedelta64(1, 'D') + origin   # '1999-01-01'

示例2:一天制,基准年份1800-1-1

time {
        String units "days since 1800-1-1 00:00:00";
        String long_name "Time";
        Float64 actual_range 19723.00000000000, 76214.00000000000;
        String delta_t "0000-01-00 00:00:00";
        String avg_period "0000-01-00 00:00:00";
        String prev_avg_period "0000-00-07 00:00:00";
        String standard_name "time";
        String axis "t";
}
#來源: 參考2

# python
import numpy as np
origin = np.datetime64('1800-01-01', 'D')
date2 = 19723 * np.timedelta64(1, 'D') + origin   # '1854-01-01'

示例3:時間列表轉換

import numpy as np
import pandas as pd
datenums = np.array([730990, 733301, 729159,  734471, 736858, 731204]) # 來源:參考7
timestamps = pd.to_datetime(datenums-719529, unit='D')

'''
DatetimeIndex(['2001-05-19', '2007-09-16', '1996-05-14',
               '2010-11-29', '2017-06-12', '2001-12-19'],
              dtype='datetime64[ns]', freq=None)
'''

截圖(僅供參考)

ipython

驗證:

Octave

參考

  1. convert narr model data time to datevec or datestr https://ww2.mathworks.cn/matlabcentral/answers/141304-convert-narr-model-data-time-to-datevec-or-datestr
  2. OPeNDAP-某數據集格式:http://test.opendap.org/dap/data/nc/sst.mnmean.nc.gz.das
  3. Python datetime to Matlab datenum https://stackoverflow.com/questions/8776414/python-datetime-to-matlab-datenum
  4. Converting Matlab's datenum format to Python https://stackoverflow.com/questions/13965740/converting-matlabs-datenum-format-to-python/36249553
  5. [Tutor] datenum https://mail.python.org/pipermail/tutor/2003-September/025454.html
  6. 在日期時間數組、數值和文本之間轉換 https://ww2.mathworks.cn/help/matlab/matlab_prog/convert-between-datetime-arrays-numbers-and-strings.html
  7. datenum用法 https://ww2.mathworks.cn/help/matlab/ref/datenum.html
  8. 日期和時間 https://ww2.mathworks.cn/help/matlab/date-and-time-operations.html?s_tid=CRUX_lftnav


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM