Computing Fraction of Long Trips with `delayed` Functions in Dask – Python 2026

Parallel Programming With Dask Mar 17, 2026

Computing Fraction of Long Trips with `delayed` Functions in Dask – Python 2026

Calculating fractions (e.g., "what percentage of trips were longer than 30 minutes?") on large datasets is a common analytical task. Using dask.delayed gives you full control over the computation graph, making it ideal for complex or custom fraction calculations that don’t fit neatly into standard Dask DataFrame methods.

TL;DR — Pattern Overview

  • Use @delayed to define custom computation steps
  • Build the full task graph first
  • Call .compute() only once at the end
  • This approach offers maximum flexibility and clear parallelism

1. Basic Version with `delayed`


from dask import delayed

@delayed
def count_long_trips(chunk):
    """Count trips longer than 30 minutes in a chunk."""
    return (chunk["trip_duration_minutes"] > 30).sum()

@delayed
def count_total_trips(chunk):
    """Count all trips in a chunk."""
    return len(chunk)

# Build computation graph
files = ["trips/part_*.parquet"]

total_long = 0
total_trips = 0

for f in files:
    chunk = delayed(pd.read_parquet)(f)
    total_long += count_long_trips(chunk)
    total_trips += count_total_trips(chunk)

# Final fraction
fraction_long = total_long / total_trips

print("Fraction of long trips:", fraction_long.compute())

2. Cleaner & More Scalable Version


from dask import delayed
import dask.dataframe as dd

@delayed
def compute_fraction(df):
    """Compute fraction of long trips in one DataFrame chunk."""
    long_trips = (df["trip_duration_minutes"] > 30).sum()
    total = len(df)
    return {"long": long_trips, "total": total}

# Use Dask DataFrame for better scalability
ddf = dd.read_parquet("trips/year=2025/*.parquet")

# Map custom delayed function over partitions
delayed_results = ddf.map_partitions(compute_fraction).to_delayed()

# Aggregate all partition results
total_long = delayed(sum)([r["long"] for r in delayed_results])
total_trips = delayed(sum)([r["total"] for r in delayed_results])

fraction = (total_long / total_trips).compute()

print(f"Fraction of long trips: {fraction:.4f} ({fraction*100:.2f}%)")

3. Best Practices in 2026

  • Use @delayed for custom logic that doesn’t fit Dask’s built-in methods
  • Keep delayed functions small and focused
  • Build the entire graph before calling .compute()
  • Use dd.from_delayed() or map_partitions() when working with tabular data
  • Visualize the task graph with .visualize() to verify parallelism
  • Prefer native Dask methods (e.g., boolean masking + .mean()) when possible — they are usually faster

Conclusion

Computing fractions with dask.delayed gives you fine-grained control over parallel execution. In 2026, this pattern is particularly useful when you need custom aggregation logic or when combining multiple delayed steps. For simple fraction calculations, native Dask DataFrame methods are often faster, but delayed remains the go-to tool for maximum flexibility.

Next steps:

Share:
Last updated: March 2026
Built-in Functions in Python 2026 str() in Python 2026: String Conversion + Modern Formatting & Best Practices memoryview with JAX in Python 2026: Zero-Copy NumPy → JAX Array Interop + Efficient ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + GPU Pinning & ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + ML Examples memoryview with NumPy & PyTorch in Python 2026: Zero-Copy Views, Efficient Slicing & ML Interop Examples memoryview with NumPy in Python 2026: Zero-Copy Views, Efficient Slicing & Real ML Examples memoryview() in Python 2026: Zero-Copy Magic for Large Binary Data + Real Examples import() in Python 2026: How It Works, Security Risks & Modern Alternatives (importlib) zip() in Python 2026: How to Use It, strict=True Behavior & Real-World Examples vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices super() in Python 2026: Method Resolution & Modern Inheritance Patterns sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices