我一直在尝试创建一个通用的天气导入器,它可以对数据进行重新采样以设置时间间隔(例如,从20分钟到小时或类似时间(我在下面的代码中使用了60分钟))。 为此,我想使用熊猫重采样功能。经过一点困惑之后,我想出了下面的代码(这不是最漂亮的代码)。我有一个问题是在设定的周期内平均风向,我试图用pandas的重采样器来解决这个问题
但是,我遇到了一个定义问题,它给出了以下错误: TypeError:无法将复数转换为浮点
我意识到我想把一个方形的钉子塞进一个圆形的洞里,但我不知道如何克服这个问题。如有任何提示,将不胜感激
import pandas as pd
import os
from datetime import datetime
from pandas import ExcelWriter
from math import *
os.chdir('C:\\test')
file = 'bom.csv'
df = pd.read_csv(file,skiprows=0, low_memory=False)
#custom dataframe reampler (.resampler.apply)
def custom_resampler(thetalist):
try:
s=0
c=0
n=0.0
for theta in thetalist:
s=s+sin(radians(theta))
c=c+cos(radians(theta))
n+=1
s=s/n
c=c/n
eps=(1-(s**2+c**2))**0.5
sigma=asin(eps)*(1+(2.0/3.0**0.5-1)*eps**3)
except ZeroDivisionError:
sigma=0
return degrees(sigma)
# create time index and format dataframes
df['DateTime'] = pd.to_datetime(df['DateTime'],format='%d/%m/%Y %H:%M')
df.index = df['DateTime']
df = df.drop(['Year','Month', 'Date', 'Hour', 'Minutes','DateTime'], axis=1)
dfws = df
dfwdd = df
dfws = dfws.drop(['WDD'], axis=1)
dfwdd = dfwdd.drop(['WS'], axis=1)
#resample data to xxmin and merge data
dfwdd = dfwdd.resample('60T').apply(custom_resampler)
dfws = dfws.resample('60T').mean()
dfoutput = pd.merge(dfws, dfwdd, right_index=True, left_index=True)
# write series to Excel
writer = pd.ExcelWriter('bom_out.xlsx', engine='openpyxl')
dfoutput.to_excel(writer, sheet_name='bom_out')
writer.save()
做了更多的研究,发现改变定义效果最好。 然而,这给了一个奇怪的结果,相反的角度(180度)划分,这是我偶然发现的。我不得不扣除一个很小的值,这将在实际结果中给出一个程度误差
我仍有兴趣知道:
相关问题 更多 >
编程相关推荐