-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
Add doc for counting categorical dtype #59327
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 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e4be568
Add doc for counting categorical dtype
jahn96 1348bdc
Move example to docstring instead
jahn96 d40849b
Remove backslash
jahn96 bc2378c
add line
jahn96 8497da5
Merge branch 'main' into categorical
jahn96 d838181
Undo the change in categorical.rst
jahn96 86facac
Rename it to Categorical Dtypes
jahn96 a847c13
undo categorical.rst
jahn96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -240,6 +240,8 @@ expects a ``dtype``. For example :func:`pandas.read_csv`, | |
array. In other words, ``dtype='category'`` is equivalent to | ||
``dtype=CategoricalDtype()``. | ||
|
||
.. _categorical.equalitysemantics: | ||
|
||
Equality semantics | ||
~~~~~~~~~~~~~~~~~~ | ||
|
||
|
@@ -1178,3 +1180,17 @@ Use ``copy=True`` to prevent such a behaviour or simply don't reuse ``Categorica | |
This also happens in some cases when you supply a NumPy array instead of a ``Categorical``: | ||
using an int array (e.g. ``np.array([1,2,3,4])``) will exhibit the same behavior, while using | ||
a string array (e.g. ``np.array(["a","b","c","a"])``) will not. | ||
|
||
Counting CategoricalDtype | ||
~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
As mentioned in :ref:`Equality Semantics <categorical.equalitysemantics>`, two instances of :class:`~pandas.api.types.CategoricalDtype` compare equal | ||
whenever they have the same categories and order. Therefore, when counting data types, the multiple instances of :class:`~pandas.api.types.CategoricalDtype` will be counted as one group if they have the same categories and order. | ||
In the example below, even though ``a``, ``c``, and ``d`` all have data types of ``category``, they will not be counted as one group since they don't have the same categories. | ||
|
||
.. ipython:: python | ||
|
||
df = pd.DataFrame({'a': [1], 'b': ['2'], 'c': [3], 'd': [3]}).astype({'a': 'category', 'c': 'category', 'd': 'category'}) | ||
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. Can you put this in the docstring instead? 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. addressed here |
||
df | ||
df.dtypes | ||
df.dtypes.value_counts() |
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.
Could you undo the changes in this file?
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.
Addressed here
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.
Looks like there's still changes in this file
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.
Ah sorry! Addressed here