-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: support CategoricalIndex (GH7629) #9741
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -7,6 +7,10 @@ This is a minor bug-fix release from 0.16.0 and includes a a large number of | |
bug fixes along several new features, enhancements, and performance improvements. | ||
We recommend that all users upgrade to this version. | ||
|
||
Highlights include: | ||
|
||
- Support for a ``CategoricalIndex``, a category based index, see :ref:`here <whatsnew_0161`.enhancements.categoricalindex>` | ||
|
||
.. contents:: What's new in v0.16.1 | ||
:local: | ||
:backlinks: none | ||
|
@@ -31,6 +35,7 @@ Enhancements | |
will return a `np.array` instead of a boolean `Index` (:issue:`8875`). This enables the following expression | ||
to work naturally: | ||
|
||
|
||
.. ipython:: python | ||
|
||
idx = Index(['a1', 'a2', 'b1', 'b2']) | ||
|
@@ -40,6 +45,7 @@ Enhancements | |
s[s.index.str.startswith('a')] | ||
|
||
- ``DataFrame.mask()`` and ``Series.mask()`` now support same keywords as ``where`` (:issue:`8801`) | ||
|
||
- ``drop`` function can now accept ``errors`` keyword to suppress ValueError raised when any of label does not exist in the target data. (:issue:`6736`) | ||
|
||
.. ipython:: python | ||
|
@@ -58,6 +64,75 @@ Enhancements | |
|
||
- ``DataFrame`` and ``Series`` now have ``_constructor_expanddim`` property as overridable constructor for one higher dimensionality data. This should be used only when it is really needed, see :ref:`here <ref-subclassing-pandas>` | ||
|
||
.. _whatsnew_0161.enhancements.categoricalindex: | ||
|
||
CategoricalIndex | ||
^^^^^^^^^^^^^^^^ | ||
|
||
We introduce a ``CategoricalIndex``, a new type of index object that is useful for supporting | ||
indexing with duplicates. This is a container around a ``Categorical`` (introduced in v0.15.0) | ||
and allows efficient indexing and storage of an index with a large number of duplicated elements. Prior to 0.16.1, | ||
setting the index of a ``DataFrame/Series`` with a ``category`` dtype would convert this to regular object-based ``Index``. | ||
|
||
.. ipython :: python | ||
|
||
df = DataFrame({'A' : np.arange(6), | ||
'B' : Series(list('aabbca')).astype('category', | ||
categories=list('cab')) | ||
}) | ||
df | ||
df.dtypes | ||
df.B.cat.categories | ||
|
||
setting the index, will create create a CategoricalIndex | ||
|
||
.. ipython :: python | ||
|
||
df2 = df.set_index('B') | ||
df2.index | ||
|
||
indexing with ``__getitem__/.iloc/.loc/.ix`` works similarly to an Index with duplicates. | ||
The indexers MUST be in the category or the operation will raise. | ||
|
||
.. ipython :: python | ||
|
||
df2.loc['a'] | ||
|
||
and preserves the ``CategoricalIndex`` | ||
|
||
.. ipython :: python | ||
|
||
df2.loc['a'].index | ||
|
||
sorting will order by the order of the categories | ||
|
||
.. ipython :: python | ||
|
||
df2.sort_index() | ||
|
||
groupby operations on the index will preserve the index nature as well | ||
|
||
.. ipython :: python | ||
|
||
df2.groupby(level=0).sum() | ||
df2.groupby(level=0).sum().index | ||
|
||
reindexing operations, will return a resulting index based on the type of the passed | ||
indexer, meaning that passing a list will return a plain-old-``Index``; indexing with | ||
a ``Categorical`` will return a ``CategoricalIndex``, indexed according to the categories | ||
of the PASSED ``Categorical`` dtype. This allows one to arbitrarly index these even with | ||
values NOT in the categories, similarly to how you can reindex ANY pandas index. | ||
|
||
.. ipython :: python | ||
|
||
df2.reindex(['a','e']) | ||
df2.reindex(['a','e']).index | ||
df2.reindex(pd.Categorical(['a','e'],categories=list('abcde'))) | ||
df2.reindex(pd.Categorical(['a','e'],categories=list('abcde'))).index | ||
|
||
See the :ref:`documentation <advanced.categoricalindex>` for more. (:issue:`7629`) | ||
>>>>>>> support CategoricalIndex | ||
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. small leftover from rebasing 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. fixed |
||
|
||
.. _whatsnew_0161.api: | ||
|
||
API changes | ||
|
This file contains hidden or 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 hidden or 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
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.
small issue: you removed the label of the "Float64Index" section below this
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.
fixed