-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: maximlt <[email protected]> Co-authored-by: Maxime Liquet <[email protected]>
- Loading branch information
1 parent
8062d52
commit 4e9436d
Showing
6 changed files
with
158 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"""Adds the `.hvplot` method to pl.DataFrame, pl.LazyFrame and pl.Series""" | ||
import itertools | ||
|
||
from hvplot import hvPlotTabular, post_patch | ||
from hvplot.converter import HoloViewsConverter | ||
from hvplot.util import is_list_like | ||
|
||
|
||
class hvPlotTabularPolars(hvPlotTabular): | ||
def _get_converter(self, x=None, y=None, kind=None, **kwds): | ||
import polars as pl | ||
|
||
params = dict(self._metadata, **kwds) | ||
x = x or params.pop("x", None) | ||
y = y or params.pop("y", None) | ||
kind = kind or params.pop("kind", None) | ||
|
||
# Find columns which should be converted for LazyDataFrame and DataFrame | ||
if isinstance(self._data, (pl.LazyFrame, pl.DataFrame)): | ||
if params.get("hover_cols") == "all": | ||
columns = list(self._data.columns) | ||
else: | ||
possible_columns = [ | ||
[v] if isinstance(v, str) else v | ||
for v in params.values() | ||
if isinstance(v, (str, list)) | ||
] | ||
columns = ( | ||
set(self._data.columns) & set(itertools.chain(*possible_columns)) | ||
) or {self._data.columns[0]} | ||
xs = x if is_list_like(x) else (x,) | ||
ys = y if is_list_like(y) else (y,) | ||
columns |= {*xs, *ys} | ||
columns.discard(None) | ||
|
||
if isinstance(self._data, pl.DataFrame): | ||
data = self._data.select(columns).to_pandas() | ||
elif isinstance(self._data, pl.Series): | ||
data = self._data.to_pandas() | ||
elif isinstance(self._data, pl.LazyFrame): | ||
data = self._data.select(columns).collect().to_pandas() | ||
else: | ||
raise ValueError( | ||
"Only Polars DataFrame, Series, and LazyFrame are supported" | ||
) | ||
|
||
return HoloViewsConverter(data, x, y, kind=kind, **params) | ||
|
||
|
||
def patch(name="hvplot", extension="bokeh", logo=False): | ||
try: | ||
import polars as pl | ||
except: | ||
raise ImportError( | ||
"Could not patch plotting API onto Polars. Polars could not be imported." | ||
) | ||
pl.api.register_dataframe_namespace(name)(hvPlotTabularPolars) | ||
pl.api.register_series_namespace(name)(hvPlotTabularPolars) | ||
pl.api.register_lazyframe_namespace(name)(hvPlotTabularPolars) | ||
|
||
post_patch(extension, logo) | ||
|
||
|
||
patch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,66 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import hvplot.pandas # noqa | ||
|
||
import pytest | ||
|
||
@pytest.mark.parametrize("y", ( | ||
from hvplot import hvPlotTabular | ||
|
||
try: | ||
import polars as pl | ||
import hvplot.polars # noqa | ||
skip_polar = False | ||
except ImportError: | ||
class pl: | ||
DataFrame = None | ||
LazyFrame = None | ||
Series = None | ||
skip_polar = True | ||
|
||
|
||
TYPES = {t for t in dir(hvPlotTabular) if not t.startswith("_")} | ||
FRAME_TYPES = TYPES - {"bivariate", "heatmap", "hexbin", "labels", "vectorfield"} | ||
SERIES_TYPES = FRAME_TYPES - {"points", "polygons", "ohlc", "paths"} | ||
frame_kinds = pytest.mark.parametrize("kind", FRAME_TYPES) | ||
series_kinds = pytest.mark.parametrize("kind", SERIES_TYPES) | ||
|
||
y_combinations = pytest.mark.parametrize("y", ( | ||
["A", "B", "C", "D"], | ||
("A", "B", "C", "D"), | ||
{"A", "B", "C", "D"}, | ||
np.array(["A", "B", "C", "D"]), | ||
pd.Index(["A", "B", "C", "D"]), | ||
pd.Series(["A", "B", "C", "D"]), | ||
)) | ||
def test_diffent_input_types(y): | ||
), | ||
ids=lambda x: type(x).__name__ | ||
) | ||
|
||
|
||
@frame_kinds | ||
@y_combinations | ||
def test_dataframe_pandas(kind, y): | ||
df = pd._testing.makeDataFrame() | ||
types = {t for t in dir(df.hvplot) if not t.startswith("_")} | ||
ignore_types = {'bivariate', 'heatmap', 'hexbin', 'labels', 'vectorfield'} | ||
df.hvplot(y=y, kind=kind) | ||
|
||
|
||
@series_kinds | ||
def test_series_pandas(kind): | ||
ser = pd.Series(np.random.rand(10), name="A") | ||
ser.hvplot(kind=kind) | ||
|
||
|
||
@pytest.mark.skipif(skip_polar, reason="polars not installed") | ||
@pytest.mark.parametrize("cast", (pl.DataFrame, pl.LazyFrame)) | ||
@frame_kinds | ||
@y_combinations | ||
def test_dataframe_polars(kind, y, cast): | ||
df = cast(pd._testing.makeDataFrame()) | ||
assert isinstance(df, cast) | ||
df.hvplot(y=y, kind=kind) | ||
|
||
|
||
for t in types - ignore_types: | ||
df.hvplot(y=y, kind=t) | ||
@pytest.mark.skipif(skip_polar, reason="polars not installed") | ||
@series_kinds | ||
def test_series_polars(kind): | ||
ser = pl.Series(values=np.random.rand(10), name="A") | ||
assert isinstance(ser, pl.Series) | ||
ser.hvplot(kind=kind) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters