Github user felixcheung commented on a diff in the pull request: https://github.com/apache/spark/pull/18999#discussion_r134123764 --- Diff: python/pyspark/sql/dataframe.py --- @@ -659,19 +659,77 @@ def distinct(self): return DataFrame(self._jdf.distinct(), self.sql_ctx) @since(1.3) - def sample(self, withReplacement, fraction, seed=None): + def sample(self, withReplacement=None, fraction=None, seed=None): """Returns a sampled subset of this :class:`DataFrame`. + :param withReplacement: Sample with replacement or not (default False). + :param fraction: Fraction of rows to generate, range [0.0, 1.0]. + :param seed: Seed for sampling (default a random seed). + .. note:: This is not guaranteed to provide exactly the fraction specified of the total count of the given :class:`DataFrame`. - >>> df.sample(False, 0.5, 42).count() - 2 - """ - assert fraction >= 0.0, "Negative fraction value: %s" % fraction - seed = seed if seed is not None else random.randint(0, sys.maxsize) - rdd = self._jdf.sample(withReplacement, fraction, long(seed)) - return DataFrame(rdd, self.sql_ctx) + .. note:: `fraction` is required and, `withReplacement` and `seed` are optional. + + >>> df = spark.range(10) + >>> df.sample(0.5, 3).count() + 4 + >>> df.sample(fraction=0.5, seed=3).count() + 4 + >>> df.sample(withReplacement=True, fraction=0.5, seed=3).count() + 1 + >>> df.sample(1.0).count() + 10 + >>> df.sample(fraction=1.0).count() + 10 + >>> df.sample(False, fraction=1.0).count() + 10 + >>> df.sample("a").count() + Traceback (most recent call last): + ... + TypeError:... + >>> df.sample(seed="abc").count() + Traceback (most recent call last): + ... + TypeError:... --- End diff -- maybe we don't do the error cases here in doctest, but move them to unit test instead? also these cases aren't really that meaningfully different to me as an user....? ``` >>> df.sample(0.5, 3).count() + 4 + >>> df.sample(fraction=0.5, seed=3).count() + 4 + >>> df.sample(1.0).count() + 10 + >>> df.sample(fraction=1.0).count() + 10 + >>> df.sample(False, fraction=1.0).count() + 10 ```
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org