我正在浏览Koalas的源代码,试图了解它们如何真正实现绘制大型数据集。事实证明,他们要么使用抽样,要么使用TopN
- 选择给定数量的记录。
我了解采样的含义,并且在内部使用它spark.DataFrame.sample
来执行此操作。但是,对于TopN
,他们只是max_rows
使用data = data.head(max_rows + 1).to_pandas()
.
这看起来很奇怪,我想知道它是否正确反映了以这种方式进行数据选择的数据集的统计属性。
Koalas DataFrame 的绘图访问器:
class KoalasPlotAccessor(PandasObject):
pandas_plot_data_map = {
"pie": TopNPlotBase().get_top_n,
"bar": TopNPlotBase().get_top_n,
"barh": TopNPlotBase().get_top_n,
"scatter": SampledPlotBase().get_sampled,
"area": SampledPlotBase().get_sampled,
"line": SampledPlotBase().get_sampled,
}
_backends = {} # type: ignore
...
class TopNPlotBase:
def get_top_n(self, data):
from databricks.koalas import DataFrame, Series
max_rows = get_option("plotting.max_rows")
# Simply use the first 1k elements and make it into a pandas dataframe
# For categorical variables, it is likely called from df.x.value_counts().plot.xxx().
if isinstance(data, (Series, DataFrame)):
data = data.head(max_rows + 1).to_pandas()
...