MATLAB datenum日期转换为Python日期

目录

  • 摘要
  • 示例1:小时制,基准年份1800-1-1
  • 示例2:一天制,基准年份1800-1-1
  • 示例3:时间列表转换
  • 截图(参考)
  • 参考

摘要

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)
'''

截图(参考)

验证:

参考

  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

原文地址:https://www.cnblogs.com/oucbl/p/11618709.html

时间: 2024-08-02 05:57:21

MATLAB datenum日期转换为Python日期的相关文章

Json格式日期转换为一般日期

Json日期转换为一般日期:json日期:/Date(1316756746000)/ 转换为2013-09-01格式的 1 //将json格式的时间转换成一般时间 2 function ChangeDateFormat(jsondate) { 3 jsondate = jsondate.replace("/Date(", "").replace(")/", ""); 4 if (jsondate.indexOf("+

Python 日期和时间 —— datetime

Python 日期和时间 —— datetime Python提供了多个内置模块用于操作日期时间,如calendar,time,datetime.time提供的接口与C标准库time.h基本一致.其中应用最广的是datetime,相比于time模块,datetime模块的接口则更直观.更容易调用. datetime 模块为日期和时间处理同时提供了简单和复杂的方法.支持日期和时间算法的同时,实现的重点在于更有效的处理和格式化输出.该模块还支持时区处理.本文对 datetime库进行学习. date

python日期转化

1.将字符串的时间转换为时间戳     方法:         a = "2013-10-10 23:40:00"         将其转换为时间数组         import time         timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")     转换为时间戳:     timeStamp = int(time.mktime(timeArray))     timeStamp == 13814196

python日期格式化操作

1.将字符串的时间转换为时间戳 方法: a = "2013-10-10 23:40:00" #将其转换为时间数组 import time timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S") #转换为时间戳: timeStamp = int(time.mktime(timeArray)) timeStamp == 1381419600 2.格式更改如a = "2013-10-10 23:40:00",

14、python 日期和时间

Python  日期和时间 Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 时间间隔是以秒为单位的浮点小数. 每个时间戳都以自从1970年1月1日午夜(历元)经过了多长时间来表示. Python 的 time 模块下有很多函数可以转换常见日期格式.如函数time.time()用于获取当前时间戳, 如下实例: #!/usr/bin/python # -*- coding: UTF

python 日期、时间、字符串相互转换

python 日期.时间.字符串相互转换 在python中,日期类型date和日期时间类型dateTime是不能比较的. (1)如果要比较,可以将dateTime转换为date,date不能直接转换为dateTime import datetime dateTime_p = datetime.datetime.now() date_p = dateTime_p.date() print(dateTime_p) #2019-01-30 15:17:46.573139 print(date_p) #

Python 日期和时间(转)

Python 日期和时间 Python程序能用很多方式处理日期和时间.转换日期格式是一个常见的例行琐事.Python有一个 time 和 calendar 模组可以帮忙. 什么是Tick? 时间间隔是以秒为单位的浮点小数. 每个时间戳都以自从1970年1月1日午夜(历元)经过了多长时间来表示. Python附带的受欢迎的time模块下有很多函数可以转换常见日期格式.如函数time.time()用ticks计时单位返回从12:00am, January 1, 1970(epoch) 开始的记录的当

python 日期格式化常用标记

符号 说明             例子    %a  英文星期的简写 Mon  %A  英文星期的完整编写 Monday  %b  英文月份的简写 Jun  %B  英文月份的完整编写 June  %c  显示本地的日期和时间 06/30/14 01:03:17  %I  小时数,取值在01~12之间 01  %j  显示从本年第一天开始到当天的天数 181  %w (week)  显示今天是星期几,0表示星期天 1  %W  显示当天属于本年的第几周,星期一作为一周的第一天进行计算 26  

Java 毫秒转换为日期类型、日期转换为毫秒

//毫秒转换为日期 public static void main(String[] args) {DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); long now = System.currentTimeMillis(); Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(now); System.out.pri