-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
DOC: EX01 ({Categorical, Interval, Multi, Datetime, Timedelta}-Index) #53925
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
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9bc9f37
add examples for CategoricalIndex.equals
Charlie-XIAO 1abc04a
add examples for MultiIndex.dtypes
Charlie-XIAO 53399f8
add examples for MultiIndex.drop
Charlie-XIAO 81b6d15
add examples for DatetimeIndex.to_pydatetime/std
Charlie-XIAO 5d9c6d4
add examples for TimedeltaIndex
Charlie-XIAO 2dd05ab
minor modifications
Charlie-XIAO 00ff128
Merge branch 'main' into doc-ex01-2
Charlie-XIAO b495dc7
Merge branch 'main' into doc-ex01-2
Charlie-XIAO a178262
Merge branch 'main' into doc-ex01-2
Charlie-XIAO 7c91789
fix docstrings and make up for removed ones
Charlie-XIAO 3324b17
fix doctests
Charlie-XIAO 758fc0a
Merge branch 'main' into doc-ex01-2
Charlie-XIAO d680568
Merge branch 'doc-ex01-2' of https://github.com/Charlie-XIAO/pandas i…
Charlie-XIAO c35aeba
Merge branch 'main' into doc-ex01-2
Charlie-XIAO 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -272,7 +272,7 @@ class Categorical(NDArrayBackedExtensionArray, PandasObject, ObjectStringArrayMi | |
Attributes | ||
---------- | ||
categories : Index | ||
The categories of this categorical | ||
The categories of this categorical. | ||
codes : ndarray | ||
The codes (integer positions, which point to the categories) of this | ||
categorical, read only. | ||
|
@@ -760,23 +760,32 @@ def categories(self) -> Index: | |
|
||
Examples | ||
-------- | ||
For :class:`pandas.Series`: | ||
|
||
For Series: | ||
|
||
>>> ser = pd.Series(["a", "b", "c", "a"], dtype="category") | ||
>>> ser = pd.Series(['a', 'b', 'c', 'a'], dtype='category') | ||
>>> ser.cat.categories | ||
Index(['a', 'b', 'c'], dtype='object') | ||
|
||
>>> raw_cat = pd.Categorical(["a", "b", "c", "a"], categories=["b", "c", "d"],) | ||
>>> raw_cat = pd.Categorical(['a', 'b', 'c', 'a'], categories=['b', 'c', 'd']) | ||
Comment on lines
-770
to
+769
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. just for the future - in general, let's not bother with stylistic changes like changing double quotes to single quotes, at least until we have an automated tool to enforce this on docstrings |
||
>>> ser = pd.Series(raw_cat) | ||
>>> ser.cat.categories | ||
Index(['b', 'c', 'd'], dtype='object') | ||
|
||
For Categorical: | ||
For :class:`pandas.Categorical`: | ||
|
||
>>> cat = pd.Categorical(['a', 'b'], ordered=True) | ||
>>> cat.categories | ||
Index(['a', 'b'], dtype='object') | ||
|
||
For :class:`pandas.CategoricalIndex`: | ||
|
||
>>> ci = pd.CategoricalIndex(['a', 'c', 'b', 'a', 'c', 'b']) | ||
>>> ci.categories | ||
Index(['a', 'b', 'c'], dtype='object') | ||
|
||
>>> ci = pd.CategoricalIndex(['a', 'c'], categories=['c', 'b', 'a']) | ||
>>> ci.categories | ||
Index(['c', 'b', 'a'], dtype='object') | ||
""" | ||
return self.dtype.categories | ||
|
||
|
@@ -787,19 +796,18 @@ def ordered(self) -> Ordered: | |
|
||
Examples | ||
-------- | ||
For :class:`pandas.Series`: | ||
|
||
For Series: | ||
|
||
>>> ser = pd.Series(["a", "b", "c", "a"], dtype="category") | ||
>>> ser = pd.Series(['a', 'b', 'c', 'a'], dtype='category') | ||
>>> ser.cat.ordered | ||
False | ||
|
||
>>> raw_cat = pd.Categorical(["a", "b", "c", "a"], ordered=True) | ||
>>> raw_cat = pd.Categorical(['a', 'b', 'c', 'a'], ordered=True) | ||
>>> ser = pd.Series(raw_cat) | ||
>>> ser.cat.ordered | ||
True | ||
|
||
For Categorical: | ||
For :class:`pandas.Categorical`: | ||
|
||
>>> cat = pd.Categorical(['a', 'b'], ordered=True) | ||
>>> cat.ordered | ||
|
@@ -808,13 +816,23 @@ def ordered(self) -> Ordered: | |
>>> cat = pd.Categorical(['a', 'b'], ordered=False) | ||
>>> cat.ordered | ||
False | ||
|
||
For :class:`pandas.CategoricalIndex`: | ||
|
||
>>> ci = pd.CategoricalIndex(['a', 'b'], ordered=True) | ||
>>> ci.ordered | ||
True | ||
|
||
>>> ci = pd.CategoricalIndex(['a', 'b'], ordered=False) | ||
>>> ci.ordered | ||
False | ||
""" | ||
return self.dtype.ordered | ||
|
||
@property | ||
def codes(self) -> np.ndarray: | ||
""" | ||
The category codes of this categorical. | ||
The category codes of this categorical index. | ||
|
||
Codes are an array of integers which are the positions of the actual | ||
values in the categories array. | ||
|
@@ -825,13 +843,25 @@ def codes(self) -> np.ndarray: | |
Returns | ||
------- | ||
ndarray[int] | ||
A non-writable view of the `codes` array. | ||
A non-writable view of the ``codes`` array. | ||
|
||
Examples | ||
-------- | ||
For :class:`pandas.Categorical`: | ||
|
||
>>> cat = pd.Categorical(['a', 'b'], ordered=True) | ||
>>> cat.codes | ||
array([0, 1], dtype=int8) | ||
|
||
For :class:`pandas.CategoricalIndex`: | ||
|
||
>>> ci = pd.CategoricalIndex(['a', 'b', 'c', 'a', 'b', 'c']) | ||
>>> ci.codes | ||
array([0, 1, 2, 0, 1, 2], dtype=int8) | ||
|
||
>>> ci = pd.CategoricalIndex(['a', 'c'], categories=['c', 'b', 'a']) | ||
>>> ci.codes | ||
array([2, 0], dtype=int8) | ||
""" | ||
v = self._codes.view() | ||
v.flags.writeable = False | ||
|
@@ -915,12 +945,23 @@ def as_ordered(self) -> Self: | |
|
||
Examples | ||
-------- | ||
>>> ser = pd.Series(["a", "b", "c", "a"], dtype="category") | ||
For :class:`pandas.Series`: | ||
|
||
>>> ser = pd.Series(['a', 'b', 'c', 'a'], dtype='category') | ||
>>> ser.cat.ordered | ||
False | ||
>>> ser = ser.cat.as_ordered() | ||
>>> ser.cat.ordered | ||
True | ||
|
||
For :class:`pandas.CategoricalIndex`: | ||
|
||
>>> ci = pd.CategoricalIndex(['a', 'b', 'c', 'a']) | ||
>>> ci.ordered | ||
False | ||
>>> ci = ci.as_ordered() | ||
>>> ci.ordered | ||
True | ||
""" | ||
return self.set_ordered(True) | ||
|
||
|
@@ -935,24 +976,36 @@ def as_unordered(self) -> Self: | |
|
||
Examples | ||
-------- | ||
>>> raw_cate = pd.Categorical(["a", "b", "c"], | ||
... categories=["a", "b", "c"], ordered=True) | ||
>>> ser = pd.Series(raw_cate) | ||
For :class:`pandas.Series`: | ||
|
||
>>> raw_cat = pd.Categorical(['a', 'b', 'c', 'a'], ordered=True) | ||
>>> ser = pd.Series(raw_cat) | ||
>>> ser.cat.ordered | ||
True | ||
>>> ser = ser.cat.as_unordered() | ||
>>> ser.cat.ordered | ||
False | ||
|
||
For :class:`pandas.CategoricalIndex`: | ||
|
||
>>> ci = pd.CategoricalIndex(['a', 'b', 'c', 'a'], ordered=True) | ||
>>> ci.ordered | ||
True | ||
>>> ci = ci.as_unordered() | ||
>>> ci.ordered | ||
False | ||
""" | ||
return self.set_ordered(False) | ||
|
||
def set_categories(self, new_categories, ordered=None, rename: bool = False): | ||
""" | ||
Set the categories to the specified new_categories. | ||
Set the categories to the specified new categories. | ||
|
||
`new_categories` can include new categories (which will result in | ||
``new_categories`` can include new categories (which will result in | ||
unused categories) or remove old categories (which results in values | ||
set to NaN). If `rename==True`, the categories will simple be renamed | ||
set to ``NaN``). If ``rename=True``, the categories will simply be renamed | ||
(less or more items than in old categories will result in values set to | ||
NaN or in unused categories respectively). | ||
``NaN`` or in unused categories respectively). | ||
|
||
This method can be used to perform more than one action of adding, | ||
removing, and reordering simultaneously and is therefore faster than | ||
|
@@ -994,23 +1047,41 @@ def set_categories(self, new_categories, ordered=None, rename: bool = False): | |
|
||
Examples | ||
-------- | ||
>>> raw_cate = pd.Categorical(["a", "b", "c", "A"], | ||
... categories=["a", "b", "c"], ordered=True) | ||
>>> ser = pd.Series(raw_cate) | ||
For :class:`pandas.Series`: | ||
|
||
>>> raw_cat = pd.Categorical(['a', 'b', 'c', 'A'], | ||
... categories=['a', 'b', 'c'], ordered=True) | ||
>>> ser = pd.Series(raw_cat) | ||
>>> ser | ||
0 a | ||
1 b | ||
2 c | ||
3 NaN | ||
dtype: category | ||
Categories (3, object): ['a' < 'b' < 'c'] | ||
>>> ser.cat.set_categories(["A", "B", "C"], rename=True) | ||
|
||
>>> ser.cat.set_categories(['A', 'B', 'C'], rename=True) | ||
0 A | ||
1 B | ||
2 C | ||
3 NaN | ||
dtype: category | ||
Categories (3, object): ['A' < 'B' < 'C'] | ||
|
||
For :class:`pandas.CategoricalIndex`: | ||
|
||
>>> ci = pd.CategoricalIndex(['a', 'b', 'c', 'A'], | ||
... categories=['a', 'b', 'c'], ordered=True) | ||
>>> ci | ||
CategoricalIndex(['a', 'b', 'c', nan], categories=['a', 'b', 'c'], | ||
ordered=True, dtype='category') | ||
|
||
>>> ci.set_categories(['A', 'b', 'c']) | ||
CategoricalIndex([nan, 'b', 'c', nan], categories=['A', 'b', 'c'], | ||
ordered=True, dtype='category') | ||
>>> ci.set_categories(['A', 'b', 'c'], rename=True) | ||
CategoricalIndex(['A', 'b', 'c', nan], categories=['A', 'b', 'c'], | ||
ordered=True, dtype='category') | ||
""" | ||
|
||
if ordered is None: | ||
|
@@ -1108,7 +1179,7 @@ def reorder_categories(self, new_categories, ordered=None) -> Self: | |
""" | ||
Reorder categories as specified in new_categories. | ||
|
||
`new_categories` need to include all old categories and no new category | ||
``new_categories`` need to include all old categories and no new category | ||
items. | ||
|
||
Parameters | ||
|
@@ -1140,7 +1211,9 @@ def reorder_categories(self, new_categories, ordered=None) -> Self: | |
|
||
Examples | ||
-------- | ||
>>> ser = pd.Series(["a", "b", "c", "a"], dtype="category") | ||
For :class:`pandas.Series`: | ||
|
||
>>> ser = pd.Series(['a', 'b', 'c', 'a'], dtype='category') | ||
>>> ser = ser.cat.reorder_categories(['c', 'b', 'a'], ordered=True) | ||
>>> ser | ||
0 a | ||
|
@@ -1149,14 +1222,24 @@ def reorder_categories(self, new_categories, ordered=None) -> Self: | |
3 a | ||
dtype: category | ||
Categories (3, object): ['c' < 'b' < 'a'] | ||
>>> ser = ser.sort_values() | ||
>>> ser | ||
|
||
>>> ser.sort_values() | ||
2 c | ||
1 b | ||
0 a | ||
3 a | ||
dtype: category | ||
Categories (3, object): ['c' < 'b' < 'a'] | ||
|
||
For :class:`pandas.CategoricalIndex`: | ||
|
||
>>> ci = pd.CategoricalIndex(['a', 'b', 'c', 'a']) | ||
>>> ci | ||
CategoricalIndex(['a', 'b', 'c', 'a'], categories=['a', 'b', 'c'], | ||
ordered=False, dtype='category') | ||
>>> ci.reorder_categories(['c', 'b', 'a'], ordered=True) | ||
CategoricalIndex(['a', 'b', 'c', 'a'], categories=['c', 'b', 'a'], | ||
ordered=True, dtype='category') | ||
""" | ||
if ( | ||
len(self.categories) != len(new_categories) | ||
|
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.
nice!