Aggregating data every n elements

Open
#351 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
pandas, python

Research direction

Start with the reported stream.map(...).partition(100).to_batch().map_partitions(func) chain and inspect the map_partitions entry point, especially how its example is inferred. Determine whether chunked pandas value_counts aggregation is supported or requires a new API, then define a reproducible example and tests that establish the expected output for successive 100-element batches.

Written by the indexing model from the issue text.

Description

Hi,

I would like to calculate the .value_counts() from a pandas dataframe in chunks of n elements and output the aggregated result.
For example, I have 1000 elements. Calculate the value_counts() for the first 100 and output the result, aggregate the result to the next 100 elements and output it again.

I tried

def func(df):
    return df
stream.map(df["OFFENSE_CODE"]).partition(100).to_batch().map_partitions(func)

but I get

TypeError                                 Traceback (most recent call last)
<ipython-input-291-e68a39a9e8bb> in <module>
      1 def func(df):
      2     return df
----> 3 stream.map(df["OFFENSE_CODE"]).partition(100).to_batch().map_partitions(func)
      4 
      5 # a.value_counts()

~\Anaconda3\lib\site-packages\streamz\collection.py in map_partitions(func, *args, **kwargs)
     19     example = kwargs.pop('example', None)
     20     if example is None:
---> 21         example = func(*[getattr(arg, 'example', arg) for arg in args], **kwargs)
     22 
     23     streams = [arg for arg in args if isinstance(arg, Streaming)]

TypeError: func() missing 1 required positional argument: 'df'

BTW I am not sure this is the best approach to do this. Any hint?

Dominant language
Python
Stars
1.3k
Forks
149
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python-streamz/streamz

All issues in python-streamz/streamz

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.