user189035
user189035

Reputation: 5789

.apply() a bivariate numpy() lambda function to an .expanding() window of a pandas DataFrame

Consider this simple code (numpy.sum in loc_fun is a stand in for a more complicated bivariate function using numpy):

import pandas
import numpy


def loc_fun(A, B):
    return numpy.sum(A[:-1] > B[-1])


df = pandas.DataFrame(numpy.random.normal(0, 1, [100000, 2]), columns=['size_A', 'size_B']).cumsum(axis=0)
df.expanding(2).apply(lambda x: loc_fun(x.size_A.values, x.size_B.values))

The last line in the code above results in an error I cannot make sense of. Basically, I would like to apply loc_fun to an expanding() window of the values in the columns.

Upvotes: 1

Views: 329

Answers (1)

CezarySzulc
CezarySzulc

Reputation: 2009

In lambda x is a numpy.ndarray so You can not refer to column 'A-values' or 'B_values'.

df.expanding(2).apply(lambda x: print(type(x)))

>><class 'numpy.ndarray'>

Upvotes: 1

Related Questions