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

fix(python): don't lose track of ones and zeros dtype, improve use with Array, raise error if dtype invalid #13326

Merged
merged 4 commits into from Dec 31, 2023

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Dec 30, 2023

Various minor fixes/improvements to the ones and zeros convenience functions.
(Follow up from #13121 (comment)).

  • Raise a suitable error on invalid/unsuitable "dtype" value instead of returning a series of null values.
  • Ensure proper "dtype" pass-through from repeatparse_as_list_of_expressionslit.
  • Extended test coverage to include error conditions, expected defaults, additional dtypes.
  • Improve handling of Array dtype (get width and create 1/0 arrays of that length).

Examples

Improve handling of Array dtype:

pl.ones(n=2, dtype=pl.Array(pl.UInt8,width=3), eager=True)

Before

shape: (2,)
Series: 'ones' [null]
[
    null
    null
]

After

shape: (2,)
Series: 'ones' [array[u8, 3]]
[
   [1, 1, 1]
   [1, 1, 1]
]

Raise on unsuitable/invalid dtype:

pl.zeros(n=2, dtype=pl.Struct({"x": pl.Null, "y": pl.Duration}), eager=True) 

Before

shape: (2,)
Series: 'zeros' [struct[2]]
[
    {null,null}
    {null,null}
]

After

TypeError: invalid dtype for `zeros`; found Struct({'x': Null, 'y': Duration}))

@github-actions github-actions bot added fix Bug fix python Related to Python Polars labels Dec 30, 2023
@alexander-beedie alexander-beedie changed the title fix(python): don't lose track of the explicit dtype in ones and zeros, and raise suitable error if dtype is invalid fix(python): don't lose track of ones and zeros dtype, raise suitable error if dtype is invalid Dec 30, 2023
…ros`, and raise suitable error if dtype is invalid
@alexander-beedie
Copy link
Collaborator Author

(Rebased to pick up #13327 fix for Array dtype).

@alexander-beedie alexander-beedie changed the title fix(python): don't lose track of ones and zeros dtype, raise suitable error if dtype is invalid fix(python): don't lose track of ones and zeros dtype, improve use with Array dtype, raise error if dtype invalid Dec 30, 2023
@alexander-beedie alexander-beedie changed the title fix(python): don't lose track of ones and zeros dtype, improve use with Array dtype, raise error if dtype invalid fix(python): don't lose track of ones and zeros dtype, improve use with Array, raise error if dtype invalid Dec 31, 2023
@ritchie46 ritchie46 merged commit 51837cc into pola-rs:main Dec 31, 2023
23 checks passed
@alexander-beedie alexander-beedie deleted the improve-ones-zeros branch December 31, 2023 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants