Skip to content

ENH: Add basic support for pd.DataFrame.hist pd.Series.hist #14

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

Merged
merged 4 commits into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions altair_pandas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Altair plotting extension for pandas."""
__version__ = "0.1.0dev0"
__all__ = ["plot"]
__all__ = ["plot", "hist_frame", "hist_series"]

from ._core import plot
from ._core import plot, hist_frame, hist_series
24 changes: 24 additions & 0 deletions altair_pandas/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ def hist(self, bins=None, **kwargs):
)
)

def hist_series(self, **kwargs):
return self.hist(**kwargs)

def box(self, **kwargs):
data = self._preprocess_data(with_index=False)
return (
Expand Down Expand Up @@ -190,6 +193,19 @@ def hist(self, bins=None, stacked=None, **kwargs):
)
)

def hist_frame(self, grid_columns=2, **kwargs):
data = self._preprocess_data(with_index=False)
data = data._get_numeric_data()
return (
alt.Chart(data)
.mark_bar()
.encode(
x=alt.X(alt.repeat("repeat"), type="quantitative", bin=True),
y=alt.Y("count()", title="Frequency"),
)
.repeat(repeat=list(data.columns), columns=grid_columns)
)

def box(self, **kwargs):
data = self._preprocess_data(with_index=False)
return (
Expand All @@ -210,3 +226,11 @@ def plot(data, kind="line", **kwargs):
raise NotImplementedError(f"kind='{kind}' for data of type {type(data)}")

return plotfunc(**kwargs)


def hist_frame(data, **kwargs):
return _PandasPlotter.create(data).hist_frame(**kwargs)


def hist_series(data, **kwargs):
return _PandasPlotter.create(data).hist_series(**kwargs)
20 changes: 20 additions & 0 deletions altair_pandas/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,23 @@ def test_dataframe_boxplot(dataframe, with_plotting_backend):
assert spec["encoding"]["x"]["field"] == "column"
assert spec["encoding"]["y"]["field"] == "value"
assert spec["transform"][0]["fold"] == ["x", "y"]


def test_dataframe_hist_series(series, with_plotting_backend):
chart = series.hist()
spec = chart.to_dict()
assert spec["mark"] == "bar"
assert spec["encoding"]["x"]["field"] == "data_name"
assert "field" not in spec["encoding"]["y"]
assert spec["encoding"]["x"]["bin"] == {"maxbins": 10}


def test_dataframe_hist_frame(dataframe, with_plotting_backend):
chart = dataframe.hist()
spec = chart.to_dict()
assert spec["repeat"] == ["x", "y"]
assert spec["columns"] == 2
assert spec["spec"]["mark"] == "bar"
assert spec["spec"]["encoding"]["x"]["field"] == {"repeat": "repeat"}
assert spec["spec"]["encoding"]["x"]["bin"] is True
assert "field" not in spec["spec"]["encoding"]["y"]