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

ENH: Let Categorical.rename_categories take a callable #18862

Merged
merged 2 commits into from Dec 21, 2017

Conversation

Projects
None yet
3 participants
@topper-123
Contributor

topper-123 commented Dec 20, 2017

  • tests added / passed
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

This PR allows Categorical.rename_categories to take a callable as its argument.

This is useful for quickly changing the categories the same way for all categories, e.g.

>>> pd.Categorical(['a', 'b']).rename_categories("cat_{}".format)
[cat_a, cat_b]
Categories (2, object): [cat_a, cat_b]
@jreback

lgtm. minor changes.

@@ -139,6 +139,7 @@ Other Enhancements
- :func:`read_excel()` has gained the ``nrows`` parameter (:issue:`16645`)
- :func:``DataFrame.to_json`` and ``Series.to_json`` now accept an ``index`` argument which allows the user to exclude the index from the JSON output (:issue:`17394`)
- ``IntervalIndex.to_tuples()`` has gained the ``na_tuple`` parameter to control whether NA is returned as a tuple of NA, or NA itself (:issue:`18756`)
- ``Categorical.rename_categories`` can how take a callable as its argument (:issue:`18862`)

This comment has been minimized.

@jreback

jreback Dec 20, 2017

Contributor

use :func:`Categorical.rename_categories` here

This comment has been minimized.

@TomAugspurger

TomAugspurger Dec 20, 2017

Contributor

how -> now

This comment has been minimized.

@TomAugspurger

TomAugspurger Dec 20, 2017

Contributor

Also, we don't have Categorical in the API docs yet, so that won't work.

:meth:`Series.cat.rename_categories`

may work (though it applies to CategoricalIndex too).

This comment has been minimized.

@jreback

jreback Dec 20, 2017

Contributor

actually let's also test this for a CI and a Series.cat.rename_categories

This comment has been minimized.

@topper-123

topper-123 Dec 20, 2017

Contributor

Not sure I understand this about CI. I assume that will be a seperate PR? else I'd appreciate guidance.

Do you mean test Series.cat.rename_categories? Isn't it a bit odd as that's a delegation from the Categorical namespace (not at the coding computer ATM, but that's how I understand it)?

This comment has been minimized.

@jreback

jreback Dec 20, 2017

Contributor

CI = CategoricalIndex, sure its separate but makes sense to do it here (it should just work). Series.cat....IS a delegation, but we do test things there as well (for example to make sure things are passed thru). See what we have for testing and add as appropriate (or indicate that we are not testing except for basic functionaility).

This comment has been minimized.

@topper-123

topper-123 Dec 20, 2017

Contributor

Ok, I understood CI as Continuous Integration. Sure, I can take a look at adding a test for CategoricalIndex,

@@ -854,6 +854,10 @@ def rename_categories(self, new_categories, inplace=False):
are passed through and extra categories in the mapping are
ignored. *New in version 0.21.0*.
* callable : a callable that is called on all items in the old
categories and whose return values comprise the new categories.
*New in version 0.22.0*.

This comment has been minimized.

@jreback

jreback Dec 20, 2017

Contributor

use a versionadded tag

This comment has been minimized.

@jreback

jreback Dec 20, 2017

Contributor

and if you'd fix the prior one as well

@@ -71,9 +71,14 @@ def test_rename_categories(self):
exp_cat = Index(["a", "b", "c"])
tm.assert_index_equal(cat.categories, exp_cat)
res = cat.rename_categories([1, 2, 3], inplace=True)
# GH18862

This comment has been minimized.

@jreback

jreback Dec 20, 2017

Contributor

add a comment (callable)

@jreback jreback added this to the 0.22.0 milestone Dec 20, 2017

@topper-123

This comment has been minimized.

Contributor

topper-123 commented Dec 21, 2017

Updated and all green.

@topper-123

This comment has been minimized.

Contributor

topper-123 commented Dec 21, 2017

BTW, on Github, in the timeline this is added to the 0.22 milestone, but on the right side overview, it says milestone 0.23.

Seems to be something strange going on?

Spelling error
corrected a minor spelling error

@jreback jreback merged commit c98e3ef into pandas-dev:master Dec 21, 2017

3 checks passed

ci/circleci Your tests passed on CircleCI!
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@jreback

This comment has been minimized.

Contributor

jreback commented Dec 21, 2017

thanks @topper-123

we are going to release 0.22 shortly with #18876 only, everything else gets moved to 0.23.

@topper-123 topper-123 deleted the topper-123:rename_categories_callable branch Dec 26, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment