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

.implode + .arr.head + .over raises PanicException #8967

Closed
2 tasks done
cmdlineluser opened this issue May 22, 2023 · 1 comment · Fixed by #9013
Closed
2 tasks done

.implode + .arr.head + .over raises PanicException #8967

cmdlineluser opened this issue May 22, 2023 · 1 comment · Fixed by #9013
Labels
bug Something isn't working python Related to Python Polars

Comments

@cmdlineluser
Copy link
Contributor

Polars version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of Polars.

Issue description

While testing out the flatten behaviour in #8965 - I noticed using .arr.head(1) raised an exception.

.arr.slice(0, 1) raises the same exception, but .arr.first() succeeds.

Oddly enough .arr.head(2) also succeeds but returns the same output as .arr.first() which doesn't seem right?

Reproducible example

import polars as pl

df = pl.DataFrame({
    'type': ['water','fire','water','earth']
})


df.select(
   pl.col('type').implode().arr.first().over('type')
)

# shape: (4, 1)
# ┌───────┐
# │ type  │
# │ ---   │
# │ str   │
# ╞═══════╡
# │ water │
# │ fire  │
# │ water │
# │ earth │
# └───────┘

df.select(
   pl.col('type').implode().arr.head(1).over('type')
)

# ComputeError: the length of the window expression did not match that of the group
# > group: ("water")
# > group length: 1
# > output: 'shape: (1,)
# Series: '' [str]
# [
# 	"water"
# ]'
# 
# Error originated in expression: 'col("type").list().slice([0, 1]).over([col("type")])'

Expected behavior

No exception.

Installed versions

--------Version info---------
Polars:      0.17.14
Index type:  UInt32
Python:      3.10.9 (main, Jan 14 2023, 16:56:26)

----Optional dependencies----
numpy:       1.23.5
pandas:      2.0.1
pyarrow:     9.0.0
connectorx:  <not installed>
deltalake:   <not installed>
fsspec:      2022.8.2
matplotlib:  3.5.1
xlsx2csv:    0.8
xlsxwriter:  3.0.8
@cmdlineluser cmdlineluser added bug Something isn't working python Related to Python Polars labels May 22, 2023
@ritchie46
Copy link
Member

This is correct. This will be more clear with the new mapping strategy argument released.

The default strategy of a window expression is "group_to_idx".

Maybe this makes it more clear:

df = pl.DataFrame({
    'type': ['water','fire','water','earth']
})

df.select(
    pl.all(),
   pl.col('type').over('type', mapping_strategy="join").alias("join"),
   pl.col('type').over('type', mapping_strategy="group_to_rows").alias("g2r"),
   pl.col('type').implode().arr.head(1).over('type', mapping_strategy="join").alias("this cannot be mapped to rows, as it doesn't have enough elements")
)
shape: (4, 4)
┌───────┬────────────────────┬───────┬───────────────────────────────────┐
│ type  ┆ join               ┆ g2r   ┆ this cannot be mapped to rows, a… │
│ ---   ┆ ---                ┆ ---   ┆ ---                               │
│ str   ┆ list[str]          ┆ str   ┆ list[str]                         │
╞═══════╪════════════════════╪═══════╪═══════════════════════════════════╡
│ water ┆ ["water", "water"] ┆ water ┆ ["water"]                         │
│ fire  ┆ ["fire"]           ┆ fire  ┆ ["fire"]                          │
│ water ┆ ["water", "water"] ┆ water ┆ ["water"]                         │
│ earth ┆ ["earth"]          ┆ earth ┆ ["earth"]                         │
└───────┴────────────────────┴───────┴───────────────────────────────────┘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working python Related to Python Polars
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants