How to use with.columns in LazyGroupBy object in polars?

Question:

I am trying to calculate the difference of lag variable group by id variable. However,

when I tried to run the following code:

ad.v2.groupby('id').with_columns(
      diff = pl.col('Movement_Time_clear') - pl.col('Movement_Time_clear').diff()
)

A warning was popped:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'LazyGroupBy' object has no attribute 'with_columns'

What was the cause of the warning?

Asked By: doraemon

||

Answers:

Thanks for Wayoshi’ s reminder, I should use agg instead of with.columns

The updated code is as below:

ad.v2.groupby('id').agg(
      diff = pl.col('Movement_Time_clear') - 
             pl.col('Movement_Time_clear').shift()
                    ).collect()
Answered By: doraemon
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.