-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
DOC: Fixed examples in pandas/core/arrays/ #33179
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
Changes from 2 commits
08322a5
1855f14
4c87ad5
435564e
3f03fde
3aeed89
b2c0dc7
250b267
a745584
a8cc997
5fbfd46
51b034e
b6bdad8
7f730ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1598,19 +1598,19 @@ def sort_values(self, inplace=False, ascending=True, na_position="last"): | |
|
||
>>> c = pd.Categorical([np.nan, 2, 2, np.nan, 5]) | ||
>>> c | ||
[NaN, 2.0, 2.0, NaN, 5.0] | ||
[NaN, 2, 2, NaN, 5] | ||
Categories (2, int64): [2, 5] | ||
>>> c.sort_values() | ||
[2.0, 2.0, 5.0, NaN, NaN] | ||
[2, 2, 5, NaN, NaN] | ||
Categories (2, int64): [2, 5] | ||
>>> c.sort_values(ascending=False) | ||
[5.0, 2.0, 2.0, NaN, NaN] | ||
[5, 2, 2, NaN, NaN] | ||
Categories (2, int64): [2, 5] | ||
>>> c.sort_values(na_position='first') | ||
[NaN, NaN, 2.0, 2.0, 5.0] | ||
[NaN, NaN, 2, 2, 5] | ||
Categories (2, int64): [2, 5] | ||
>>> c.sort_values(ascending=False, na_position='first') | ||
[NaN, NaN, 5.0, 2.0, 2.0] | ||
[NaN, NaN, 5, 2, 2] | ||
Categories (2, int64): [2, 5] | ||
""" | ||
inplace = validate_bool_kwarg(inplace, "inplace") | ||
|
@@ -1835,7 +1835,7 @@ def take(self, indexer, allow_fill: bool = False, fill_value=None): | |
|
||
>>> cat.take([0, -1, -1], allow_fill=True, fill_value='a') | ||
[a, a, a] | ||
Categories (3, object): [a, b] | ||
Categories (2, object): [a, b] | ||
|
||
Specifying a fill value that's not in ``self.categories`` | ||
will raise a ``TypeError``. | ||
|
@@ -2237,21 +2237,20 @@ def unique(self): | |
order of appearance. | ||
|
||
>>> pd.Categorical(list('baabc')) | ||
[b, a, c] | ||
Categories (3, object): [b, a, c] | ||
[b, a, a, b, c] | ||
Categories (3, object): [a, b, c] | ||
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. I think there are missing 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. Good catch! I got into a tunnel vision of just correcting the output, I guess I didn't even looked at what is the docstring I'm editing |
||
|
||
>>> pd.Categorical(list('baabc'), categories=list('abc')) | ||
[b, a, c] | ||
Categories (3, object): [b, a, c] | ||
[b, a, a, b, c] | ||
Categories (3, object): [a, b, c] | ||
|
||
An ordered Categorical preserves the category ordering. | ||
|
||
>>> pd.Categorical(list('baabc'), | ||
... categories=list('abc'), | ||
... ordered=True) | ||
[b, a, c] | ||
>>> pd.Categorical(list('baabc'), categories=list('abc'), ordered=True) | ||
[b, a, a, b, c] | ||
Categories (3, object): [a < b < c] | ||
|
||
|
||
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. do we need two lines here or just one? 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. I think one. Besides "See also" header, should come before the "Examples" header. Ill fix that |
||
See Also | ||
-------- | ||
unique | ||
|
@@ -2438,7 +2437,7 @@ def replace(self, to_replace, value, inplace: bool = False): | |
-------- | ||
>>> s = pd.Categorical([1, 2, 1, 3]) | ||
>>> s.replace(1, 3) | ||
[3, 3, 2, 3] | ||
[3, 2, 3, 3] | ||
Categories (2, int64): [2, 3] | ||
""" | ||
inplace = validate_bool_kwarg(inplace, "inplace") | ||
|
@@ -2506,16 +2505,90 @@ class CategoricalAccessor(PandasDelegate, PandasObject, NoNewAttributesMixin): | |
|
||
Examples | ||
-------- | ||
>>> s = pd.Series(list("aabc")).astype("category") | ||
>>> s | ||
0 a | ||
1 a | ||
2 b | ||
3 c | ||
dtype: category | ||
Categories (3, object): [a, b, c] | ||
|
||
>>> s.cat.categories | ||
>>> s.cat.categories = list('abc') | ||
>>> s.cat.rename_categories(list('cab')) | ||
>>> s.cat.reorder_categories(list('cab')) | ||
>>> s.cat.add_categories(['d','e']) | ||
Index(['a', 'b', 'c'], dtype='object') | ||
|
||
>>> s.cat.categories = list("bcd") | ||
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. I personally think we should remove this example ... (seems bad practice to me to modify the categorical like this in place) |
||
>>> s | ||
0 b | ||
1 b | ||
2 c | ||
3 d | ||
dtype: category | ||
Categories (3, object): [b, c, d] | ||
|
||
>>> s.cat.rename_categories(list("abc")) | ||
0 a | ||
1 a | ||
2 b | ||
3 c | ||
dtype: category | ||
Categories (3, object): [a, b, c] | ||
|
||
>>> s.cat.reorder_categories(list("cdb")) | ||
0 b | ||
1 b | ||
2 c | ||
3 d | ||
dtype: category | ||
Categories (3, object): [c, d, b] | ||
|
||
>>> s.cat.add_categories(["e", "f"]) | ||
0 b | ||
1 b | ||
2 c | ||
3 d | ||
dtype: category | ||
Categories (5, object): [b, c, d, e, f] | ||
|
||
>>> s.cat.remove_categories(['d']) | ||
0 b | ||
1 b | ||
2 c | ||
3 NaN | ||
dtype: category | ||
Categories (2, object): [b, c] | ||
|
||
>>> s.cat.remove_unused_categories() | ||
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 assign the above one with |
||
>>> s.cat.set_categories(list('abcde')) | ||
0 b | ||
1 b | ||
2 c | ||
3 d | ||
dtype: category | ||
Categories (3, object): [b, c, d] | ||
|
||
>>> s.cat.set_categories(list("abcde")) | ||
0 b | ||
1 b | ||
2 c | ||
3 d | ||
dtype: category | ||
Categories (5, object): [a, b, c, d, e] | ||
|
||
>>> s.cat.as_ordered() | ||
0 b | ||
1 b | ||
2 c | ||
3 d | ||
dtype: category | ||
Categories (3, object): [b < c < d] | ||
|
||
>>> s.cat.as_unordered() | ||
0 b | ||
1 b | ||
2 c | ||
3 d | ||
dtype: category | ||
Categories (3, object): [b, c, d] | ||
""" | ||
|
||
def __init__(self, data): | ||
|
@@ -2603,7 +2676,7 @@ def _recode_for_categories(codes: np.ndarray, old_categories, new_categories): | |
>>> new_cat = pd.Index(['a', 'b']) | ||
>>> codes = np.array([0, 1, 1, 2]) | ||
>>> _recode_for_categories(codes, old_cat, new_cat) | ||
array([ 1, 0, 0, -1]) | ||
array([ 1, 0, 0, -1], dtype=int8) | ||
""" | ||
if len(old_categories) == 0: | ||
# All null anyway, so just retain the nulls | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -181,7 +181,7 @@ def _unbox_scalar(self, value: Union[Period, Timestamp, Timedelta, NaTType]) -> | |
|
||
Examples | ||
-------- | ||
>>> self._unbox_scalar(Timedelta('10s')) # DOCTEST: +SKIP | ||
>>> _unbox_scalar(Timedelta('10s')) # doctest: +SKIP | ||
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. i think this makes less sense without the 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. I agree, let's keep the 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. I was trying to see if this will run without the "self", forgot to revert it. |
||
10000000000 | ||
""" | ||
raise AbstractMethodError(self) | ||
|
Uh oh!
There was an error while loading. Please reload this page.