base.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. """
  2. Provide basic components for groupby.
  3. """
  4. from __future__ import annotations
  5. import dataclasses
  6. from typing import Hashable
  7. @dataclasses.dataclass(order=True, frozen=True)
  8. class OutputKey:
  9. label: Hashable
  10. position: int
  11. # special case to prevent duplicate plots when catching exceptions when
  12. # forwarding methods from NDFrames
  13. plotting_methods = frozenset(["plot", "hist"])
  14. # cythonized transformations or canned "agg+broadcast", which do not
  15. # require postprocessing of the result by transform.
  16. cythonized_kernels = frozenset(["cumprod", "cumsum", "shift", "cummin", "cummax"])
  17. # List of aggregation/reduction functions.
  18. # These map each group to a single numeric value
  19. reduction_kernels = frozenset(
  20. [
  21. "all",
  22. "any",
  23. "corrwith",
  24. "count",
  25. "first",
  26. "idxmax",
  27. "idxmin",
  28. "last",
  29. "max",
  30. "mean",
  31. "median",
  32. "min",
  33. "nunique",
  34. "prod",
  35. # as long as `quantile`'s signature accepts only
  36. # a single quantile value, it's a reduction.
  37. # GH#27526 might change that.
  38. "quantile",
  39. "sem",
  40. "size",
  41. "skew",
  42. "std",
  43. "sum",
  44. "var",
  45. ]
  46. )
  47. # List of transformation functions.
  48. # a transformation is a function that, for each group,
  49. # produces a result that has the same shape as the group.
  50. transformation_kernels = frozenset(
  51. [
  52. "bfill",
  53. "cumcount",
  54. "cummax",
  55. "cummin",
  56. "cumprod",
  57. "cumsum",
  58. "diff",
  59. "ffill",
  60. "fillna",
  61. "ngroup",
  62. "pct_change",
  63. "rank",
  64. "shift",
  65. ]
  66. )
  67. # these are all the public methods on Grouper which don't belong
  68. # in either of the above lists
  69. groupby_other_methods = frozenset(
  70. [
  71. "agg",
  72. "aggregate",
  73. "apply",
  74. "boxplot",
  75. # corr and cov return ngroups*ncolumns rows, so they
  76. # are neither a transformation nor a reduction
  77. "corr",
  78. "cov",
  79. "describe",
  80. "dtypes",
  81. "expanding",
  82. "ewm",
  83. "filter",
  84. "get_group",
  85. "groups",
  86. "head",
  87. "hist",
  88. "indices",
  89. "ndim",
  90. "ngroups",
  91. "nth",
  92. "ohlc",
  93. "pipe",
  94. "plot",
  95. "resample",
  96. "rolling",
  97. "tail",
  98. "take",
  99. "transform",
  100. "sample",
  101. "value_counts",
  102. ]
  103. )
  104. # Valid values of `name` for `groupby.transform(name)`
  105. # NOTE: do NOT edit this directly. New additions should be inserted
  106. # into the appropriate list above.
  107. transform_kernel_allowlist = reduction_kernels | transformation_kernels