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: Add strict parameter to rename_fields #19646

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions py-polars/polars/expr/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,16 @@ def unnest(self) -> Expr:
"""
return self.field("*")

def rename_fields(self, names: Sequence[str]) -> Expr:
def rename_fields(self, names: Sequence[str], strict: bool = True) -> Expr:
"""
Rename the fields of the struct.

Parameters
----------
names
New names, given in the same order as the struct's fields.
strict
If True, raises an error if the length of names does not match the number of fields.

Examples
--------
Expand All @@ -214,7 +216,7 @@ def rename_fields(self, names: Sequence[str]) -> Expr:
│ struct_col │
│ --- │
│ struct[4] │
╞═════════════════════╡
╞══════��═══════════════╡
Copy link
Collaborator

Choose a reason for hiding this comment

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

Funky formatting happening here 😬

│ {1,"ab",true,[1, 2]} │
│ {2,"cd",null,[3]} │
└──────────────────────┘
Expand Down Expand Up @@ -251,6 +253,8 @@ def rename_fields(self, names: Sequence[str]) -> Expr:
>>> df.select(pl.col("struct_col").struct.field("aaa")) # doctest: +SKIP
StructFieldNotFoundError: aaa
"""
if strict and len(names) != len(self.fields):
raise ValueError("The length of names must match the number of fields in the struct.")
return wrap_expr(self._pyexpr.struct_rename_fields(names))

def json_encode(self) -> Expr:
Expand Down Expand Up @@ -332,7 +336,7 @@ def with_fields(
│ 1.0 ┆ 4 ┆ 40 ┆ 10 │
│ 2.0 ┆ 9 ┆ 18 ┆ 2 │
│ 3.0 ┆ 16 ┆ 48 ┆ 3 │
└─────┴─────┴──────┴─────────┘
└─────┴─────┴──���────┴───���──────┘
Copy link
Collaborator

Choose a reason for hiding this comment

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

Funky formatting happening here 😬


Parameters
----------
Expand Down
13 changes: 7 additions & 6 deletions py-polars/polars/series/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,25 @@ def field(self, name: str) -> Series:
]
"""

def rename_fields(self, names: Sequence[str]) -> Series:
def rename_fields(self, names: Sequence[str], strict: bool = True) -> Series:
"""
Rename the fields of the struct.

Parameters
----------
names
New names in the order of the struct's fields.
strict
If True, raises an error if the length of names does not match the number of fields.

Examples
--------
>>> s = pl.Series([{"a": 1, "b": 2}, {"a": 3, "b": 4}])
>>> s.struct.fields
['a', 'b']
>>> s = s.struct.rename_fields(["c", "d"])
>>> s.struct.fields
['c', 'd']
>>> s.struct.rename_fields(["c", "d"], strict=True)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I reckon the example would be more informative if you:

  • show the error you get if you try to rename but provide the wrong number of names with strict=True;
  • show what happens if you try to rename but provide the wrong number of names with strict=False;
  • both of the above

"""
if strict and len(names) != len(self.fields):
raise ValueError("The length of names must match the number of fields in the struct.")


@property
def schema(self) -> Schema:
Expand Down
Loading