如何在Pandasresample().mean()和resample().sum()时禁用nans计算?

zte4gxcn  于 2023-02-11  发布在  其他
关注(0)|答案(3)|浏览(118)

我需要从月度数据中计算出年平均值。如果月度数据中有一个nan值,我希望全年也是nan。
这是我的代码:

station_data = pd.read_csv(station_data_files[0], sep=';', header=0)
station_data = station_data.replace(-999, np.nan)
station_data = station_data.set_index("MESS_DATUM_BEGINN") # it is a row with time dates

station_data_anual = pd.DataFrame()
station_data_anual["Y_TT"] = station_data["MO_TT"].resample("A").mean()
station_data_anual["Y_RR"] = station_data["MO_RR"].resample("A").sum()

问题是,它忽略了nans。这意味着,例如,station_data_anual["Y_RR"]值太低。对于我只有nans作为每月值的年份,它返回0。
注:有一些问题与我的问题相似,但对我没有帮助。巨蟒
一些澄清:
输入数据:

station_data
Out[235]: 
                   STATIONS_ID MESS_DATUM_ENDE  QN_4  ...  MO_RR  MX_RS  eor
MESS_DATUM_BEGINN                                     ...                   
1981-01-01               403.0      1981-01-31  10.0  ...   51.5   10.0  eor
1981-02-01               403.0      1981-02-28  10.0  ...   23.8    5.4  eor
1981-03-01               403.0      1981-03-31  10.0  ...  116.5   28.0  eor
1981-04-01               403.0      1981-04-30  10.0  ...   24.1    9.5  eor
1981-05-01               403.0      1981-05-31  10.0  ...   29.4    8.4  eor
                       ...             ...   ...  ...    ...    ...  ...
2010-08-01               403.0      2010-08-31  10.0  ...    NaN   29.1  eor
2010-09-01               403.0      2010-09-30  10.0  ...    NaN   29.8  eor
2010-10-01               403.0      2010-10-31  10.0  ...    NaN    5.5  eor
2010-11-01               403.0      2010-11-30  10.0  ...    NaN   17.5  eor
2010-12-01               403.0      2010-12-31  10.0  ...    NaN    8.2  eor

[360 rows x 16 columns]

仔细看看:

station_data["MO_RR"][276:288]
Out[242]: 
MESS_DATUM_BEGINN
2004-01-01    66.3
2004-02-01     NaN
2004-03-01     NaN
2004-04-01     NaN
2004-05-01     NaN
2004-06-01     NaN
2004-07-01     NaN
2004-08-01     NaN
2004-09-01     NaN
2004-10-01     NaN
2004-11-01     NaN
2004-12-01     NaN
Name: MO_RR, dtype: float64

输出数据:

station_data_anual
Out[238]: 
                        Y_TT   Y_RR
MESS_DATUM_BEGINN                  
...
2003-12-31          9.866667  430.5
2004-12-31          9.620833   66.3
2005-12-31          9.665833    0.0
2006-12-31         10.158333    0.0
2007-12-31         10.555000    0.0
2008-12-31         10.361667    0.0
2009-12-31          9.587500    0.0
2010-12-31          8.207500    0.0

我的结果应该是这样的

Y_TT       Y_TX      Y_TN   Y_RR
MESS_DATUM_BEGINN                                       
... 
                        Y_TT   Y_RR
MESS_DATUM_BEGINN                  
...
2003-12-31          9.866667  430.5
2004-12-31          9.620833    nan    # getting nan instead of 66.3 is especially important
2005-12-31          9.665833    nan
2006-12-31         10.158333    nan
2007-12-31         10.555000    nan
2008-12-31         10.361667    nan
2009-12-31          9.587500    nan
2010-12-31          8.207500    nan
13z8s7eq

13z8s7eq1#

我从来没有使用过采样,可能有更好的解决方案,可以简单地忽略基于“条件”的“组”。但一个非常简单的解决方案可能是在resample之后使用自定义均值函数。

def very_mean(array_like):
    if any(pd.isnull(array_like)):
        return np.nan
    else:
        return array_like.mean()

station_data_anual["Y_TT"] = station_data["MO_TT"].resample("A").apply(very_mean)
j5fpnvbx

j5fpnvbx2#

你能先用删除nan值吗?

station_data_anual = pd.DataFrame()
station_data_anual["Y_TT"] = station_data["MO_TT"].dropna().resample("A").mean()
station_data_anual["Y_RR"] = station_data["MO_RR"].dropna().resample("A").sum()
1bqhqjot

1bqhqjot3#

考虑到以下实验,NaN值似乎未包括在平均值中:

df_ = pd.DataFrame(index=pd.date_range("2022","2023",periods=12))
df_['a'] = np.ones(12)
df_.iloc[1]['a'] = np.NaN
df_.resample("2M").mean()

所有平均2个月期间的平均值仍为1.0,作为上述mean()计算的输出中的平均值:

a
2022-01-31  1.0
2022-03-31  1.0
2022-05-31  1.0
2022-07-31  1.0
2022-09-30  1.0
2022-11-30  1.0
2023-01-31  1.0

相关问题