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

docs(python): Add examples for multiple Series functions #16172

Merged
merged 4 commits into from
May 12, 2024
Merged
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
153 changes: 151 additions & 2 deletions py-polars/polars/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3315,6 +3315,28 @@ def limit(self, n: int = 10) -> Series:
See Also
--------
head

Examples
--------
>>> s = pl.Series("a", [1, 2, 3, 4, 5])
>>> s.limit(3)
shape: (3,)
Series: 'a' [i64]
[
1
2
3
]

Pass a negative value to get all rows `except` the last `abs(n)`.

>>> s.limit(-3)
shape: (2,)
Series: 'a' [i64]
[
1
2
]
"""
return self.head(n)

Expand Down Expand Up @@ -4065,6 +4087,28 @@ def explode(self) -> Series:
--------
Series.list.explode : Explode a list column.
Series.str.explode : Explode a string column.

Examples
--------
>>> s = pl.Series("a", [[1, 2, 3], [4, 5, 6]])
>>> s
shape: (2,)
Series: 'a' [list[i64]]
[
[1, 2, 3]
[4, 5, 6]
]
>>> s.explode()
shape: (6,)
Series: 'a' [i64]
[
1
2
3
4
5
6
]
"""

def equals(
Expand Down Expand Up @@ -4213,6 +4257,29 @@ def rechunk(self, *, in_place: bool = False) -> Self:
----------
in_place
In place or not.

Examples
--------
>>> s1 = pl.Series("a", [1, 2, 3])
>>> s1.n_chunks()
1
>>> s2 = pl.Series("a", [4, 5, 6])
>>> s = pl.concat([s1, s2], rechunk=False)
>>> s.n_chunks()
2
>>> s.rechunk(in_place=True)
shape: (6,)
Series: 'a' [i64]
[
1
2
3
4
5
6
]
>>> s.n_chunks()
1
"""
opt_s = self._s.rechunk(in_place)
return self if in_place else self._from_pyseries(opt_s)
Expand Down Expand Up @@ -6279,6 +6346,26 @@ def reinterpret(self, *, signed: bool = True) -> Series:
----------
signed
If True, reinterpret as `pl.Int64`. Otherwise, reinterpret as `pl.UInt64`.

Examples
--------
>>> s = pl.Series("a", [-(2**60), -2, 3])
>>> s
shape: (3,)
Series: 'a' [i64]
[
-1152921504606846976
-2
3
]
>>> s.reinterpret(signed=False)
shape: (3,)
Series: 'a' [u64]
[
17293822569102704640
18446744073709551614
3
]
"""

def interpolate(self, method: InterpolationMethod = "linear") -> Series:
Expand Down Expand Up @@ -7247,7 +7334,21 @@ def set_sorted(self, *, descending: bool = False) -> Self:
return self._from_pyseries(self._s.set_sorted_flag(descending))

def new_from_index(self, index: int, length: int) -> Self:
"""Create a new Series filled with values from the given index."""
"""
Create a new Series filled with values from the given index.

Examples
--------
>>> s = pl.Series("a", [1, 2, 3, 4, 5])
>>> s.new_from_index(1, 3)
shape: (3,)
Series: 'a' [i64]
[
2
2
2
]
"""
return self._from_pyseries(self._s.new_from_index(index, length))

def shrink_dtype(self) -> Series:
Expand All @@ -7256,10 +7357,58 @@ def shrink_dtype(self) -> Series:

Shrink to the dtype needed to fit the extrema of this [`Series`].
This can be used to reduce memory pressure.

Examples
--------
>>> s = pl.Series("a", [1, 2, 3, 4, 5, 6])
>>> s
shape: (6,)
Series: 'a' [i64]
[
1
2
3
4
5
6
]
>>> s.shrink_dtype()
shape: (6,)
Series: 'a' [i8]
[
1
2
3
4
5
6
]
"""

def get_chunks(self) -> list[Series]:
"""Get the chunks of this Series as a list of Series."""
"""
Get the chunks of this Series as a list of Series.

Examples
--------
>>> s1 = pl.Series("a", [1, 2, 3])
>>> s2 = pl.Series("a", [4, 5, 6])
>>> s = pl.concat([s1, s2], rechunk=False)
>>> s.get_chunks()
[shape: (3,)
Series: 'a' [i64]
[
1
2
3
], shape: (3,)
Series: 'a' [i64]
[
4
5
6
]]
"""
return self._s.get_chunks()

def implode(self) -> Self:
Expand Down