Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

value is a list (of dates in this case). Wanted to subtract another column (scalar date) from all the entries. #15706

Closed
zbenmo opened this issue Apr 16, 2024 · 3 comments
Labels
enhancement New feature or an improvement of an existing feature

Comments

@zbenmo
Copy link

zbenmo commented Apr 16, 2024

Description

PanicException: sub operation not supported for dtype list[date]

@zbenmo zbenmo added the enhancement New feature or an improvement of an existing feature label Apr 16, 2024
@alexander-beedie
Copy link
Collaborator

alexander-beedie commented Apr 16, 2024

You may find list eval suitable if you can get away with subtracting a constant?

from datetime import date
import polars as pl

d = date.today()

df = pl.DataFrame(
  data={"dts": [[d,d],[d,d]]},
).with_columns(
  sub = pl.col("dts").list.eval(pl.element() - date(2024,1,1))
)
# shape: (2, 2)
# ┌──────────────────────────┬────────────────────┐
# │ dts                      ┆ sub                │
# │ ---                      ┆ ---                │
# │ list[date]               ┆ list[duration[ms]] │
# ╞══════════════════════════╪════════════════════╡
# │ [2024-04-17, 2024-04-17] ┆ [107d, 107d]       │
# │ [2024-04-17, 2024-04-17] ┆ [107d, 107d]       │
# └──────────────────────────┴────────────────────┘

If not, the current solutions are all bit more exotic 😅
(See @zbenmo links above).

@zbenmo
Copy link
Author

zbenmo commented Apr 17, 2024

.list.eval is indeed promising, it works at least at the moment with a constant scalar yet not with a scalar from another column.

For my current use case, I'll solve it by having the list column first as long format (exploded), with the needed date duplicated for all rows in another column, and only then group_by and agg.

@zbenmo zbenmo closed this as completed Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants