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

feat(python): Raise DuplicateError if given a pyarrow Table object with duplicate column names #20624

Merged
merged 2 commits into from
Jan 9, 2025

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jan 8, 2025

Closes #20616.

We already catch this case in read_database (see: #18548), but read_database_uri has exposed a more generic gremlin in frame construction from pyarrow.Table objects (eg: connectorx loads query results to pyarrow.Table, which gets passed to pl.from_arrow).

We weren't explicitly checking the incoming pyarrow.Table for duplicate columns, which leads to a somewhat non-obvious ShapeError; with this PR we now raise a more helpful DuplicateError that explicitly refers to the column name that appears more than once.

Example

import pyarrow as pa
import polars as pl

tbl = pa.Table.from_arrays(
    arrays=[
        pa.array([1, 2, 3], type=pa.int32()),
        pa.array([4, 5, 6], type=pa.int32()),
    ],
    schema=pa.schema([
        ("dupe_col", pa.int32()), 
        ("dupe_col", pa.int32()),
    ]),
)

🔴 Before

pl.from_arrow(tbl)
# ShapeError: 2 column names provided for a DataFrame of width 1

🟢 After

pl.from_arrow(tbl)
# DuplicateError: column 'dupe_col' appears 2 times; names must be unique

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars labels Jan 8, 2025
Copy link

codecov bot commented Jan 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.04%. Comparing base (92fd75d) to head (9d1cba4).
Report is 15 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #20624      +/-   ##
==========================================
+ Coverage   79.00%   79.04%   +0.04%     
==========================================
  Files        1566     1557       -9     
  Lines      221034   220543     -491     
  Branches     2510     2514       +4     
==========================================
- Hits       174618   174325     -293     
+ Misses      45842    45644     -198     
  Partials      574      574              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@alexander-beedie alexander-beedie removed the rust Related to Rust Polars label Jan 9, 2025
@alexander-beedie alexander-beedie changed the title feat: Raise DuplicateError if given a pyarrow Table object with duplicate column names feat(python): Raise DuplicateError if given a pyarrow Table object with duplicate column names Jan 9, 2025
@ritchie46 ritchie46 merged commit 53a493f into pola-rs:main Jan 9, 2025
15 checks passed
@alexander-beedie alexander-beedie deleted the duplicate-arrow-cols branch January 9, 2025 12:22
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 python Related to Python Polars
Projects
None yet
2 participants