Skip to content

Commit c10585d

Browse files
authored
Merge pull request #14 from pratapvardhan/hist_func
ENH: Add basic support for pd.DataFrame.hist pd.Series.hist
2 parents ae5c846 + 688faf9 commit c10585d

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

altair_pandas/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Altair plotting extension for pandas."""
22
__version__ = "0.1.0dev0"
3-
__all__ = ["plot"]
3+
__all__ = ["plot", "hist_frame", "hist_series"]
44

5-
from ._core import plot
5+
from ._core import plot, hist_frame, hist_series

altair_pandas/_core.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ def hist(self, bins=None, **kwargs):
8787
)
8888
)
8989

90+
def hist_series(self, **kwargs):
91+
return self.hist(**kwargs)
92+
9093
def box(self, **kwargs):
9194
data = self._preprocess_data(with_index=False)
9295
return (
@@ -190,6 +193,19 @@ def hist(self, bins=None, stacked=None, **kwargs):
190193
)
191194
)
192195

196+
def hist_frame(self, grid_columns=2, **kwargs):
197+
data = self._preprocess_data(with_index=False)
198+
data = data._get_numeric_data()
199+
return (
200+
alt.Chart(data)
201+
.mark_bar()
202+
.encode(
203+
x=alt.X(alt.repeat("repeat"), type="quantitative", bin=True),
204+
y=alt.Y("count()", title="Frequency"),
205+
)
206+
.repeat(repeat=list(data.columns), columns=grid_columns)
207+
)
208+
193209
def box(self, **kwargs):
194210
data = self._preprocess_data(with_index=False)
195211
return (
@@ -210,3 +226,11 @@ def plot(data, kind="line", **kwargs):
210226
raise NotImplementedError(f"kind='{kind}' for data of type {type(data)}")
211227

212228
return plotfunc(**kwargs)
229+
230+
231+
def hist_frame(data, **kwargs):
232+
return _PandasPlotter.create(data).hist_frame(**kwargs)
233+
234+
235+
def hist_series(data, **kwargs):
236+
return _PandasPlotter.create(data).hist_series(**kwargs)

altair_pandas/test_plotting.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,23 @@ def test_dataframe_boxplot(dataframe, with_plotting_backend):
150150
assert spec["encoding"]["x"]["field"] == "column"
151151
assert spec["encoding"]["y"]["field"] == "value"
152152
assert spec["transform"][0]["fold"] == ["x", "y"]
153+
154+
155+
def test_dataframe_hist_series(series, with_plotting_backend):
156+
chart = series.hist()
157+
spec = chart.to_dict()
158+
assert spec["mark"] == "bar"
159+
assert spec["encoding"]["x"]["field"] == "data_name"
160+
assert "field" not in spec["encoding"]["y"]
161+
assert spec["encoding"]["x"]["bin"] == {"maxbins": 10}
162+
163+
164+
def test_dataframe_hist_frame(dataframe, with_plotting_backend):
165+
chart = dataframe.hist()
166+
spec = chart.to_dict()
167+
assert spec["repeat"] == ["x", "y"]
168+
assert spec["columns"] == 2
169+
assert spec["spec"]["mark"] == "bar"
170+
assert spec["spec"]["encoding"]["x"]["field"] == {"repeat": "repeat"}
171+
assert spec["spec"]["encoding"]["x"]["bin"] is True
172+
assert "field" not in spec["spec"]["encoding"]["y"]

0 commit comments

Comments
 (0)