Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/pandas/core/groupby/base.py: 68%

22 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1""" 

2Provide basic components for groupby. These definitions 

3hold the allowlist of methods that are exposed on the 

4SeriesGroupBy and the DataFrameGroupBy objects. 

5""" 

6from __future__ import annotations 

7 

8import dataclasses 

9from typing import ( 

10 Hashable, 

11 Literal, 

12) 

13 

14 

15@dataclasses.dataclass(order=True, frozen=True) 

16class OutputKey: 

17 label: Hashable 

18 position: int 

19 

20 

21# special case to prevent duplicate plots when catching exceptions when 

22# forwarding methods from NDFrames 

23plotting_methods = frozenset(["plot", "hist"]) 

24 

25common_apply_allowlist = ( 

26 frozenset( 

27 [ 

28 "quantile", 

29 "fillna", 

30 "mad", 

31 "take", 

32 "idxmax", 

33 "idxmin", 

34 "tshift", 

35 "skew", 

36 "corr", 

37 "cov", 

38 "diff", 

39 ] 

40 ) 

41 | plotting_methods 

42) 

43 

44series_apply_allowlist: frozenset[str] = ( 

45 common_apply_allowlist 

46 | frozenset( 

47 {"nlargest", "nsmallest", "is_monotonic_increasing", "is_monotonic_decreasing"} 

48 ) 

49) | frozenset(["dtype", "unique"]) 

50 

51dataframe_apply_allowlist: frozenset[str] = common_apply_allowlist | frozenset( 

52 ["dtypes", "corrwith"] 

53) 

54 

55# cythonized transformations or canned "agg+broadcast", which do not 

56# require postprocessing of the result by transform. 

57cythonized_kernels = frozenset(["cumprod", "cumsum", "shift", "cummin", "cummax"]) 

58 

59# List of aggregation/reduction functions. 

60# These map each group to a single numeric value 

61reduction_kernels = frozenset( 

62 [ 

63 "all", 

64 "any", 

65 "corrwith", 

66 "count", 

67 "first", 

68 "idxmax", 

69 "idxmin", 

70 "last", 

71 "mad", 

72 "max", 

73 "mean", 

74 "median", 

75 "min", 

76 "nth", 

77 "nunique", 

78 "prod", 

79 # as long as `quantile`'s signature accepts only 

80 # a single quantile value, it's a reduction. 

81 # GH#27526 might change that. 

82 "quantile", 

83 "sem", 

84 "size", 

85 "skew", 

86 "std", 

87 "sum", 

88 "var", 

89 ] 

90) 

91 

92# List of transformation functions. 

93# a transformation is a function that, for each group, 

94# produces a result that has the same shape as the group. 

95 

96 

97# TODO(2.0) Remove after pad/backfill deprecation enforced 

98def maybe_normalize_deprecated_kernels(kernel) -> Literal["bfill", "ffill"]: 

99 if kernel == "backfill": 

100 kernel = "bfill" 

101 elif kernel == "pad": 

102 kernel = "ffill" 

103 return kernel 

104 

105 

106transformation_kernels = frozenset( 

107 [ 

108 "backfill", 

109 "bfill", 

110 "cumcount", 

111 "cummax", 

112 "cummin", 

113 "cumprod", 

114 "cumsum", 

115 "diff", 

116 "ffill", 

117 "fillna", 

118 "ngroup", 

119 "pad", 

120 "pct_change", 

121 "rank", 

122 "shift", 

123 "tshift", 

124 ] 

125) 

126 

127# these are all the public methods on Grouper which don't belong 

128# in either of the above lists 

129groupby_other_methods = frozenset( 

130 [ 

131 "agg", 

132 "aggregate", 

133 "apply", 

134 "boxplot", 

135 # corr and cov return ngroups*ncolumns rows, so they 

136 # are neither a transformation nor a reduction 

137 "corr", 

138 "cov", 

139 "describe", 

140 "dtypes", 

141 "expanding", 

142 "ewm", 

143 "filter", 

144 "get_group", 

145 "groups", 

146 "head", 

147 "hist", 

148 "indices", 

149 "ndim", 

150 "ngroups", 

151 "ohlc", 

152 "pipe", 

153 "plot", 

154 "resample", 

155 "rolling", 

156 "tail", 

157 "take", 

158 "transform", 

159 "sample", 

160 "value_counts", 

161 ] 

162) 

163# Valid values of `name` for `groupby.transform(name)` 

164# NOTE: do NOT edit this directly. New additions should be inserted 

165# into the appropriate list above. 

166transform_kernel_allowlist = reduction_kernels | transformation_kernels