-
Notifications
You must be signed in to change notification settings - Fork 358
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
Exclude Index columns for exposed Spark DataFrame and disallow Koalas DataFrame with no index #655
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -682,7 +682,6 @@ def cumsum(self): | |
""" | ||
return self._cum(F.sum) | ||
|
||
# TODO: Series support is not implemented yet. | ||
def apply(self, func): | ||
""" | ||
Apply function `func` group-wise and combine the results together. | ||
|
@@ -797,7 +796,31 @@ def apply(self, func): | |
return_schema = None # schema will inferred. | ||
else: | ||
return_schema = _infer_return_type(func).tpe | ||
return self._apply(func, return_schema, retain_index=return_schema is None) | ||
|
||
should_infer_schema = return_schema is None | ||
input_groupnames = [s.name for s in self._groupkeys] | ||
|
||
if should_infer_schema: | ||
# Here we execute with the first 1000 to get the return type. | ||
# If the records were less than 1000, it uses pandas API directly for a shortcut. | ||
limit = 1000 | ||
pdf = self._kdf.head(limit + 1).to_pandas() | ||
pdf = pdf.groupby(input_groupnames).apply(func) | ||
kdf = DataFrame(pdf) | ||
return_schema = kdf._sdf.schema | ||
if len(pdf) <= limit: | ||
return kdf | ||
|
||
sdf = self._spark_group_map_apply( | ||
func, return_schema, retain_index=should_infer_schema) | ||
|
||
if should_infer_schema: | ||
# If schema is inferred, we can restore indexes too. | ||
internal = kdf._internal.copy(sdf=sdf) | ||
else: | ||
# Otherwise, it loses index. | ||
internal = _InternalFrame(sdf=sdf) | ||
return DataFrame(internal) | ||
|
||
# TODO: implement 'dropna' parameter | ||
def filter(self, func): | ||
|
@@ -843,24 +866,11 @@ def filter(self, func): | |
def pandas_filter(pdf): | ||
return pdf.groupby(groupby_names).filter(func) | ||
|
||
kdf = self._apply(pandas_filter, data_schema, retain_index=True) | ||
return DataFrame(self._kdf._internal.copy(sdf=kdf._sdf)) | ||
|
||
def _apply(self, func, return_schema, retain_index): | ||
should_infer_schema = return_schema is None | ||
input_groupnames = [s.name for s in self._groupkeys] | ||
|
||
if should_infer_schema: | ||
# Here we execute with the first 1000 to get the return type. | ||
# If the records were less than 1000, it uses pandas API directly for a shortcut. | ||
limit = 1000 | ||
pdf = self._kdf.head(limit + 1).to_pandas() | ||
pdf = pdf.groupby(input_groupnames).apply(func) | ||
kdf = DataFrame(pdf) | ||
return_schema = kdf._sdf.schema | ||
if len(pdf) <= limit: | ||
return kdf | ||
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. Mainly just a refactoring to disallow |
||
sdf = self._spark_group_map_apply( | ||
pandas_filter, data_schema, retain_index=True) | ||
return DataFrame(self._kdf._internal.copy(sdf=sdf)) | ||
|
||
def _spark_group_map_apply(self, func, return_schema, retain_index): | ||
index_columns = self._kdf._internal.index_columns | ||
index_names = self._kdf._internal.index_names | ||
data_columns = self._kdf._internal.data_columns | ||
|
@@ -934,14 +944,7 @@ def rename_output(pdf): | |
input_groupkeys = [s._scol for s in self._groupkeys] | ||
sdf = sdf.groupby(*input_groupkeys).apply(grouped_map_func) | ||
|
||
if should_infer_schema: | ||
# If schema is inferred, we can restore indexes too. | ||
internal = kdf._internal.copy(sdf=sdf) | ||
else: | ||
# Otherwise, it loses index. | ||
internal = _InternalFrame( | ||
sdf=sdf, data_columns=return_schema.fieldNames(), index_map=[]) | ||
return DataFrame(internal) | ||
return sdf | ||
|
||
def rank(self, method='average', ascending=True): | ||
""" | ||
|
@@ -1007,7 +1010,6 @@ def rank(self, method='average', ascending=True): | |
""" | ||
return self._rank(method, ascending) | ||
|
||
# TODO: Series support is not implemented yet. | ||
def transform(self, func): | ||
""" | ||
Apply function column-by-column to the GroupBy object. | ||
|
@@ -1117,7 +1119,9 @@ def pandas_transform(pdf): | |
pdf = pdf.drop(columns=input_groupnames) | ||
return pdf.transform(func) | ||
|
||
if return_sig is None: | ||
should_infer_schema = return_sig is None | ||
|
||
if should_infer_schema: | ||
# Here we execute with the first 1000 to get the return type. | ||
# If the records were less than 1000, it uses pandas API directly for a shortcut. | ||
limit = 1000 | ||
|
@@ -1128,16 +1132,22 @@ def pandas_transform(pdf): | |
if len(pdf) <= limit: | ||
return pdf | ||
|
||
applied_kdf = self._apply(pandas_transform, return_schema, retain_index=True) | ||
# kdf inferred from pdf holds a correct index. | ||
return DataFrame(kdf._internal.copy(sdf=applied_kdf._sdf)) | ||
sdf = self._spark_group_map_apply( | ||
pandas_transform, return_schema, retain_index=True) | ||
# If schema is inferred, we can restore indexes too. | ||
internal = kdf._internal.copy(sdf=sdf) | ||
else: | ||
return_type = _infer_return_type(func).tpe | ||
data_columns = self._kdf._internal.data_columns | ||
return_schema = StructType([ | ||
StructField(c, return_type) for c in data_columns if c not in input_groupnames]) | ||
|
||
return self._apply(pandas_transform, return_schema, retain_index=False) | ||
sdf = self._spark_group_map_apply( | ||
pandas_transform, return_schema, retain_index=False) | ||
# Otherwise, it loses index. | ||
internal = _InternalFrame(sdf=sdf) | ||
|
||
return DataFrame(internal) | ||
|
||
def nunique(self, dropna=True): | ||
""" | ||
|
@@ -1362,9 +1372,6 @@ def _cum(self, func): | |
elif func.__name__ == "cumprod": | ||
func = "cumprod" | ||
|
||
if len(self._kdf._internal.index_columns) == 0: | ||
raise ValueError("Index must be set.") | ||
|
||
applied = [] | ||
kdf = self._kdf | ||
groupkey_columns = set(s.name for s in self._groupkeys) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
These codes are needed because previously it removed the index when
reset_index
. Now it sets the default index.