繁体   English   中英

特定时间段内的平均值

[英]Average over a specific time period

我在 python 中从 .h5 文件中有一个非常大的表表的开头看起来有点像这样:

table =
                [WIND REL DIRECTION  [deg]]  [WIND SPEED  [kts]]  \
735381.370833                            0             0.000000   
735381.370845                            0             0.000000   
735381.370880                            0             0.000000   
735381.370891                            0             0.000000   
735381.370903                            0             0.000000   
735381.370972                            0             0.000000   
735381.370984                            0             0.000000   
735381.370995                            0             0.000000   
735381.371007                            0             0.000000   
735381.371019                            0             0.000000   
...

索引行是数据的时间戳。 我需要每 15 秒计算一次平均 WIND REL SPEED 和 WIND SPEED,并将其变成一行。 我真的需要以一种有效的方式做到这一点,这个 .h5 文件很大。

下面是一些相关的代码:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pylab import *
import matplotlib.dates as pltd
import tables

pltd.num2date(table.index) #to turn the timestamp into a date

我在这里很无能,感谢所有帮助。

resample是你的朋友。

idx = pltd.num2date(table.index)
df = pd.DataFrame({'direction': np.random.randn(10), 
                   'speed': np.random.randn(10)}, 
                  index=idx)

>>> df
                                  direction     speed
2014-05-28 08:53:59.971204+00:00   0.205429  0.699439
2014-05-28 08:54:01.008002+00:00   0.383199 -0.392261
2014-05-28 08:54:04.031995+00:00  -2.146569 -0.325526
2014-05-28 08:54:04.982402+00:00   1.572352  1.289276
2014-05-28 08:54:06.019200+00:00   0.880394 -0.440667
2014-05-28 08:54:11.980795+00:00  -1.343758  0.615725
2014-05-28 08:54:13.017603+00:00  -1.713043  0.552017
2014-05-28 08:54:13.968000+00:00  -0.350017  0.728910
2014-05-28 08:54:15.004798+00:00  -0.619273  0.286762
2014-05-28 08:54:16.041596+00:00   0.459747  0.524788

>>> df.resample('15S', how='mean') # how='mean' is the default here
                           direction     speed
2014-05-28 08:53:45+00:00   0.205429  0.699439
2014-05-28 08:54:00+00:00  -0.388206  0.289639
2014-05-28 08:54:15+00:00  -0.079763  0.405775

性能类似于@LondonRob 提供的方法。 我使用了一个包含 100 万行的 DataFrame 进行测试。

df = pd.DataFrame({'direction': np.random.randn(1e6), 'speed': np.random.randn(1e6)}, index=pd.date_range(start='2015-1-1', periods=1e6, freq='1S'))

>>> %timeit df.resample('15S')
100 loops, best of 3: 15.6 ms per loop

>>> %timeit df.groupby(pd.TimeGrouper(freq='15S')).mean()
100 loops, best of 3: 15.7 ms per loop

我认为这是做到这一点的“正确”方式。 (虽然对我来说似乎有点记录不足。无论如何它都有效!)

您需要对 DataFrame 进行groupby并使用称为TimeGrouper东西。

它是这样工作的:

import pandas as pd
import numpy as np

# Create a dataframe. You can ignore all this bit!
periods = 60 * 60
random_dates = pd.date_range('2015-12-25', periods=periods, freq='s')
random_speeds = np.random.randint(100, size=periods)
random_directions = np.random.random(periods)
df = pd.DataFrame({'date': random_dates, 'wind_speed': random_speeds, 'wind_direction': random_directions})
df = df.set_index('date')

# Here's where the magic happens:
grouped15s = df.groupby(pd.TimeGrouper(freq='15S'))
averages_ws_15s = grouped15s.wind_speed.mean()

或者,如果您坚持在列名中包含空格,则最后一行将变为:

averages_ws_15s = grouped15s['Wind Speed'].mean()

这导致以下结果:

date
2015-12-25 00:00:00    45.800000
2015-12-25 00:00:15    48.466667
2015-12-25 00:00:30    38.066667
2015-12-25 00:00:45    54.866667
2015-12-25 00:01:00    34.866667
2015-12-25 00:01:15    37.000000
2015-12-25 00:01:30    47.133333
etc....                etc....

暂无
暂无

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

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