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

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

问题描述

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

<块引用>

序列日期编号表示日历日期,即自固定基准日期以来经过的天数.在 MATLAB 中,序列号 1 是 0000 年 1 月 1 日.

MATLAB 还使用串行时间来表示从午夜开始的天数;例如,下午 6 点等于 0.75 个连续天数.所以 MATLAB 中的字符串 '31-Oct-2003, 6:00 PM' 是日期编号 731885.75.

(取自 Matlab 文档)

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

<块引用>

如果您使用 python 的 datetime.fromordinal(731965.04835648148) 解析它,那么结果可能看起来合理 [...]

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

<预><代码>>>>datetime.fromordinal(731965.04835648148)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:应为整数参数,得到浮点数

虽然我可以将它们四舍五入以获得每日数据,但我实际上需要导入每分钟的时间序列.有没有人有解决这个问题的方法?我想避免重新格式化我的 .mat 文件,因为我的 .mat 文件很多,我的同事也需要使用它们.

如果有帮助,其他人要求相反.遗憾的是,我对 Python 太陌生了,无法真正理解那里发生的事情.

/edit (2012-11-01):这已在上面发布的教程中修复.

解决方案

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

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

可以在这里找到更长的解释

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:

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 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.

(taken from the Matlab documentation)

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

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

(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

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.

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

a longer explanation can be found here

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

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