我对pyhton的编程很新,我希望你们中的任何一个人都有心情帮助我。
嗯,我有许多不同的气候站,在1分钟内也可以在10分钟的时间内完成太阳辐射测量。测量值还包含Na值。 现在我想用15分钟和60分钟的时间分辨率来计算平均值,但是应该考虑到数据缺口的长度。如果底层时间跨度中的数据间隙大于此时间跨度中的可用值的相对数量(例如20%),则不执行任何其他操作,否则构建平均值。 例如: - 12点的小时平均值应为NA,因为基础时间跨度中有50%的NA
09.08.2011 11:10 553
09.08.2011 11:20 567
09.08.2011 11:30 NA
09.08.2011 11:40 NA
09.08.2011 11:50 NA
09.08.2011 12:00 NA
1点的平均值应该是NA导致100%的NA(参见下面的数据示例)
2点的平均值应为210.6,因为基础时间内只有16.7%的NA
我的数据如下所示:
09.08.2011 10:00 189
09.08.2011 10:10 337
09.08.2011 10:20 567
09.08.2011 10:30 432
09.08.2011 10:40 634
09.08.2011 10:50 965
09.08.2011 11:00 897
09.08.2011 11:10 553
09.08.2011 11:20 567
09.08.2011 11:30 NA
09.08.2011 11:40 NA
09.08.2011 11:50 NA
09.08.2011 12:00 NA
09.08.2011 12:20 NA
09.08.2011 12:30 NA
09.08.2011 12:40 NA
09.08.2011 12:50 NA
09.08.2011 13:00 NA
09.08.2011 13:10 NA
09.08.2011 13:20 445
09.08.2011 13:30 115
09.08.2011 13:40 34
09.08.2011 13:50 128
09.08.2011 14:00 331
import pandas as pd
import numpy as np
df_csv_data = pd.io.parsers.read_csv(station_path, skiprows=5, parse_dates= True, index_col=0, na_values=[-999], names= names_header , sep=' ', header=None , squeeze=True)
ts15 = df_csv_data.resample('15Min', how='mean')
ts60 = df_csv_data.resample('60Min', how='mean')
我想用相对数量的数据间隙解决这个问题,导致不同的所需时间分辨率。
有人有想法解决这个问题吗?
非常感谢提前!
steff
`
答案 0 :(得分:1)
# Setup problem
import pandas as pd
import numpy as np
num_samples = 100
s = pd.Series(np.random.randint(0, 500, num_samples), index=pd.date_range('03/06/2015', periods=num_samples, freq='10min'))
mask = np.random.rand(num_samples) < .7
s[mask] = np.nan
# Loop through index
# Note the perc_nan variable can be changed depending on what percentage of the interval must be nan for the mean value to also be nan
perc_nan = 0.5
data, indices = [], []
for dt in s.index:
if dt.minute == 0:
d = s[('00:00:00' <= dt - s.index) & (dt - s.index < '01:00:00')]
data.append(d.mean() if d.isnull().sum() <= len(d)*perc_nan else np.nan)
indices.append(dt)
# Solution
pd.Series(data, index=indices)