@@ -1598,19 +1598,19 @@ def sort_values(self, inplace=False, ascending=True, na_position="last"):
1598
1598
1599
1599
>>> c = pd.Categorical([np.nan, 2, 2, np.nan, 5])
1600
1600
>>> c
1601
- [NaN, 2.0 , 2.0 , NaN, 5.0 ]
1601
+ [NaN, 2, 2, NaN, 5]
1602
1602
Categories (2, int64): [2, 5]
1603
1603
>>> c.sort_values()
1604
- [2.0 , 2.0 , 5.0 , NaN, NaN]
1604
+ [2, 2, 5, NaN, NaN]
1605
1605
Categories (2, int64): [2, 5]
1606
1606
>>> c.sort_values(ascending=False)
1607
- [5.0 , 2.0 , 2.0 , NaN, NaN]
1607
+ [5, 2, 2, NaN, NaN]
1608
1608
Categories (2, int64): [2, 5]
1609
1609
>>> c.sort_values(na_position='first')
1610
- [NaN, NaN, 2.0 , 2.0 , 5.0 ]
1610
+ [NaN, NaN, 2, 2, 5]
1611
1611
Categories (2, int64): [2, 5]
1612
1612
>>> c.sort_values(ascending=False, na_position='first')
1613
- [NaN, NaN, 5.0 , 2.0 , 2.0 ]
1613
+ [NaN, NaN, 5, 2, 2]
1614
1614
Categories (2, int64): [2, 5]
1615
1615
"""
1616
1616
inplace = validate_bool_kwarg (inplace , "inplace" )
@@ -1835,7 +1835,7 @@ def take(self, indexer, allow_fill: bool = False, fill_value=None):
1835
1835
1836
1836
>>> cat.take([0, -1, -1], allow_fill=True, fill_value='a')
1837
1837
[a, a, a]
1838
- Categories (3 , object): [a, b]
1838
+ Categories (2 , object): [a, b]
1839
1839
1840
1840
Specifying a fill value that's not in ``self.categories``
1841
1841
will raise a ``TypeError``.
@@ -2231,33 +2231,32 @@ def unique(self):
2231
2231
-------
2232
2232
unique values : ``Categorical``
2233
2233
2234
+ See Also
2235
+ --------
2236
+ pandas.unique
2237
+ CategoricalIndex.unique
2238
+ Series.unique
2239
+
2234
2240
Examples
2235
2241
--------
2236
2242
An unordered Categorical will return categories in the
2237
2243
order of appearance.
2238
2244
2239
- >>> pd.Categorical(list(' baabc') )
2245
+ >>> pd.Categorical(list(" baabc")).unique( )
2240
2246
[b, a, c]
2241
2247
Categories (3, object): [b, a, c]
2242
2248
2243
- >>> pd.Categorical(list(' baabc' ), categories=list(' abc') )
2249
+ >>> pd.Categorical(list(" baabc" ), categories=list(" abc")).unique( )
2244
2250
[b, a, c]
2245
2251
Categories (3, object): [b, a, c]
2246
2252
2247
2253
An ordered Categorical preserves the category ordering.
2248
2254
2249
- >>> pd.Categorical(list('baabc'),
2250
- ... categories=list(' abc'),
2251
- ... ordered=True )
2255
+ >>> pd.Categorical(
2256
+ ... list("baabc"), categories=list(" abc"), ordered=True
2257
+ ... ).unique( )
2252
2258
[b, a, c]
2253
2259
Categories (3, object): [a < b < c]
2254
-
2255
- See Also
2256
- --------
2257
- unique
2258
- CategoricalIndex.unique
2259
- Series.unique
2260
-
2261
2260
"""
2262
2261
# unlike np.unique, unique1d does not sort
2263
2262
unique_codes = unique1d (self .codes )
@@ -2438,7 +2437,7 @@ def replace(self, to_replace, value, inplace: bool = False):
2438
2437
--------
2439
2438
>>> s = pd.Categorical([1, 2, 1, 3])
2440
2439
>>> s.replace(1, 3)
2441
- [3, 3, 2 , 3]
2440
+ [3, 2, 3 , 3]
2442
2441
Categories (2, int64): [2, 3]
2443
2442
"""
2444
2443
inplace = validate_bool_kwarg (inplace , "inplace" )
@@ -2506,16 +2505,100 @@ class CategoricalAccessor(PandasDelegate, PandasObject, NoNewAttributesMixin):
2506
2505
2507
2506
Examples
2508
2507
--------
2508
+ >>> s = pd.Series(list("abbccc")).astype("category")
2509
+ >>> s
2510
+ 0 a
2511
+ 1 b
2512
+ 2 b
2513
+ 3 c
2514
+ 4 c
2515
+ 5 c
2516
+ dtype: category
2517
+ Categories (3, object): [a, b, c]
2518
+
2509
2519
>>> s.cat.categories
2510
- >>> s.cat.categories = list('abc')
2511
- >>> s.cat.rename_categories(list('cab'))
2512
- >>> s.cat.reorder_categories(list('cab'))
2513
- >>> s.cat.add_categories(['d','e'])
2514
- >>> s.cat.remove_categories(['d'])
2515
- >>> s.cat.remove_unused_categories()
2516
- >>> s.cat.set_categories(list('abcde'))
2520
+ Index(['a', 'b', 'c'], dtype='object')
2521
+
2522
+ >>> s.cat.rename_categories(list("cba"))
2523
+ 0 c
2524
+ 1 b
2525
+ 2 b
2526
+ 3 a
2527
+ 4 a
2528
+ 5 a
2529
+ dtype: category
2530
+ Categories (3, object): [c, b, a]
2531
+
2532
+ >>> s.cat.reorder_categories(list("cba"))
2533
+ 0 a
2534
+ 1 b
2535
+ 2 b
2536
+ 3 c
2537
+ 4 c
2538
+ 5 c
2539
+ dtype: category
2540
+ Categories (3, object): [c, b, a]
2541
+
2542
+ >>> s.cat.add_categories(["d", "e"])
2543
+ 0 a
2544
+ 1 b
2545
+ 2 b
2546
+ 3 c
2547
+ 4 c
2548
+ 5 c
2549
+ dtype: category
2550
+ Categories (5, object): [a, b, c, d, e]
2551
+
2552
+ >>> s.cat.remove_categories(["a", "c"])
2553
+ 0 NaN
2554
+ 1 b
2555
+ 2 b
2556
+ 3 NaN
2557
+ 4 NaN
2558
+ 5 NaN
2559
+ dtype: category
2560
+ Categories (1, object): [b]
2561
+
2562
+ >>> s1 = s.cat.add_categories(["d", "e"])
2563
+ >>> s1.cat.remove_unused_categories()
2564
+ 0 a
2565
+ 1 b
2566
+ 2 b
2567
+ 3 c
2568
+ 4 c
2569
+ 5 c
2570
+ dtype: category
2571
+ Categories (3, object): [a, b, c]
2572
+
2573
+ >>> s.cat.set_categories(list("abcde"))
2574
+ 0 a
2575
+ 1 b
2576
+ 2 b
2577
+ 3 c
2578
+ 4 c
2579
+ 5 c
2580
+ dtype: category
2581
+ Categories (5, object): [a, b, c, d, e]
2582
+
2517
2583
>>> s.cat.as_ordered()
2584
+ 0 a
2585
+ 1 b
2586
+ 2 b
2587
+ 3 c
2588
+ 4 c
2589
+ 5 c
2590
+ dtype: category
2591
+ Categories (3, object): [a < b < c]
2592
+
2518
2593
>>> s.cat.as_unordered()
2594
+ 0 a
2595
+ 1 b
2596
+ 2 b
2597
+ 3 c
2598
+ 4 c
2599
+ 5 c
2600
+ dtype: category
2601
+ Categories (3, object): [a, b, c]
2519
2602
"""
2520
2603
2521
2604
def __init__ (self , data ):
@@ -2603,7 +2686,7 @@ def _recode_for_categories(codes: np.ndarray, old_categories, new_categories):
2603
2686
>>> new_cat = pd.Index(['a', 'b'])
2604
2687
>>> codes = np.array([0, 1, 1, 2])
2605
2688
>>> _recode_for_categories(codes, old_cat, new_cat)
2606
- array([ 1, 0, 0, -1])
2689
+ array([ 1, 0, 0, -1], dtype=int8 )
2607
2690
"""
2608
2691
if len (old_categories ) == 0 :
2609
2692
# All null anyway, so just retain the nulls
0 commit comments