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 docstring examples for Exceptions #15803

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
56 changes: 46 additions & 10 deletions py-polars/polars/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class ColumnNotFoundError(PolarsError): # type: ignore[no-redef, misc]
"""
Exception raised when a specified column is not found.

Example
-------
Examples
--------
>>> df = pl.DataFrame({"a": [1, 2, 3]})
>>> df.select("b")
polars.exceptions.ColumnNotFoundError: b
Expand All @@ -41,8 +41,8 @@ class DuplicateError(PolarsError): # type: ignore[no-redef, misc]
"""
Exception raised when a column name is duplicated.

Example
-------
Examples
--------
>>> df = pl.DataFrame({"a": [1, 1, 1]})
>>> pl.concat([df, df], how="horizontal")
polars.exceptions.DuplicateError: unable to hstack, column with name "a" already exists
Expand All @@ -52,8 +52,8 @@ class InvalidOperationError(PolarsError): # type: ignore[no-redef, misc]
"""
Exception raised when an operation is not allowed (or possible) against a given object or data structure.

Example
-------
Examples
--------
>>> s = pl.Series("a", [1, 2, 3])
>>> s.is_in(["x", "y"])
polars.exceptions.InvalidOperationError: `is_in` cannot check for String values in Int64 data
Expand All @@ -72,16 +72,52 @@ class SchemaError(PolarsError): # type: ignore[no-redef, misc]
"""Exception raised when an unexpected schema mismatch causes an error."""

class SchemaFieldNotFoundError(PolarsError): # type: ignore[no-redef, misc]
"""Exception raised when a specified schema field is not found."""
"""
Exception raised when a specified schema field is not found.

Examples
--------
>>> df = pl.DataFrame({"a": [1, 2, 3]})
>>> df.rename({"b": "c"})
polars.exceptions.SchemaFieldNotFoundError: b
"""

class ShapeError(PolarsError): # type: ignore[no-redef, misc]
"""Exception raised when trying to perform operations on data structures with incompatible shapes.""" # noqa: W505
"""
Exception raised when trying to perform operations on data structures with incompatible shapes.

Examples
--------
>>> pl.DataFrame({"a": [1, 2, 3], "b": [4, 5]})
polars.exceptions.ShapeError: could not create a new DataFrame: series "a" has length 3 while series "b" has length 2
""" # noqa: W505

class StringCacheMismatchError(PolarsError): # type: ignore[no-redef, misc]
"""Exception raised when string caches come from different sources."""
"""
Exception raised when string caches come from different sources.

Examples
--------
>>> pl.DataFrame(
... [
... pl.Series(["a", "b", "c"], dtype=pl.Categorical),
... pl.Series(["b", "b", "c"], dtype=pl.Categorical),
... ]
... ).transpose()
polars.exceptions.StringCacheMismatchError: cannot compare categoricals coming from different sources, consider setting a global StringCache.
""" # noqa: W505

class StructFieldNotFoundError(PolarsError): # type: ignore[no-redef, misc]
"""Exception raised when a specified Struct field is not found."""
"""
Exception raised when a specified Struct field is not found.

Examples
--------
>>> pl.struct(
... [pl.Series("a", [1, 2]), pl.Series("b", ["a", "b"])], eager=True
... ).struct.field("z")
polars.exceptions.StructFieldNotFoundError: z
"""

class PolarsWarning(Exception): # type: ignore[no-redef]
"""Base class for all Polars warnings."""
Expand Down
Loading