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 Series.bin.ends_with, Series.bin.starts_with, Series.bin.decode, Series.bin.encode. #14478

Merged
merged 1 commit into from
Feb 14, 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
73 changes: 69 additions & 4 deletions py-polars/polars/series/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,51 @@ def contains(self, literal: IntoExpr) -> Series:
"""

def ends_with(self, suffix: IntoExpr) -> Series:
"""
r"""
Check if string values end with a binary substring.

Parameters
----------
suffix
Suffix substring.

Examples
--------
>>> s = pl.Series("colors", [b"\x00\x00\x00", b"\xff\xff\x00", b"\x00\x00\xff"])
>>> s.bin.ends_with(b"\x00")
shape: (3,)
Series: 'colors' [bool]
[
true
true
false
]
"""

def starts_with(self, prefix: IntoExpr) -> Series:
"""
r"""
Check if values start with a binary substring.

Parameters
----------
prefix
Prefix substring.

Examples
--------
>>> s = pl.Series("colors", [b"\x00\x00\x00", b"\xff\xff\x00", b"\x00\x00\xff"])
>>> s.bin.starts_with(b"\x00")
shape: (3,)
Series: 'colors' [bool]
[
true
false
true
]
"""

def decode(self, encoding: TransferEncoding, *, strict: bool = True) -> Series:
"""
r"""
Decode a value using the provided encoding.

Parameters
Expand All @@ -77,10 +101,31 @@ def decode(self, encoding: TransferEncoding, *, strict: bool = True) -> Series:
strict
Raise an error if the underlying value cannot be decoded,
otherwise mask out with a null value.

Examples
--------
>>> s = pl.Series("colors", [b"000000", b"ffff00", b"0000ff"])
>>> s.bin.decode("hex")
shape: (3,)
Series: 'colors' [binary]
[
b"\x00\x00\x00"
b"\xff\xff\x00"
b"\x00\x00\xff"
]
>>> s = pl.Series("colors", [b"AAAA", b"//8A", b"AAD/"])
>>> s.bin.decode("base64")
shape: (3,)
Series: 'colors' [binary]
[
b"\x00\x00\x00"
b"\xff\xff\x00"
b"\x00\x00\xff"
]
"""

def encode(self, encoding: TransferEncoding) -> Series:
"""
r"""
Encode a value using the provided encoding.

Parameters
Expand All @@ -92,4 +137,24 @@ def encode(self, encoding: TransferEncoding) -> Series:
-------
Series
Series of data type :class:`Boolean`.

Examples
--------
>>> s = pl.Series("colors", [b"\x00\x00\x00", b"\xff\xff\x00", b"\x00\x00\xff"])
>>> s.bin.encode("hex")
shape: (3,)
Series: 'colors' [str]
[
"000000"
"ffff00"
"0000ff"
]
>>> s.bin.encode("base64")
shape: (3,)
Series: 'colors' [str]
[
"AAAA"
"//8A"
"AAD/"
]
"""