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?
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()
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?
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()