使用 Pandas Dataframe 对可变时间段的天气数据进行重采样
Resampling of Weather Data for variable timeperiods by using Pandas Dataframe
我一直在尝试创建一个通用的天气导入器,它可以对数据进行重新采样以设置间隔(例如,从 20 分钟到几小时等(我在下面的代码中使用了 60 分钟))。
为此,我想使用 Pandas 重采样函数。经过一番困惑后,我想出了下面的代码(这不是最漂亮的代码)。我遇到了一个关于设定时间段内风向平均的问题,我试图用 pandas' resampler.apply.
来解决这个问题
但是,我遇到了一个定义问题,它给出了以下 错误:
类型错误:无法将复数转换为浮点数
我意识到我正在尝试将方钉强行插入圆孔中,但我不知道如何克服这个问题。任何提示将不胜感激。
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 度)划分给出了一个奇怪的结果,这是我偶然发现的。我不得不扣除一个小值,这会给实际结果带来一定程度的误差。
我仍然有兴趣知道:
- 复杂的数学题做错了什么
- 对角(180度)更好的解决方案
# changed the imports
from math import sin,cos,atan2,pi
import numpy as np
#changed the definition
def custom_resampler(angles,weights=0,setting='degrees'):
'''computes the mean angle'''
if weights==0:
weights=np.ones(len(angles))
sumsin=0
sumcos=0
if setting=='degrees':
angles=np.array(angles)*pi/180
for i in range(len(angles)):
sumsin+=weights[i]/sum(weights)*sin(angles[i])
sumcos+=weights[i]/sum(weights)*cos(angles[i])
average=atan2(sumsin,sumcos)
if setting=='degrees':
average=average*180/pi
if average == 180 or average == -180: #added since 290 degrees and 110degrees average gave a weird outcome
average -= 0.1
elif average < 0:
average += 360
return round(average,1)
我一直在尝试创建一个通用的天气导入器,它可以对数据进行重新采样以设置间隔(例如,从 20 分钟到几小时等(我在下面的代码中使用了 60 分钟))。 为此,我想使用 Pandas 重采样函数。经过一番困惑后,我想出了下面的代码(这不是最漂亮的代码)。我遇到了一个关于设定时间段内风向平均的问题,我试图用 pandas' resampler.apply.
来解决这个问题但是,我遇到了一个定义问题,它给出了以下 错误: 类型错误:无法将复数转换为浮点数
我意识到我正在尝试将方钉强行插入圆孔中,但我不知道如何克服这个问题。任何提示将不胜感激。
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 度)划分给出了一个奇怪的结果,这是我偶然发现的。我不得不扣除一个小值,这会给实际结果带来一定程度的误差。
我仍然有兴趣知道:
- 复杂的数学题做错了什么
- 对角(180度)更好的解决方案
# changed the imports
from math import sin,cos,atan2,pi
import numpy as np
#changed the definition
def custom_resampler(angles,weights=0,setting='degrees'):
'''computes the mean angle'''
if weights==0:
weights=np.ones(len(angles))
sumsin=0
sumcos=0
if setting=='degrees':
angles=np.array(angles)*pi/180
for i in range(len(angles)):
sumsin+=weights[i]/sum(weights)*sin(angles[i])
sumcos+=weights[i]/sum(weights)*cos(angles[i])
average=atan2(sumsin,sumcos)
if setting=='degrees':
average=average*180/pi
if average == 180 or average == -180: #added since 290 degrees and 110degrees average gave a weird outcome
average -= 0.1
elif average < 0:
average += 360
return round(average,1)