def test_na_at_i_end(test_data): data = test_data(SHAPE) last_i = np.shape(data)[0] - 1 data = test_data(SHAPE, last_i, 3) actual = impy.locf(data, axis=1) data[last_i, 3] = data[last_i - 1, 3] assert np.array_equal(actual, data)
def pre_lof(data, year, year1): # 设定时间序列 # data['TIMESTAMP'] = pd.to_datetime(data['TIMESTAMP']) data = data.set_index('TIMESTAMP') if year.strip(): if year1.strip(): data = data[year:year1] else: data = data[year] # 补充了数据 ,这时补充的数据都是 0 df_period = data.resample('D').sum() # 替换空值为其他值 df_period_clone = df_period df_period_clone.replace(0, np.nan, inplace=True) # 平滑后的数据 isnullcon = df_period_clone.isnull().any() # print(isnullcon['FP_TOTALENG']) # 这里来个判断,如果数据里没有需要平滑的点,那么直接输出 if isnullcon['FP_TOTALENG']: df_after = impy.locf(df_period_clone, axis='FP_TOTALENG') for i in range(len(df_period_clone)): # print('-----平缓后数据-----') # print(df_after[0][i]) # print('-----平缓前数据-----') # print(df_period['FP_TOTALENG'][i]) df_period_clone['FP_TOTALENG'][i] = df_after[0][i] return df_period_clone
def test_na_at_i_end(self): """Check if at row[last_i],column[i]=row[last_i-1],column[i]""" last_i = np.shape(self.data_c)[0] - 1 self.data_c[last_i][3] = np.nan actual = impy.locf(self.data_c, axis=1) self.data_c[last_i][3] = self.data_c[last_i - 1][3] self.assertTrue(np.array_equal(actual, self.data_c))
def replace_data(df_period, field): pd.set_option('display.max_rows', None) # 替换空值为其他值 df_period_clone = df_period df_period_clone.replace(0, np.nan, inplace=True) # 平滑后的数据 isnullcon = df_period_clone.isnull().any() # print(isnullcon['FP_TOTALENG']) # 这里来个判断,如果数据里没有需要平滑的点,那么直接输出 df_peroid_v = df_period_clone[field] df_peroid_v.reset_index(drop=True, inplace=True) print(df_peroid_v.index) if isnullcon[field]: df_after = impy.locf(df_period_clone, axis=field) for i in range(len(df_period_clone)): print('-----平缓后数据-----') print(df_after[0][i]) print('-----平缓前数据-----') print(df_period[field][i]) df_period_clone[field][i] = df_after[0][i] # print(df_period_clone) return df_period_clone
def test_locf_(test_data): data = test_data(SHAPE) imputed = impy.locf(data) return_na_check(imputed)
def test_out_of_bounds(test_data): """Check out of bounds error, should throw BadInputError for any axis outside [0,1]""" data = test_data(SHAPE) with np.testing.assert_raises(error.BadInputError): impy.locf(data, axis=3)
def test_na_at_i(test_data): data = test_data(SHAPE, 3, 3) actual = impy.locf(data, axis=1) data[3, 3] = data[2, 3] assert np.array_equal(actual, data)
def test_na_at_i_start(test_data): data = test_data(SHAPE) actual = impy.locf(data, axis=1) data[0, 0] = data[1, 0] assert np.array_equal(actual, data)
def test_na_at_i(self): """Check if a null row[i],column[j]=row[i-1],column[j]""" self.data_c[3][3] = np.nan actual = impy.locf(self.data_c, axis=1) self.data_c[3][3] = self.data_c[2][3] self.assertTrue(np.array_equal(actual, self.data_c))
def test_na_at_i_start(self): """Check if null value at a row[0],column[0]=row[1],column[0]""" self.data_c[0][0] = np.nan actual = impy.locf(self.data_c, axis=1) self.data_c[0][0] = self.data_c[1][0] self.assertTrue(np.array_equal(actual, self.data_c))
def test_return_type(self): """ Check return type, should return an np.ndarray""" imputed = impy.locf(self.data_m) self.assertTrue(isinstance(imputed, np.ndarray))
def test_impute_missing_values(self): """ After imputation, no NaN's should exist""" imputed = impy.locf(self.data_m) self.assertFalse(np.isnan(imputed).any())