将Matlab的datenum格式转换为Python [英] Converting Matlab's datenum format to Python

查看:334
本文介绍了将Matlab的datenum格式转换为Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始从Matlab迁移到Python 2.7,但在读取.mat文件时遇到了一些麻烦.时间信息以Matlab的datenum格式存储.对于那些不熟悉它的人:

I just started moving from Matlab to Python 2.7 and I have some trouble reading my .mat-files. Time information is stored in Matlab's datenum format. For those who are not familiar with it:

序列号代表日历日,表示自固定基准日期以来已过去的天数.在MATLAB中,序列号1为0000年1月1日.

A serial date number represents a calendar date as the number of days that has passed since a fixed base date. In MATLAB, serial date number 1 is January 1, 0000.

MATLAB还使用串行时间来表示从午夜开始的几分之一秒;例如下午6点等于0.75个连续日.因此,MATLAB中的字符串"2003年10月31日,下午6:00"是日期号731885.75.

MATLAB also uses serial time to represent fractions of days beginning at midnight; for example, 6 p.m. equals 0.75 serial days. So the string '31-Oct-2003, 6:00 PM' in MATLAB is date number 731885.75.

(摘自Matlab文档)

(taken from the Matlab documentation)

我想将此转换为Pythons时间格式,发现本教程.简而言之,作者指出

I would like to convert this to Pythons time format and I found this tutorial. In short, the author states that

如果您使用python的datetime.fromordinal(731965.04835648148)进行解析,则结果可能看起来很合理[...]

If you parse this using python's datetime.fromordinal(731965.04835648148) then the result might look reasonable [...]

(在进行任何进一步的转换之前),这对我不起作用,因为datetime.fromordinal需要一个整数:

(before any further conversions), which doesn't work for me, since datetime.fromordinal expects an integer:

>>> datetime.fromordinal(731965.04835648148) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: integer argument expected, got float

虽然我可以将它们四舍五入以获取每日数据,但实际上我需要导入微小的时间序列.有人对此问题有解决方案吗?我想避免重新格式化.mat文件,因为其中有很多文件,而我的同事也需要使用它们.

While I could just round them down for daily data, I actually need to import minutely time series. Does anyone have a solution for this problem? I would like to avoid reformatting my .mat files since there's a lot of them and my colleagues need to work with them as well.

如果有帮助,请有人要求另一种方法.可悲的是,我对Python来说还太陌生,无法真正了解那里发生的事情.

If it helps, someone else asked for the other way round. Sadly, I'm too new to Python to really understand what is happening there.

/edit(2012-11-01):在上面发布的教程中已解决此问题.

/edit (2012-11-01): This has been fixed in the tutorial posted above.

推荐答案

您链接到解决方案,它有一个小问题.就是这样:

You link to the solution, it has a small issue. It is this:

python_datetime = datetime.fromordinal(int(matlab_datenum)) + timedelta(days=matlab_datenum%1) - timedelta(days = 366)

可以在此处

这篇关于将Matlab的datenum格式转换为Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆