简体   繁体   中英

Pandas rolling_max with variable window size specified in a df column

I'd like to calculate a rolling_max of a pandas column, where the window size varies and is a difference between current row index and a row where a certain condition was met.

So, as an example, I have:

df = pd.DataFrame({'a': [0,1,0,0,0,1,0,0,0,0,1,0],
                   'b': [5,4,3,6,1,2,3,4,2,1,7,8]})

I want a rolling_max of df.b since df.a == 1 the previous time. Ie I want to get this:

     a   b   rm
 0   0   5   NaN  <- no previous a==1
 1   1   4   4    <- a==1
 2   0   3   4
 3   0   6   6
 4   0   1   6
 5   1   2   2    <- a==1
 6   0   3   3
 7   0   4   4
 8   0   2   4
 9   0   1   4
10   1   7   7    <- a==1
11   0   8   8

My df has an integer index without gaps, so I tried to do this:

df['last_a'] = np.where(df.a == 1, df.index, np.nan)
df['last_a'].fillna(method='ffill', inplace=True)
df['rm'] = pd.rolling_max(df['b'], window = df.index - df['last_a'] + 1)

but I'm getting a TypeError: an integer is required.

This is a part of a long script operating on quite a big data frame, so I need the fastest solution possible. I have successfully tried to do this with a loop instead of rolling_max, but it's very slow. Could you please help?

Just for reference. The ugly and long loop that I have now, and which, regardless its ugliness, seems to be quite fast on my data frame (50,000 x 25 for a test), is as follows:

df['rm2'] = df.b
df['rm1'] = np.where( (df['a'] == 1) | (df['rm2'].diff() > 0), df['rm2'], np.nan)
df['rm1'].fillna(method = 'ffill', inplace = True)
df['Dif'] = (df['rm1'] - df['rm2']).abs()
while df['Dif'].sum() != 0:
    df['rm2'] = df['rm1']
    df['rm1'] = np.where( (df['a'] == 1) | (df['rm2'].diff() > 0), df['rm2'], np.nan) 
    df['rm1'].fillna(method = 'ffill', inplace = True)
    df['Dif'] = (df['rm1'] - df['rm2']).abs()

I would create an index and groupby this index to use cummax :

import numpy as np

df['index'] = df['a'].cumsum()
df['rm']    = df.groupby('index')['b'].cummax()

df.loc[df['index']==0, 'rm'] = np.nan

In [104]: df
Out[104]:
    a  b  index  rm
0   0  5      0 NaN
1   1  4      1   4
2   0  3      1   4
3   0  6      1   6
4   0  1      1   6
5   1  2      2   2
6   0  3      2   3
7   0  4      2   4
8   0  2      2   4
9   0  1      2   4
10  1  7      3   7
11  0  8      3   8

Indeed, anytime you require restructuring data that involves relationships between columns and tables, consider an SQL solution using a Relational Database Management System (RDMS). And do so especially if your data derives from a database. Leave Pandas for data analysis. Of course, if you are storing large data not in a database, then that's whole another issue!

Python comes equipped with a built-in library for SQLite , the popular free, open-source file-level database. Additionally, Python libraries for MySQL, SQL Server, PostgreSQL, Oracle, and other RDMSs are available for install. You can integrate each connection seamlessly into pandas . Below are three equivalent versions of queries to achieve your conditional group max. Each assumes you maintain an autonumber primary key index, ID , in your source table, named here as RollingMax .

import sqlite3 as lite
import pandas as pd

con = lite.connect('C:\\Path\\SQLite\\DB.db')

# SQL WITH DERIVED TABLES
sql = """SELECT a, b,
               (SELECT Max(dtbl2.B) 
               FROM 
                   (SELECT t1.ID, t1.a, t1.b,
                          (SELECT Count(*) FROM RollingMax t2 
                           WHERE t1.ID >= t2.ID AND t2.A > 0) As GrpA
                    FROM RollingMax t1) dtbl2
               WHERE dtbl1.ID >= dtbl2.ID 
               AND dtbl1.GrpA = dtbl2.GrpA) As rm

         FROM 
         (
              SELECT t1.ID, t1.a, t1.b,
                     (SELECT Count(*) FROM RollingMax t2 
              WHERE t1.ID >= t2.ID AND t2.A > 0) As GrpA
              FROM RollingMax t1
         ) As dtbl1;"""

# SQL USING CTE WINDOW FUNCTION (AVAILABLE AS OF VERSION 3.8.3)
sql = """WITH grp (ID, a, b, GrpA)
         AS  (
              SELECT t1.ID, t1.a, t1.b,
                    (SELECT Count(*) FROM RollingMax t2 
                     WHERE t1.ID >= t2.ID AND t2.A > 0) As GrpA
              FROM RollingMax t1
             )
         SELECT a, b,
               (SELECT Max(dtbl2.B) 
                FROM grp AS dtbl2
                WHERE dtbl1.ID >= dtbl2.ID 
                AND dtbl1.GrpA = dtbl2.GrpA) As rm
         FROM grp AS dtbl1;"""

# SQL USING SAVED VIEW
'''To be saved inside database'''
saved_view = """SELECT t1.ID, t1.a, t1.b,
                  (SELECT Count(*) FROM RollingMax t2 
                   WHERE t1.ID >= t2.ID AND t2.A > 0) As GrpA
                FROM RollingMax t1;"""

sql = """SELECT a, b,
             (SELECT Max(dtbl2.B) 
              FROM saved_view AS dtbl2
              WHERE dtbl1.ID >= dtbl2.ID 
              AND dtbl1.GrpA = dtbl2.GrpA) As rm
         FROM saved_view As dtbl1;"""

df = pd.read_sql(sql, conn)

OUTPUT (only challenge here is the first grouping without preceding a==1)

a   b   rm
0   5   5
1   4   4
0   3   4
0   6   6
0   1   6
1   2   2
0   3   3
0   4   4
0   2   4
0   1   4
1   7   7
0   8   8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM