Skip to content

Instantly share code, notes, and snippets.

@swo
Created December 6, 2024 23:50
Show Gist options
  • Save swo/ce4bba030004e995c215a246ce92a2a8 to your computer and use it in GitHub Desktop.
Save swo/ce4bba030004e995c215a246ce92a2a8 to your computer and use it in GitHub Desktop.
polars diff with leading zero
def diff0(x: pl.Expr, first_value=0.0) -> pl.Expr:
"""
Get the differenced vector, putting a certain value (not null)
in the first place
"""
i = pl.arange(0, x.len())
return pl.when(i == 0).then(first_value).otherwise(x.diff())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment