Skip to content
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

Add DataFrame.plot.kde() (an alias of 'density') #784

Merged
merged 8 commits into from
Sep 28, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions databricks/koalas/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,11 @@ def hist(self, bins=10, **kwds):

hist.__doc__ = KoalasFramePlotMethods.hist.__doc__

def kde(self, bw_method=None, ind=None, **kwds):
return self.plot.kde(bw_method, ind, **kwds)

kde.__doc__ = KoalasFramePlotMethods.kde.__doc__

add.__doc__ = _flex_doc_FRAME.format(
desc='Addition',
op_name='+',
Expand Down
31 changes: 24 additions & 7 deletions databricks/koalas/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,6 @@ def _plot(
cls, ax, y, style=None, bw_method=None, ind=None,
column_num=None, stacking_id=None, **kwds):
# 'y' is a Spark DataFrame that selects one column.

itholic marked this conversation as resolved.
Show resolved Hide resolved
# Using RDD is slow so we might have to change it to Dataset based implementation
# once Spark has that implementation.
sample = y.rdd.map(lambda x: float(x[0]))
Expand Down Expand Up @@ -919,7 +918,6 @@ def _plot(data, x=None, y=None, subplots=False,
if kind in ('scatter', 'hexbin'):
plot_obj = klass(data, x, y, subplots=subplots, ax=ax, kind=kind, **kwds)
else:

itholic marked this conversation as resolved.
Show resolved Hide resolved
# check data type and do preprocess before applying plot
if isinstance(data, DataFrame):
if x is not None:
Expand Down Expand Up @@ -1246,8 +1244,30 @@ def line(self, x=None, y=None, **kwargs):
"""
return self(kind='line', x=x, y=y, **kwargs)

def kde(self, bw_method=None, ind=None, **kwds):
return _unsupported_function(class_name='pd.DataFrame', method_name='kde')()
def kde(self, bw_method=None, ind=None, **kwargs):
"""
Generate Kernel Density Estimate plot using Gaussian kernels.

Parameters
----------
bw_method : scalar
The method used to calculate the estimator bandwidth.
See KernelDensity in PySpark for more information.
ind : NumPy array or integer, optional
Evaluation points for the estimated PDF. If None (default),
1000 equally spaced points are used. If `ind` is a NumPy array, the
KDE is evaluated at the points passed. If `ind` is an integer,
`ind` number of equally spaced points are used.
**kwargs : optional
Keyword arguments to pass on to :meth:`Koalas.DataFrame.plot`.

Returns
-------
matplotlib.axes.Axes or numpy.ndarray of them
"""
return self(kind="kde", bw_method=bw_method, ind=ind, **kwargs)

density = kde

def pie(self, y=None, **kwds):
"""
Expand Down Expand Up @@ -1362,9 +1382,6 @@ def barh(self, x=None, y=None, **kwargs):
def hexbin(self, **kwds):
return _unsupported_function(class_name='pd.DataFrame', method_name='hexbin')()

def density(self, **kwds):
return _unsupported_function(class_name='pd.DataFrame', method_name='density')()

def box(self, **kwds):
return _unsupported_function(class_name='pd.DataFrame', method_name='box')()

Expand Down
18 changes: 17 additions & 1 deletion databricks/koalas/tests/test_frame_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,26 @@ def _test_hist_plot(pdf, kdf):
kdf.columns = columns
_test_hist_plot(pdf, kdf)

def test_kde_plot(self):
pdf = self.pdf1
kdf = self.kdf1

ax1 = pdf.plot.kde(bw_method=3)
ax2 = kdf.plot.kde(bw_method=3)
self.compare_plots(ax1, ax2)

ax1 = pdf.plot(kind='kde', bw_method=3)
ax2 = kdf.plot(kind='kde', bw_method=3)
self.compare_plots(ax1, ax2)

ax1 = pdf.plot.kde(bw_method=3, ind=[1, 2, 3])
ax2 = kdf.plot.kde(bw_method=3, ind=[1, 2, 3])
self.compare_plots(ax1, ax2)
itholic marked this conversation as resolved.
Show resolved Hide resolved

def test_missing(self):
ks = self.kdf1

unsupported_functions = ['box', 'density', 'hexbin', 'kde']
unsupported_functions = ['box', 'hexbin']

for name in unsupported_functions:
with self.assertRaisesRegex(PandasNotImplementedError,
Expand Down
4 changes: 3 additions & 1 deletion docs/source/reference/frame.rst
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,6 @@ specific plotting methods of the form ``DataFrame.plot.<kind>``.
DataFrame.plot.line
DataFrame.plot.pie
DataFrame.plot.scatter
DataFrame.hist
DataFrame.plot.hist
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like DataFrame.plot.hist is listed twice.
I'm not sure we should keep DataFrame.hist, though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@itholic can you fix this? DataFrame.hist seems in the pandas doc as an alias. Let's keep it for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, fixed in #851
Thanks for comment !!

DataFrame.plot.kde
DataFrame.plot.density