如何在具有groupby的pandas中停止log10中遇到的RuntimeWarning(:除以0)



我有一些数据。我按日期分组,然后计算对数。当只有由于过滤器而被排除的数据时,就会出现警告。下面是我的代码示例:

import pandas as pd
import numpy as np
#example of datas
df = pd.DataFrame(np.array([["2021-12-18 02:00:01", 65.4, 3],  
["2021-12-18 05:00:00", 67.9, 6], 
["2021-12-18 07:00:02", 61.9, 9], 
["2021-12-18 07:30:42", 63.4, 6], 
["2021-12-18 07:42:10", 86.9, 9], 
["2021-12-18 10:00:07", 71.0, 6], 
["2021-12-18 11:15:03", 66.3, 9],
["2021-12-18 14:01:03", 71.5, 6], 
["2021-12-18 22:25:06", 63.9, 9], 
["2021-12-18 23:51:01", 68.0, 6], 
["2021-12-19 02:00:01", 59.4, 3], 
["2021-12-19 05:00:00", 65.0, 6], 
["2021-12-19 07:00:02", 65.5, 9],
["2021-12-19 07:30:42", 67.2, 6], 
["2021-12-19 07:42:10", 80.2, 9],
["2021-12-19 10:00:07", 65.2, 6], 
["2021-12-19 11:15:03", 70.1, 9],
["2021-12-19 14:01:03", 69.4, 6], 
["2021-12-19 22:25:06", 64, 9],
["2021-12-19 23:51:01", 72.6, 6], 
["2021-12-21 02:00:01", 67.9, 1], 
["2021-12-21 05:00:00", 69.4, 1], 
["2021-12-21 07:00:02", 65.1, 1],
["2021-12-21 07:30:42", 83.4, 1], 
["2021-12-21 07:42:10", 70.2, 1],
["2021-12-21 10:00:07", 64.3, 1], 
["2021-12-21 11:15:03", 69.1, 1],
["2021-12-21 14:01:03", 59.9, 1], 
["2021-12-21 22:25:06", 64.3, 1],
["2021-12-21 23:51:01", 68.9, 1]]),
columns=['Datum', 'SEL', 'divers'])
#determine Type
df[['Datum']] = df[['Datum']].apply(pd.to_datetime) 
df[['SEL']] = df[['SEL']].apply(pd.to_numeric) 
df[['divers']] = df[['divers']].apply(pd.to_numeric) 
#Detemine index
df=df.set_index("Datum")

#group by
final=df.groupby([df.index.date]).count()

#first step of the formula with the filter
df["SEL_Temp"]=10**(df.loc[(df['divers']>=3)]["SEL"]/10)
#groupby and second step of the formula
final["LspAv"]=10*np.log10((1/86400)*(df.groupby([df.index.date])["SEL_Temp"].sum()))

由于过滤器的关系,警告出现在最后一行。

如何避免警告?

感谢

您可以使用seterr来抑制警告,如下所示:

np.seterr(divide='ignore')

你可以把这个语句放在导入库之后。

相关内容

最新更新