Skip to content

Commit

Permalink
docs(python): Include warning in docstrings that accessing `LazyFrame…
Browse files Browse the repository at this point in the history
…` properties may be expensive (#16618)
  • Loading branch information
stinodego authored May 31, 2024
1 parent 65b8cdc commit 7c7e834
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 13 deletions.
44 changes: 36 additions & 8 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,11 @@ def shape(self) -> tuple[int, int]:
@property
def height(self) -> int:
"""
Get the height of the DataFrame.
Get the number of rows.
Returns
-------
int
Examples
--------
Expand All @@ -641,13 +645,22 @@ def height(self) -> int:
@property
def width(self) -> int:
"""
Get the width of the DataFrame.
Get the number of columns.
Returns
-------
int
Examples
--------
>>> df = pl.DataFrame({"foo": [1, 2, 3, 4, 5]})
>>> df = pl.DataFrame(
... {
... "foo": [1, 2, 3],
... "bar": [4, 5, 6],
... }
... )
>>> df.width
1
2
"""
return self._df.width()

Expand All @@ -656,6 +669,11 @@ def columns(self) -> list[str]:
"""
Get or set column names.
Returns
-------
list of str
A list containing the name of each column in order.
Examples
--------
>>> df = pl.DataFrame(
Expand Down Expand Up @@ -701,13 +719,18 @@ def columns(self, names: Sequence[str]) -> None:
@property
def dtypes(self) -> list[DataType]:
"""
Get the datatypes of the columns of this DataFrame.
Get the column data types.
The datatypes can also be found in column headers when printing the DataFrame.
The data types can also be found in column headers when printing the DataFrame.
Returns
-------
list of DataType
A list containing the data type of each column in order.
See Also
--------
schema : Returns a {colname:dtype} mapping.
schema
Examples
--------
Expand Down Expand Up @@ -749,7 +772,12 @@ def flags(self) -> dict[str, dict[str, bool]]:
@property
def schema(self) -> OrderedDict[str, DataType]:
"""
Get a dict[column name, DataType].
Get a mapping of column names to their data type.
Returns
-------
OrderedDict
An ordered mapping of column names to their data type.
Examples
--------
Expand Down
52 changes: 47 additions & 5 deletions py-polars/polars/lazyframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,18 @@ def deserialize(cls, source: str | Path | IOBase) -> Self:
@property
def columns(self) -> list[str]:
"""
Get column names.
Get the column names.
Returns
-------
list of str
A list containing the name of each column in order.
Warnings
--------
Determining the column names of a LazyFrame requires resolving its schema.
Resolving the schema of a LazyFrame can be an expensive operation.
Avoid accessing this property repeatedly if possible.
Examples
--------
Expand All @@ -411,11 +422,22 @@ def columns(self) -> list[str]:
@property
def dtypes(self) -> list[DataType]:
"""
Get dtypes of columns in LazyFrame.
Get the column data types.
Returns
-------
list of DataType
A list containing the data type of each column in order.
Warnings
--------
Determining the data types of a LazyFrame requires resolving its schema.
Resolving the schema of a LazyFrame can be an expensive operation.
Avoid accessing this property repeatedly if possible.
See Also
--------
schema : Returns a {colname:dtype} mapping.
schema
Examples
--------
Expand All @@ -434,7 +456,17 @@ def dtypes(self) -> list[DataType]:
@property
def schema(self) -> OrderedDict[str, DataType]:
"""
Get a dict[column name, DataType].
Get a mapping of column names to their data type.
Returns
-------
OrderedDict
An ordered mapping of column names to their data type.
Warnings
--------
Resolving the schema of a LazyFrame can be an expensive operation.
Avoid accessing this property repeatedly if possible.
Examples
--------
Expand All @@ -453,7 +485,17 @@ def schema(self) -> OrderedDict[str, DataType]:
@property
def width(self) -> int:
"""
Get the width of the LazyFrame.
Get the number of columns.
Returns
-------
int
Warnings
--------
Determining the width of a LazyFrame requires resolving its schema.
Resolving the schema of a LazyFrame can be an expensive operation.
Avoid accessing this property repeatedly if possible.
Examples
--------
Expand Down

0 comments on commit 7c7e834

Please sign in to comment.