rolling_apply only applicable to numeric columns
See original GitHub issueHi 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:
- Created 10 years ago
- Reactions:1
- Comments:12 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
would always take a PR for this
@jreback Hello is there any possibility to get rolling.apply for objects in 2018?) It still ignores objects as of 0.22.0.
it’s ok to ignore objects with built-in functions
but why can’t i handle objects myself?
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.