Skip to content
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

Select the series correctly in SeriesGroupBy APIs #1224

Merged
merged 3 commits into from
Jan 27, 2020

Conversation

HyukjinKwon
Copy link
Member

Can be tested with the codes below:

import numpy as np
import pandas as pd
import databricks.koalas as ks

kdf = ks.DataFrame({'cust_id':['a', 'a', 'a', 'b', 'b'],
                   'sales': [100, 200, 300, 400, 500],
                   'days':[12.1,13.1,14.1,78.1,87.2]})

kdf.groupby('cust_id')['days'].apply(lambda x: x.ewm(alpha=0.5, adjust=False).mean())


pdf = kdf.to_pandas()
pdf.groupby('cust_id')['days'].apply(lambda x: x.ewm(alpha=0.5, adjust=False).mean())
0    12.10
1    12.60
2    13.35
3    78.10
4    82.65
Name: days, dtype: float64

Basic idea is that, it creates a DataFrame from Series to reuse existing implementations, and resolves the columns by name.


@property
def _kdf(self) -> DataFrame:
return self._kser._kdf
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is here.

@@ -2016,6 +2012,15 @@ class SeriesGroupBy(GroupBy):
def __init__(self, kser: Series, by: List[Series], as_index: bool = True):
self._kser = kser
self._groupkeys = by
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here.

@codecov-io
Copy link

codecov-io commented Jan 24, 2020

Codecov Report

Merging #1224 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master   #1224      +/-   ##
=========================================
+ Coverage   95.19%   95.2%   +<.01%     
=========================================
  Files          35      35              
  Lines        7263    7271       +8     
=========================================
+ Hits         6914    6922       +8     
  Misses        349     349
Impacted Files Coverage Δ
databricks/koalas/groupby.py 91.76% <100%> (-0.12%) ⬇️
databricks/koalas/frame.py 96.96% <100%> (ø) ⬆️
databricks/koalas/indexes.py 95.9% <0%> (-0.11%) ⬇️
databricks/koalas/series.py 96.46% <0%> (ø) ⬆️
databricks/koalas/indexing.py 96.32% <0%> (+0.01%) ⬆️
databricks/koalas/internal.py 95.57% <0%> (+0.36%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ff5ec10...0c4f087. Read the comment docs.

Copy link
Collaborator

@ueshin ueshin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that we should refactor these classes since the base infrastructure is rather old. We should revisit and refine later.

Comment on lines +2022 to +2023
self._groupkeys_scols = [F.col(name_like_string(s.name)) for s in self._groupkeys]
self._agg_columns_scols = [F.col(name_like_string(s.name)) for s in self._agg_columns]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F.col(s._internal.data_columns[0]) instead of F.col(name_like_string(s.name))?

Copy link
Member Author

@HyukjinKwon HyukjinKwon Jan 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched but realised that some tests fail with that change. Seems like there's some cases when internal column names and series names are different:

>>> (ks.range(10).id + 1)._internal.data_columns
['(id + 1)']
>>> (ks.range(10).id + 1).name
'id'

We use:

    @property
    def _kdf(self) -> DataFrame:
        series = [self._kser] + [s for s in self._groupkeys if not s._equals(self._kser)]
        return DataFrame(self._kser._kdf._internal.with_new_columns(series))

which always aliases via using column_index at with_new_columns. So, it seems guaranteed to have the internal Spark column names same as s.name. I think using name here for this workaround is correct.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see. hm, maybe renaming the columns in with_new_columns was not a good idea. I'll fix it later.

return self._kser._kdf
# TODO: if names from _kser and _groupkeys are name, grouping key is just ignored.
# it can be a problem when both series have the same name but different operations.
series = [self._kser] + [s for s in self._groupkeys if s.name != self._kser.name]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using s._equals(self._kser) which compares the ._scol._jcs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, seems it fails because the same names exist in the same DataFrame AnalysisException: Reference 'b' is ambiguous, could be: b, b.; ... but yeah I think it's better to fail fast for now rather than returning a wrong result.

self.assert_eq(kdf.groupby(['b'])['a'].apply(lambda x: x).sort_index(),
pdf.groupby(['b'])['a'].apply(lambda x: x).sort_index())
self.assert_eq(kdf.groupby(['b'])['b'].apply(lambda x: x).sort_index(),
pdf.groupby(['b'])['b'].apply(lambda x: x).sort_index())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, we will not support this case for now, which I think it's fine (?).

Copy link
Collaborator

@ueshin ueshin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Comment on lines +2022 to +2023
self._groupkeys_scols = [F.col(name_like_string(s.name)) for s in self._groupkeys]
self._agg_columns_scols = [F.col(name_like_string(s.name)) for s in self._agg_columns]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see. hm, maybe renaming the columns in with_new_columns was not a good idea. I'll fix it later.

@ueshin
Copy link
Collaborator

ueshin commented Jan 27, 2020

Thanks! merging.

@sushmit86
Copy link

Is this issue fixed?

@HyukjinKwon
Copy link
Member Author

Yup, this will be available for the next week's release.

@HyukjinKwon HyukjinKwon deleted the groupby-series branch September 11, 2020 07:52
rising-star92 added a commit to rising-star92/databricks-koalas that referenced this pull request Jan 27, 2023
…different values (#1233)

A small followup of databricks/koalas#1224 and databricks/koalas#1229

Now, we can cover this case
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants