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

Inconsistent behavior of n_nunique for series and dataframe #20341

Open
2 tasks done
vlulla opened this issue Dec 17, 2024 · 1 comment
Open
2 tasks done

Inconsistent behavior of n_nunique for series and dataframe #20341

vlulla opened this issue Dec 17, 2024 · 1 comment
Labels
accepted Ready for implementation bug Something isn't working P-medium Priority: medium python Related to Python Polars

Comments

@vlulla
Copy link

vlulla commented Dec 17, 2024

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

df = pl.DataFrame({"list-of-structs":[[{"a":1,"b":2},{"a":10,"b":20}],
                                                               [{"a":1,"b":2},{"a":10,"b":20}],
                                                               [{"a":3,"b":4}]]
                                })
print(df.select('list-of-structs').n_unique()," <== works") # works
print(type(df.select('list-of-structs'))) # polars.dataframe.frame.DataFrame

print(df['list-of-structs'].n_unique()) # InvalidOperationError: `n_unique` operation not supported for dtype `list[struct[2]]`
print(type(df['list-of-structs'])) # polars.series.series.Series

I am unsure why the series version of n_unique will not work whereas the dataframe version will work. I tried looking up this information in the documentation but was unable to find anything substantive.

Log output

No response

Issue description

Inconsistent behavior of n_unique for a series of list-of-structs.

Expected behavior

I believe that df.select('list-of-structs').n_unique() and df['list-of-structs'].n_unique() both ought to work and give the same answer. Currently the series version raises an InvalidOperationError.

Installed versions

--------Version info---------
Polars:              1.17.1
Index type:          UInt32
Platform:            macOS-14.4.1-arm64-arm-64bit-Mach-O
Python:              3.13.1 | packaged by conda-forge | (main, Dec  5 2024, 21:09:18) [Clang 18.1.8 ]
LTS CPU:             False

----Optional dependencies----
adbc_driver_manager  <not installed>
altair               <not installed>
boto3                <not installed>
cloudpickle          <not installed>
connectorx           <not installed>
deltalake            <not installed>
fastexcel            <not installed>
fsspec               <not installed>
gevent               <not installed>
google.auth          2.37.0
great_tables         <not installed>
matplotlib           3.10.0
nest_asyncio         1.6.0
numpy                2.2.0
openpyxl             3.1.5
pandas               2.2.3
pyarrow              18.1.0
pydantic             <not installed>
pyiceberg            <not installed>
sqlalchemy           2.0.36
torch                <not installed>
xlsx2csv             <not installed>
xlsxwriter           <not installed>
@vlulla vlulla added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Dec 17, 2024
@cmdlineluser
Copy link
Contributor

It seems even with expressions, it errors.

df.select(pl.col("list-of-structs").n_unique())
# InvalidOperationError: `n_unique` operation not supported for dtype `list[struct[2]]`

Not sure if it is due to this check:

if !self.inner_dtype().is_numeric() {

It seems the DataFrame method wraps it in a pl.struct call.

expr = F.struct(struct_fields)

df.select(pl.struct("list-of-structs").n_unique())
# shape: (1, 1)
# ┌─────────────────┐
# │ list-of-structs │
# │ ---             │
# │ u32             │
# ╞═════════════════╡
# │ 2               │
# └─────────────────┘

@orlp orlp added accepted Ready for implementation P-medium Priority: medium and removed needs triage Awaiting prioritization by a maintainer labels Dec 24, 2024
@github-project-automation github-project-automation bot moved this to Ready in Backlog Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation bug Something isn't working P-medium Priority: medium python Related to Python Polars
Projects
Status: Ready
Development

No branches or pull requests

3 participants