-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-40839][CONNECT][PYTHON] Implement DataFrame.sample
#38310
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -206,6 +206,33 @@ def sort(self, *cols: "ColumnOrString") -> "DataFrame": | |
| """Sort by a specific column""" | ||
| return DataFrame.withPlan(plan.Sort(self._plan, *cols), session=self._session) | ||
|
|
||
| def sample( | ||
| self, | ||
| fraction: float, | ||
| *, | ||
| withReplacement: bool = False, | ||
| seed: Optional[int] = None, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should just leverage keyword-only argument which will make the logic much simpler. Actually we wanted to do it in PySpark API layer in the past. Since this is a new API layer, I think it;s a good chance to replace them. cc @ueshin
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, that's a bit confusing at first glance.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, if we can break the signature, it would be: def sample(
self,
fraction: float,
*,
withReplacement: Optional[bool] = None,
seed: Optional[int] = None,
) -> "DataFrame":
...
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this idea |
||
| ) -> "DataFrame": | ||
| if not isinstance(fraction, float): | ||
| raise TypeError(f"'fraction' must be float, but got {type(fraction).__name__}") | ||
| if not isinstance(withReplacement, bool): | ||
| raise TypeError( | ||
| f"'withReplacement' must be bool, but got {type(withReplacement).__name__}" | ||
| ) | ||
| if seed is not None and not isinstance(seed, int): | ||
| raise TypeError(f"'seed' must be None or int, but got {type(seed).__name__}") | ||
|
|
||
| return DataFrame.withPlan( | ||
| plan.Sample( | ||
| child=self._plan, | ||
| lower_bound=0.0, | ||
| upper_bound=fraction, | ||
| with_replacement=withReplacement, | ||
| seed=seed, | ||
| ), | ||
| session=self._session, | ||
| ) | ||
|
|
||
| def show(self, n: int, truncate: Optional[Union[bool, int]], vertical: Optional[bool]) -> None: | ||
| ... | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pyspark dataframe API has
Can we match (as easy as copy the API into connect dataframe.py)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we can discard those ones ? @HyukjinKwon
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe my real question was, will we have an issue to be compatible with existing pyspark dataframe code (needs different imports, of course) if we discard such API? I see many other similar API existing for pyspark dataframe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
users may have to change their codes for this emigration, but I think this is also a chance to make some changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. We also can go to that direction.