From ea018b50f5dc8c281b752011c006cf700e2d115b Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Mon, 19 Aug 2024 09:27:13 +0100 Subject: [PATCH] assorted docs and typing improvements --- py-polars/polars/dataframe/frame.py | 2 +- py-polars/polars/dataframe/plotting.py | 25 ++++++++++--------- py-polars/polars/series/plotting.py | 33 +++++--------------------- py-polars/polars/series/series.py | 4 ++-- 4 files changed, 21 insertions(+), 43 deletions(-) diff --git a/py-polars/polars/dataframe/frame.py b/py-polars/polars/dataframe/frame.py index 4bb953126f58..427ac0031d56 100644 --- a/py-polars/polars/dataframe/frame.py +++ b/py-polars/polars/dataframe/frame.py @@ -614,7 +614,7 @@ def plot(self) -> DataFramePlot: .. versionchanged:: 1.6.0 In prior versions of Polars, HvPlot was the plotting backend. If you would like to restore the previous plotting functionality, all you need to do - add `import hvplot.polars` at the top of your script and replace + is add `import hvplot.polars` at the top of your script and replace `df.plot` with `df.hvplot`. Polars does not implement plotting logic itself, but instead defers to diff --git a/py-polars/polars/dataframe/plotting.py b/py-polars/polars/dataframe/plotting.py index fe207d4da156..ed118e504656 100644 --- a/py-polars/polars/dataframe/plotting.py +++ b/py-polars/polars/dataframe/plotting.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Callable, Dict, Union +from typing import TYPE_CHECKING, Callable, Dict, Union if TYPE_CHECKING: import sys @@ -38,12 +38,10 @@ class DataFramePlot: """DataFrame.plot namespace.""" - chart: alt.Chart - def __init__(self, df: DataFrame) -> None: import altair as alt - self.chart = alt.Chart(df) + self._chart = alt.Chart(df) def bar( self, @@ -68,7 +66,7 @@ def bar( .. versionchanged:: 1.6.0 In prior versions of Polars, HvPlot was the plotting backend. If you would like to restore the previous plotting functionality, all you need to do - add `import hvplot.polars` at the top of your script and replace + is add `import hvplot.polars` at the top of your script and replace `df.plot` with `df.hvplot`. Parameters @@ -106,7 +104,7 @@ def bar( encodings["color"] = color if tooltip is not None: encodings["tooltip"] = tooltip - return self.chart.mark_bar().encode(**encodings, **kwargs).interactive() + return self._chart.mark_bar().encode(**encodings, **kwargs).interactive() def line( self, @@ -131,7 +129,7 @@ def line( .. versionchanged:: 1.6.0 In prior versions of Polars, HvPlot was the plotting backend. If you would like to restore the previous plotting functionality, all you need to do - add `import hvplot.polars` at the top of your script and replace + is add `import hvplot.polars` at the top of your script and replace `df.plot` with `df.hvplot`. Parameters @@ -172,7 +170,7 @@ def line( encodings["order"] = order if tooltip is not None: encodings["tooltip"] = tooltip - return self.chart.mark_line().encode(**encodings, **kwargs).interactive() + return self._chart.mark_line().encode(**encodings, **kwargs).interactive() def point( self, @@ -181,7 +179,8 @@ def point( color: ChannelColor | None = None, size: ChannelSize | None = None, tooltip: ChannelTooltip | None = None, - **kwargs: Any, + /, + **kwargs: Unpack[EncodeKwds], ) -> alt.Chart: """ Draw scatter plot. @@ -197,7 +196,7 @@ def point( .. versionchanged:: 1.6.0 In prior versions of Polars, HvPlot was the plotting backend. If you would like to restore the previous plotting functionality, all you need to do - add `import hvplot.polars` at the top of your script and replace + is add `import hvplot.polars` at the top of your script and replace `df.plot` with `df.hvplot`. Parameters @@ -238,9 +237,9 @@ def point( if tooltip is not None: encodings["tooltip"] = tooltip return ( - self.chart.mark_point() + self._chart.mark_point() .encode( - **encodings, # type: ignore[arg-type] + **encodings, **kwargs, ) .interactive() @@ -250,7 +249,7 @@ def point( scatter = point def __getattr__(self, attr: str) -> Callable[..., alt.Chart]: - method = getattr(self.chart, f"mark_{attr}", None) + method = getattr(self._chart, f"mark_{attr}", None) if method is None: msg = "Altair has no method 'mark_{attr}'" raise AttributeError(msg) diff --git a/py-polars/polars/series/plotting.py b/py-polars/polars/series/plotting.py index 06b0379ee0ad..c666c5a9b177 100644 --- a/py-polars/polars/series/plotting.py +++ b/py-polars/polars/series/plotting.py @@ -1,26 +1,14 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Callable, Dict, Union +from typing import TYPE_CHECKING, Callable from polars.dependencies import altair as alt if TYPE_CHECKING: import sys - from altair.typing import ( - ChannelColor, - ChannelOrder, - ChannelSize, - ChannelTooltip, - ChannelX, - ChannelY, - EncodeKwds, - ) - - if sys.version_info >= (3, 10): - from typing import TypeAlias - else: - from typing_extensions import TypeAlias + from altair.typing import EncodeKwds + if sys.version_info >= (3, 11): from typing import Unpack else: @@ -28,21 +16,12 @@ from polars import Series - Encodings: TypeAlias = Dict[ - str, - Union[ - ChannelX, ChannelY, ChannelColor, ChannelOrder, ChannelSize, ChannelTooltip - ], - ] - class SeriesPlot: """Series.plot namespace.""" _accessor = "plot" - chart: alt.Chart - def __init__(self, s: Series) -> None: name = s.name or "value" self._df = s.to_frame(name) @@ -67,7 +46,7 @@ def hist( .. versionchanged:: 1.6.0 In prior versions of Polars, HvPlot was the plotting backend. If you would like to restore the previous plotting functionality, all you need to do - add `import hvplot.polars` at the top of your script and replace + is add `import hvplot.polars` at the top of your script and replace `df.plot` with `df.hvplot`. Parameters @@ -109,7 +88,7 @@ def kde( .. versionchanged:: 1.6.0 In prior versions of Polars, HvPlot was the plotting backend. If you would like to restore the previous plotting functionality, all you need to do - add `import hvplot.polars` at the top of your script and replace + is add `import hvplot.polars` at the top of your script and replace `df.plot` with `df.hvplot`. Parameters @@ -152,7 +131,7 @@ def line( .. versionchanged:: 1.6.0 In prior versions of Polars, HvPlot was the plotting backend. If you would like to restore the previous plotting functionality, all you need to do - add `import hvplot.polars` at the top of your script and replace + is add `import hvplot.polars` at the top of your script and replace `df.plot` with `df.hvplot`. Parameters diff --git a/py-polars/polars/series/series.py b/py-polars/polars/series/series.py index 8cf154090d61..b61198fb9deb 100644 --- a/py-polars/polars/series/series.py +++ b/py-polars/polars/series/series.py @@ -7370,7 +7370,7 @@ def plot(self) -> SeriesPlot: .. versionchanged:: 1.6.0 In prior versions of Polars, HvPlot was the plotting backend. If you would like to restore the previous plotting functionality, all you need to do - add `import hvplot.polars` at the top of your script and replace + is add `import hvplot.polars` at the top of your script and replace `df.plot` with `df.hvplot`. Polars does not implement plotting logic itself, but instead defers to @@ -7384,7 +7384,7 @@ def plot(self) -> SeriesPlot: `alt.Chart(s.to_frame()).transform_density(s.name, as_=[s.name, 'density']).mark_area().encode(x=s.name, y='density:Q', **kwargs).interactive()` - for any other attribute `attr`, `s.plot.attr(**kwargs)` is shorthand for - `alt.Chart(s.to_frame().with_row_index()).mark_attr().encode(x=s.name, y='index', **kwargs).interactive()` + `alt.Chart(s.to_frame().with_row_index()).mark_attr().encode(x='index', y=s.name, **kwargs).interactive()` Examples --------