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): support transparent DataFrame init from numpy structured/record arrays. #8620

Merged

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented May 1, 2023

Partially addresses #8564 (this PR adds init support; a follow-up PR will support export).
Also #3520 (comment).

HandlesDataFrame init from numpy structured/record arrays1, recognising column names and dtypes and destructuring appropriately; it's a clean addition to the existing (internal) numpy_to_pydf construction function, so no new user-facing parameters (it will "just work").

Example

import numpy as np
import polars as pl

structured_array = np.array(
    [
        ("Google Pixel 7", 521.90, True),
        ("Apple iPhone 14 Pro", 999.00, True),
        ("Samsung Galaxy S23 Ultra", 1199.99, False),
        ("OnePlus 11", 699.00, True),
    ],
    dtype = np.dtype(
        [
            ("product", "U32"),
            ("price_usd", "float64"),
            ("in_stock", "bool"),
        ]
    ),
)

Before: (results in a single anonymous "object" column)

pl.DataFrame( structured_array )

# shape: (4, 1)
# ┌───────────────────────────────────┐
# │ column_0                          │
# │ ---                               │
# │ object                            │
# ╞═══════════════════════════════════╡
# │ ('Google Pixel 7', 521.9, True)   │
# │ ('Apple iPhone 14 Pro', 999., Tr… │
# │ ('Samsung Galaxy S23 Ultra', 119… │
# │ ('OnePlus 11', 699., True)        │
# └───────────────────────────────────┘

After: (destructured to frame)

pl.DataFrame( structured_array )

# shape: (4, 3)
# ┌──────────────────────────┬───────────┬──────────┐
# │ product                  ┆ price_usd ┆ in_stock │
# │ ---                      ┆ ---       ┆ ---      │
# │ str                      ┆ f64       ┆ bool     │
# ╞══════════════════════════╪═══════════╪══════════╡
# │ Google Pixel 7           ┆ 521.9     ┆ true     │
# │ Apple iPhone 14 Pro      ┆ 999.0     ┆ true     │
# │ Samsung Galaxy S23 Ultra ┆ 1199.99   ┆ false    │
# │ OnePlus 11               ┆ 699.0     ┆ true     │
# └──────────────────────────┴───────────┴──────────┘

Footnotes

  1. Numpy structured arrays: https://numpy.org/doc/stable/user/basics.rec.html

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars labels May 1, 2023
@alexander-beedie alexander-beedie changed the title feat(python): support transparent DataFrame init from numpy structured arrays and record arrays. feat(python): support transparent DataFrame init from numpy structured/record arrays. May 1, 2023
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
Development

Successfully merging this pull request may close these issues.

1 participant