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): object to_dict #6931

Merged
merged 2 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion py-polars/polars/internals/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
Int16,
Int32,
Int64,
Object,
PolarsDataType,
SchemaDefinition,
SchemaDict,
Expand Down Expand Up @@ -7098,10 +7099,11 @@ def iter_rows(
"""
# load into the local namespace for a modest performance boost in the hot loops
columns, get_row, dict_, zip_ = self.columns, self.row, dict, zip
has_object = Object in self.dtypes

# note: buffering rows results in a 2-4x speedup over individual calls
# to ".row(i)", so it should only be disabled in extremely specific cases.
if buffer_size:
if buffer_size and not has_object:
load_pyarrow_dicts = (
named
and _PYARROW_AVAILABLE
Expand Down
5 changes: 5 additions & 0 deletions py-polars/tests/unit/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ def test_empty_sort() -> None:
)
df_filtered = df.filter(pl.col("blob").apply(lambda blob: blob["name"] == "baz"))
df_filtered.sort(pl.col("blob").apply(lambda blob: blob["sort_key"]))


def test_object_to_dicts() -> None:
df = pl.DataFrame({"d": [{"a": 1, "b": 2, "c": 3}]}, schema={"d": pl.Object})
assert df.to_dicts() == [{"d": {"a": 1, "b": 2, "c": 3}}]