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

Implement DataFrame/Series rename_axis #1843

Merged
merged 11 commits into from
Oct 20, 2020

Conversation

LucasG0
Copy link
Contributor

@LucasG0 LucasG0 commented Oct 12, 2020

Hi, this PR implements DataFrame.rename_axis and Series.rename_axis.
I did not add copy parameter as a copy is performed anyway.

@HyukjinKwon
Copy link
Member

cc @itholic can you review this please?

@itholic
Copy link
Contributor

itholic commented Oct 15, 2020

Sure, let me take a look sooner or later :)

Copy link
Contributor

@itholic itholic left a comment

Choose a reason for hiding this comment

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

Basically looks fine to me, but let me check this once again on this weekend .

databricks/koalas/frame.py Show resolved Hide resolved
databricks/koalas/frame.py Show resolved Hide resolved
databricks/koalas/frame.py Outdated Show resolved Hide resolved
databricks/koalas/frame.py Outdated Show resolved Hide resolved
databricks/koalas/frame.py Outdated Show resolved Hide resolved
databricks/koalas/frame.py Outdated Show resolved Hide resolved
databricks/koalas/series.py Outdated Show resolved Hide resolved
Copy link
Contributor

@itholic itholic left a comment

Choose a reason for hiding this comment

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

Otherwise, Seems fine to me.

Comment on lines +9139 to +9140
A scalar, list-like, dict-like or functions transformations to
apply to the axis name attribute.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this explanation for mapper is not correct?

In the pandas latest docs, they say:

Value to set the axis name attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I should have precised that pandas does not support using a dict or a function as mapper. They say :
However, if mapper is dict-like or a function, it will use the deprecated behavior of modifying the axis labels.
So, I am not sure this is the correct behavior, that is why I added the possibility to use dict / function as mapper, and therefore updated the docs.
I am interested in your opinion on this !

See Also
--------
DataFrame.rename : Alter DataFrame index labels or name.
Index.rename : Set new names on index.
Copy link
Contributor

@itholic itholic Oct 18, 2020

Choose a reason for hiding this comment

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

Maybe we can also have Series.rename ?

Copy link
Contributor Author

@LucasG0 LucasG0 Oct 18, 2020

Choose a reason for hiding this comment

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

Yes I did not put them as I split Series/DataFrame docs unlike pandas, but I will add them back. :)
Maybe we could also refer to Series.rename_axis or instead ?

Comment on lines +1126 to +1128
mapper, index : scalar, list-like, dict-like or function, optional
A scalar, list-like, dict-like or functions transformations to
apply to the index values.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this it's also not correct? I think you can refer to here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is for the same reason I explained above.

See Also
--------
Series.rename : Alter Series index labels or name.
Index.rename : Set new names on index.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can also have DataFrame.rename here ?

monkey 2
Name: num_legs, dtype: int64
"""
return first_series(self.to_frame().rename_axis(mapper=mapper, index=index))
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe inplace=inplace is missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes indeed !

self.assertRaises(ValueError, lambda: kdf.rename_axis(["cols2", "cols3"], axis=1))

# index/columns parameters and dict_like/functions mappers introduced in pandas 0.24.0
if LooseVersion(pd.__version__) >= LooseVersion("0.24.0"):
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you make tests for pandas < 0.24.0 by manually declaring the expected result like we did before ??

)

# index/columns parameters and dict_like/functions mappers introduced in pandas 0.24.0
if LooseVersion(pd.__version__) >= LooseVersion("0.24.0"):
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto ?

Comment on lines 602 to 609
self.assert_eq(
pdf.rename_axis(["index2"]).sort_index(), kdf.rename_axis(["index2"]).sort_index(),
)

self.assert_eq(
pdf.rename_axis(["index2"], axis=1).sort_index(),
kdf.rename_axis(["index2"], axis=1).sort_index(),
)
Copy link
Contributor

@itholic itholic Oct 18, 2020

Choose a reason for hiding this comment

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

Why don't we just add these tests to the above??

For example,

        for axis in [0, "index"]:
            self.assert_eq(
                pdf.rename_axis("index2", axis=axis).sort_index(),
                kdf.rename_axis("index2", axis=axis).sort_index(),
            )
            self.assert_eq(
                pdf.rename_axis(["index2"], axis=axis).sort_index(),
                kdf.rename_axis(["index2"], axis=axis).sort_index(),
            )

        for axis in [1, "columns"]:
            self.assert_eq(
                pdf.rename_axis("cols2", axis=axis).sort_index(),
                kdf.rename_axis("cols2", axis=axis).sort_index(),
            )
            self.assert_eq(
                pdf.rename_axis(["cols2"], axis=axis).sort_index(),
                kdf.rename_axis(["cols2"], axis=axis).sort_index(),
            )

Comment on lines 9281 to 9290
spark_frame = self._internal.resolved_copy.spark_frame
internal = InternalFrame(
spark_frame=spark_frame,
index_map=index_map,
column_labels=self._internal.column_labels,
data_spark_columns=[
scol_for(spark_frame, col) for col in self._internal.data_spark_column_names
],
column_label_names=column_label_names,
)
Copy link
Contributor

@itholic itholic Oct 18, 2020

Choose a reason for hiding this comment

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

Maybe I think we could just simply update the index_map and column_label_names using self._internal.copy rather than create the new InternalFrame here ??

Because this API updates only the name of index or columns.

        internal = self._internal.copy(
            index_map=index_map,
            column_label_names=column_label_names,
        )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely !

@LucasG0
Copy link
Contributor Author

LucasG0 commented Oct 18, 2020

By the way, I actually do not think anymore that a copy of the underlying data is performed in this implementation. However, I am not sure copying the immutable spark_frame is relevant here, so I wonder if we should keep copy parameter.

@codecov-io
Copy link

codecov-io commented Oct 18, 2020

Codecov Report

Merging #1843 into master will decrease coverage by 0.01%.
The diff coverage is 97.22%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1843      +/-   ##
==========================================
- Coverage   94.16%   94.15%   -0.02%     
==========================================
  Files          40       40              
  Lines        9725     9772      +47     
==========================================
+ Hits         9158     9201      +43     
- Misses        567      571       +4     
Impacted Files Coverage Δ
databricks/koalas/missing/frame.py 100.00% <ø> (ø)
databricks/koalas/missing/series.py 100.00% <ø> (ø)
databricks/koalas/frame.py 96.76% <96.66%> (-0.01%) ⬇️
databricks/koalas/series.py 96.86% <100.00%> (+0.01%) ⬆️
databricks/koalas/generic.py 95.35% <0.00%> (-0.29%) ⬇️
databricks/koalas/plot.py 91.92% <0.00%> (-0.24%) ⬇️
databricks/koalas/indexing.py 92.73% <0.00%> (ø)
databricks/koalas/internal.py 96.44% <0.00%> (ø)
... and 1 more

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 d01d9a7...edcef50. 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.

Otherwise, LGTM.

databricks/koalas/frame.py Outdated Show resolved Hide resolved
@HyukjinKwon HyukjinKwon merged commit 0036768 into databricks:master Oct 20, 2020
@LucasG0 LucasG0 deleted the rename_axis branch October 20, 2020 11:03
@LucasG0
Copy link
Contributor Author

LucasG0 commented Oct 20, 2020

Thanks !

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.

5 participants