Skip to content

Commit

Permalink
docs(python): Add missing docstring examples in the Struct namespace (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
petrosbar authored Mar 15, 2024
1 parent bb1aa26 commit fec8a55
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions py-polars/polars/series/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ def _ipython_key_completions_(self) -> list[str]:

@property
def fields(self) -> list[str]:
"""Get the names of the fields."""
"""
Get the names of the fields.
Examples
--------
>>> s = pl.Series([{"a": 1, "b": 2}, {"a": 3, "b": 4}])
>>> s.struct.fields
['a', 'b']
"""
if getattr(self, "_s", None) is None:
return []
return self._s.struct_fields()
Expand All @@ -49,7 +57,18 @@ def field(self, name: str) -> Series:
Parameters
----------
name
Name of the field
Name of the field.
Examples
--------
>>> s = pl.Series([{"a": 1, "b": 2}, {"a": 3, "b": 4}])
>>> s.struct.field("a")
shape: (2,)
Series: 'a' [i64]
[
1
3
]
"""

def rename_fields(self, names: Sequence[str]) -> Series:
Expand All @@ -59,12 +78,29 @@ def rename_fields(self, names: Sequence[str]) -> Series:
Parameters
----------
names
New names in the order of the struct's fields
New names in the order of the struct's 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']
"""

@property
def schema(self) -> OrderedDict[str, DataType]:
"""Get the struct definition as a name/dtype schema dict."""
"""
Get the struct definition as a name/dtype schema dict.
Examples
--------
>>> s = pl.Series([{"a": 1, "b": 2}, {"a": 3, "b": 4}])
>>> s.struct.schema
OrderedDict({'a': Int64, 'b': Int64})
"""
if getattr(self, "_s", None) is None:
return OrderedDict()
return OrderedDict(self._s.dtype().to_schema())
Expand Down

0 comments on commit fec8a55

Please sign in to comment.