question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

rolling_apply only applicable to numeric columns

See original GitHub issue

Hi everybody,

I discovered that the rolling_apply function is only applicable to numeric columns. I think this should be changed as this seems too limited to me. Let’s take the following example,

    import datetime as DT
    df = pd.DataFrame({
    'Buyer': 'Carl Mark Carl Joe Joe Carl'.split(),
    'Quantity': [1,3,5,8,9,3],
    'Date' : [
        DT.datetime(2013,9,1,13,0),
        DT.datetime(2013,9,1,13,5),
        DT.datetime(2013,10,1,20,0),
        DT.datetime(2013,10,3,10,0),
        DT.datetime(2013,12,2,12,0),                                      
        DT.datetime(2013,12,2,14,0),
        ]}).set_index('Date')

Now I want to count all new customers each 10 days.

buyers = []

def novices(x):
    new = [n for n in x if n not in buyers]

    if (len(new) > 0): buyers.extend(new) 

    return len(new)


pd.rolling_apply(df['Buyer'], 10, novices)

throws an exception “ValueError: could not convert string to float: Carl”

However, although meaningless a call with a numeric column such as:

pd.rolling_apply(df['Quantity'], 2, novices)

works.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:1
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
jrebackcommented, Mar 21, 2018

would always take a PR for this

0reactions
Darel13712commented, Mar 21, 2018

@jreback Hello is there any possibility to get rolling.apply for objects in 2018?) It still ignores objects as of 0.22.0.

idx = pd.date_range(pd.to_datetime('2017-01-01'), pd.to_datetime('2017-01-03'))
df = pd.DataFrame([[1, 's'],[2, 't'],[3, 'r']], index=idx)
0 1
2017-01-01 1 s
2017-01-02 2 t
2017-01-03 3 r

it’s ok to ignore objects with built-in functions

df.rolling(2, min_periods=1).sum()
0 1
2017-01-01 1 s
2017-01-02 3 t
2017-01-03 5 r

but why can’t i handle objects myself?

def const(x):
    return 5
df.rolling(2, min_periods=1).apply(const)
0 1
2017-01-01 5 s
2017-01-02 5 t
2017-01-03 5 r

This example above is not really useful but I want to be able to apply function to a column that contains objects.

The only solution I’ve found is to create your own DataFrame/Series type https://stackoverflow.com/questions/44479384/pandas-rolling-apply-doesnt-do-anything

But I really think this should be in pandas by default.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pandas rolling apply using multiple columns - Stack Overflow
I am trying to use a pandas.DataFrame.rolling.apply() rolling function on multiple columns. Python version is 3.7, pandas is 1.0.2.
Read more >
Pandas rolling().apply() Function - Linux Hint
We want this DataFrame to have four columns; hence, we named them “East”, “West”, “North”, and “South”. The data type for all the...
Read more >
pandas.DataFrame.rolling — pandas 1.5.2 documentation
Provide rolling window calculations. Parameters. windowint, offset, or BaseIndexer subclass. Size of the moving window. If an integer, the ...
Read more >
Python | Pandas dataframe.rolling() - GeeksforGeeks
Pandas dataframe.rolling() function provides the feature of rolling window calculations. The concept of rolling window calculation is most ...
Read more >
pandas rolling apply doesn't do anything-Pandas,Python
This creates a custom dataframe class that has a rolling_object method. It does not well match the pandas way in that it only...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found