简体   繁体   English

Python:如何使具有不同单位的两个时间数组具有可比性?

[英]Python: How do I make two time arrays with different units comparable?

I have the following problem: 我有以下问题:

I have one time array with " Hours since 1900-01-01 00:00:0.0 " as unit and I want to compare it with a time array with " Seconds since midnight 2016-10-01 00:00:0.0 " as unit. 我有一个时间单位为“ 自1900-01-01小时的小时数 ”的时间数组,我想将其与时间单位为“ 自午夜以来的秒数2016-10-01 00:00:0.0 ”作为单位的时间数组进行比较。 Is there a python function which makes both time arrays comparable? 是否存在使两个时间数组具有可比性的python函数?

The motivation behind this task is to compare atmospheric variables derived from aircraft measurements with those from ERA5 reanalysis. 该任务背后的动机是将飞机测量得出的大气变量与ERA5重新分析得出的变量进行比较。

Thanks! 谢谢!

You could transform both to appropriate datetime instances, using your base date and adding the according deltas: 您可以使用基准日期并添加相应的增量,将它们都转换为适当的datetime实例:

from datetime import datetime, timedelta

a1 = [datetime(1900, 1, 1)) + timedelta(hours=float(t1)) for t1 in time_array_1]
a2 = [datetime(2016, 10, 1)) + timedelta(seconds=float(t2)) for t2 in time_array_2]

The resulting datetime objects are easily comparable via < , > , etc. and you can get their difference as timedelta objects using the normal subtraction operator - . 生成的datetime对象可以通过<>等轻松地进行比较,并且您可以使用普通的减法运算符-来将它们作为timedelta对象进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM