From a51f32b3194f67b96d1d261d5d28fa58b0561c2b Mon Sep 17 00:00:00 2001 From: NedJWestern Date: Mon, 15 Jan 2024 08:33:58 +1100 Subject: [PATCH] docs(python): Examples for errors --- py-polars/polars/exceptions.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/py-polars/polars/exceptions.py b/py-polars/polars/exceptions.py index 386f1a1758d0e..23f9d43c60560 100644 --- a/py-polars/polars/exceptions.py +++ b/py-polars/polars/exceptions.py @@ -23,16 +23,40 @@ class PolarsError(Exception): # type: ignore[no-redef] """Base class for all Polars errors.""" class ColumnNotFoundError(PolarsError): # type: ignore[no-redef, misc] - """Exception raised when a specified column is not found.""" + """ + Exception raised when a specified column is not found. + + Example + ------- + >>> df = pl.DataFrame({"a": [1, 2, 3]}) + >>> df.select("b") + polars.exceptions.ColumnNotFoundError: b + """ class ComputeError(PolarsError): # type: ignore[no-redef, misc] """Exception raised when polars could not finish the computation.""" class DuplicateError(PolarsError): # type: ignore[no-redef, misc] - """Exception raised when a column name is duplicated.""" + """ + Exception raised when a column name is duplicated. + + Example + ------- + >>> 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 + """ # noqa: W505 class InvalidOperationError(PolarsError): # type: ignore[no-redef, misc] - """Exception raised when an operation is not allowed on a certain data type.""" + """ + Exception raised when an operation is not allowed on a certain data type. + + Example + ------- + >>> 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 + """ # noqa: W505 class NoDataError(PolarsError): # type: ignore[no-redef, misc] """Exception raised when an operation can not be performed on an empty data structure.""" # noqa: W505