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): respect Object dtype designation #14072

Merged

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jan 29, 2024

Closes #14051.

While I wouldn't encourage storing dataclasses inside frames as objects, we also shouldn't override the caller's explicitly-indicated intent to do so (eg: they specified the column/series as Object dtype).

Fixed the dataclass/namedtuple introspection occurring before the dtype was checked to see if it was Object.

Example

from dataclasses import dataclass
import polars as pl

@dataclass
class Item:
    name: str = ''
    price: int = 0

data = [
    Item(name='xxx', price=10),
    Item(name='yyy', price=30),
    Item(name='zzz', price=50),
]
s = pl.Series("items", data, pl.Object)

# shape: (3,)
# Series: 'items' [o][object]
# [
# 	Item(name='xxx', price=10)
# 	Item(name='yyy', price=30)
# 	Item(name='zzz', price=50)
# ]

(But don't actually do this if you can possibly avoid it 😉)

@github-actions github-actions bot added fix Bug fix python Related to Python Polars labels Jan 29, 2024
Copy link
Member

@stinodego stinodego left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks! Good to merge if CI is green.

@alexander-beedie alexander-beedie merged commit 5da14a0 into pola-rs:main Jan 29, 2024
16 checks passed
@alexander-beedie alexander-beedie deleted the respect-object-dtype branch January 29, 2024 15:50
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.

How to avoid the automatic dataclass data automatically converts to struct?
2 participants