gitee.com/quant1x/num@v0.3.2/fillna.go (about) 1 package num 2 3 import ( 4 "slices" 5 ) 6 7 // Fill 填充 8 // 9 // Fill NA/NaN values using the specified method. 10 // Parameters 11 // ---------- 12 // value : scalar, dict, Series, or DataFrame 13 // Value to use to fill holes (e.g. 0), alternately a 14 // dict/Series/DataFrame of values specifying which value to use for 15 // each index (for a Series) or column (for a DataFrame). Values not 16 // in the dict/Series/DataFrame will not be filled. This value cannot 17 // be a list. 18 // method : {{'backfill', 'bfill', 'pad', 'ffill', None}}, default None 19 // Method to use for filling holes in reindexed Series 20 // pad / ffill: propagate last valid observation forward to next valid 21 // backfill / bfill: use next valid observation to fill gap. 22 // axis : {axes_single_arg} 23 // Axis along which to fill missing values. For `Series` 24 // this parameter is unused and defaults to 0. 25 // inplace : bool, default False [√] 26 // If True, fill in-place. Note: this will modify any 27 // other views on this object (e.g., a no-copy slice for a column in a 28 // DataFrame). 29 // limit : int, default None 30 // If method is specified, this is the maximum number of consecutive 31 // NaN values to forward/backward fill. In other words, if there is 32 // a gap with more than this number of consecutive NaNs, it will only 33 // be partially filled. If method is not specified, this is the 34 // maximum number of entries along the entire axis where NaNs will be 35 // filled. Must be greater than 0 if not None. 36 // downcast : dict, default is None 37 // A dict of item->dtype of what to downcast if possible, 38 // or the string 'infer' which will try to downcast to an appropriate 39 // equal type (e.g. float64 to int64 if possible). 40 // 41 // Returns 42 // ------- 43 // []T or None 44 func Fill[T BaseType](v []T, d T, args ...any) (rows []T) { 45 // 默认不替换 46 var __optInplace = false 47 if len(args) > 0 { 48 // 第一个参数为是否copy 49 if _cp, ok := args[0].(bool); ok { 50 __optInplace = _cp 51 } 52 } 53 var dest []T 54 if __optInplace { 55 dest = v 56 } else { 57 dest = slices.Clone(v) 58 } 59 var values any = dest 60 switch rows := values.(type) { 61 case []string: 62 ds := AnyToString(d) 63 for idx, iv := range rows { 64 if StringIsNaN(iv) { 65 rows[idx] = ds 66 } 67 } 68 case []float32: 69 df32 := AnyToFloat32(d) 70 for idx, iv := range rows { 71 if Float32IsNaN(iv) { 72 rows[idx] = df32 73 } 74 } 75 case []float64: 76 df64 := AnyToFloat64(d) 77 for idx, iv := range rows { 78 if Float64IsNaN(iv) { 79 rows[idx] = df64 80 } 81 } 82 default: 83 return dest 84 } 85 return dest 86 } 87 88 // FillNa NaN填充默认值 89 func FillNa[T BaseType](x []T, v any, args ...any) []T { 90 // 默认不copy 91 var __optInplace = false 92 if len(args) > 0 { 93 // 第一个参数为是否copy 94 if _cp, ok := args[0].(bool); ok { 95 __optInplace = _cp 96 } 97 } 98 var dest []T 99 if __optInplace { 100 dest = x 101 } else { 102 dest = slices.Clone(x) 103 } 104 var values any = dest 105 switch rows := values.(type) { 106 case []string: 107 for idx, iv := range rows { 108 if StringIsNaN(iv) { 109 rows[idx] = AnyToString(v) 110 } 111 } 112 case []int64: 113 for idx, iv := range rows { 114 if Float64IsNaN(float64(iv)) { 115 rows[idx] = AnyToInt64(v) 116 } 117 } 118 case []float32: 119 for idx, iv := range rows { 120 if Float32IsNaN(iv) { 121 rows[idx] = AnyToFloat32(v) 122 } 123 } 124 case []float64: 125 for idx, iv := range rows { 126 if Float64IsNaN(iv) { 127 rows[idx] = AnyToFloat64(v) 128 } 129 } 130 default: 131 return dest 132 } 133 return dest 134 }