Skip to content

Commit f678c75

Browse files
dchvnHyukjinKwon
andcommitted
[SPARK-36973][PYTHON] Deduplicate prepare data method for HistogramPlotBase and KdePlotBase
### What changes were proposed in this pull request? Deduplicate prepare data method for HistogramPlotBase and KdePlotBase ### Why are the changes needed? Deduplicate code Remove 2 ```TODO``` comment ### Does this PR introduce _any_ user-facing change? No, only for Dev ### How was this patch tested? Existing tests Closes #34251 from dchvn/SPARK-36973. Lead-authored-by: dch nguyen <dgd_contributor@viettel.com.vn> Co-authored-by: Hyukjin Kwon <gurwls223@gmail.com> Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
1 parent 5982162 commit f678c75

1 file changed

Lines changed: 11 additions & 20 deletions

File tree

python/pyspark/pandas/plot/core.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,9 @@ def set_result_text(self, ax):
9898
)
9999

100100

101-
class HistogramPlotBase:
101+
class NumericPlotBase:
102102
@staticmethod
103-
def prepare_hist_data(data, bins):
104-
# TODO: this logic is similar with KdePlotBase. Might have to deduplicate it.
103+
def prepare_numeric_data(data):
105104
from pyspark.pandas.series import Series
106105

107106
if isinstance(data, Series):
@@ -117,6 +116,13 @@ def prepare_hist_data(data, bins):
117116
"Empty {0!r}: no numeric data to " "plot".format(numeric_data.__class__.__name__)
118117
)
119118

119+
return data, numeric_data
120+
121+
122+
class HistogramPlotBase(NumericPlotBase):
123+
@staticmethod
124+
def prepare_hist_data(data, bins):
125+
data, numeric_data = NumericPlotBase.prepare_numeric_data(data)
120126
if is_integer(bins):
121127
# computes boundaries for the column
122128
bins = HistogramPlotBase.get_bins(data.to_spark(), bins)
@@ -340,25 +346,10 @@ def get_fliers(colname, outliers, min_val):
340346
return fliers
341347

342348

343-
class KdePlotBase:
349+
class KdePlotBase(NumericPlotBase):
344350
@staticmethod
345351
def prepare_kde_data(data):
346-
# TODO: this logic is similar with HistogramPlotBase. Might have to deduplicate it.
347-
from pyspark.pandas.series import Series
348-
349-
if isinstance(data, Series):
350-
data = data.to_frame()
351-
352-
numeric_data = data.select_dtypes(
353-
include=["byte", "decimal", "integer", "float", "long", "double", np.datetime64]
354-
)
355-
356-
# no empty frames or series allowed
357-
if len(numeric_data.columns) == 0:
358-
raise TypeError(
359-
"Empty {0!r}: no numeric data to " "plot".format(numeric_data.__class__.__name__)
360-
)
361-
352+
_, numeric_data = NumericPlotBase.prepare_numeric_data(data)
362353
return numeric_data
363354

364355
@staticmethod

0 commit comments

Comments
 (0)