You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Dask的GroupBy添加自定义聚合函数?求chunk等参数解析

Hey there! Let's break down your Dask questions step by step—this stuff is super useful once you get the hang of how Dask handles distributed computations.

1. Adding Custom Aggregations to Dask GroupBy

Dask's distributed nature means custom aggregations need to work across partitions, which is where dd.Aggregation comes in. It lets you define a multi-stage computation: process each chunk, aggregate chunk results, and finalize the output. Below are examples for the aggregations you mentioned:

Mean (Custom Implementation)

While Dask has a built-in mean(), building it from scratch shows the core pattern:

# Step 1: Chunk function – calculate sum and count per partition
def chunk_mean(s):
    return pd.Series({'sum': s.sum(), 'count': s.count()})

# Step 2: Agg function – combine sum and count across all partitions
def agg_mean(results):
    combined = pd.DataFrame(results).sum()
    return pd.Series({'sum': combined['sum'], 'count': combined['count']})

# Step 3: Finalize function – compute mean from total sum and count
def finalize_mean(result):
    return result['sum'] / result['count']

# Create the aggregation object
custom_mean = dd.Aggregation('custom_mean', chunk_mean, agg_mean, finalize_mean)

# Use it in a GroupBy operation
df.groupby('category')['value'].agg(custom_mean)

Mode

Mode isn't a built-in aggregation for Dask GroupBy, so we need to define it explicitly:

def chunk_mode(s):
    # Count occurrences of each value in the current partition
    return s.value_counts()

def agg_mode(counts):
    # Add up count series from all partitions to get total counts
    return sum(counts, start=pd.Series(dtype='int64'))

def finalize_mode(total_counts):
    # Pick the value with the highest total count (the mode)
    return total_counts.idxmax()

# Build the mode aggregation
custom_mode = dd.Aggregation('custom_mode', chunk_mode, agg_mode, finalize_mode)

# Usage example
df.groupby('category')['value'].agg(custom_mode)

Median & 95th Percentile

For percentiles (including median, the 50th percentile), note that exact calculations require collecting sorted data across partitions (which can be expensive for large datasets). Here's an approximate implementation that balances performance and accuracy:

def chunk_percentile(s, q=50):
    # Return quantile values from the current partition
    return s.quantile([q/100])

def agg_percentile(quantiles):
    # Combine partition quantiles and compute the final percentile
    combined = pd.concat(quantiles)
    return combined.quantile(q/100)

# Create median (50th percentile) aggregation
custom_median = dd.Aggregation(
    'custom_median', 
    lambda s: chunk_percentile(s, 50), 
    agg_percentile
)

# Create 95th percentile aggregation
custom_95p = dd.Aggregation(
    'custom_95p', 
    lambda s: chunk_percentile(s, 95), 
    agg_percentile
)

# Use both in a GroupBy
df.groupby('category')['value'].agg([custom_median, custom_95p])
2. Understanding dd.Aggregation('mode', chunk, agg, finalize)

Let's break down each parameter using the mode example to make it concrete:

chunk Function

  • What it does: Processes a single partition (chunk) of your Dask DataFrame/Series. Its job is to generate a partial result that can be combined with results from other partitions.
  • Type: A callable function that takes a pandas Series/DataFrame (the partition's data) and returns any serializable object (like a count series, or a dict of sum/count values).
  • Mode example: chunk_mode counts how many times each value appears in the current partition, returning a pandas Series where the index is the unique values and the values are their counts.

agg Function

  • What it does: Takes the output from all chunk functions and merges them into a single intermediate result. Think of this as the "reduce" step in map-reduce.
  • Type: A callable function that takes an iterator of chunk results and returns a merged intermediate result.
  • Mode example: agg_mode adds up all the count series from each partition, giving us a total count for every value across the entire dataset.

finalize Function (Optional)

  • What it does: Takes the merged result from the agg step and converts it into the final output you want. This is optional if the agg result is already in the format you need.
  • Type: A callable function that takes the aggregated intermediate result and returns the final value/object.
  • Mode example: finalize_mode takes the total count series and returns the value with the highest count (the actual mode).

Quick Tip

If your aggregation doesn't need a finalization step (e.g., if you just want total counts per group), you can omit the finalize parameter entirely—Dask will use the agg result directly.

内容的提问来源于stack exchange,提问作者Aaditya Agrawal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:03:42