@@ -361,20 +361,16 @@ Renaming categories is done by using the
361
361
Categories must be unique or a ``ValueError `` is raised:
362
362
363
363
.. ipython :: python
364
+ :okexcept: no_traceback
364
365
365
- try :
366
- s = s.cat.rename_categories([1 , 1 , 1 ])
367
- except ValueError as e:
368
- print (" ValueError:" , str (e))
366
+ s = s.cat.rename_categories([1 , 1 , 1 ])
369
367
370
368
Categories must also not be ``NaN `` or a ``ValueError `` is raised:
371
369
372
370
.. ipython :: python
371
+ :okexcept: no_traceback
373
372
374
- try :
375
- s = s.cat.rename_categories([1 , 2 , np.nan])
376
- except ValueError as e:
377
- print (" ValueError:" , str (e))
373
+ s = s.cat.rename_categories([1 , 2 , np.nan])
378
374
379
375
Appending new categories
380
376
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -577,24 +573,21 @@ Equality comparisons work with any list-like object of same length and scalars:
577
573
This doesn't work because the categories are not the same:
578
574
579
575
.. ipython :: python
576
+ :okexcept: no_traceback
580
577
581
- try :
582
- cat > cat_base2
583
- except TypeError as e:
584
- print (" TypeError:" , str (e))
578
+ cat > cat_base2
585
579
586
580
If you want to do a "non-equality" comparison of a categorical series with a list-like object
587
581
which is not categorical data, you need to be explicit and convert the categorical data back to
588
582
the original values:
589
583
590
584
.. ipython :: python
585
+ :flake8- group: Ignore
591
586
592
587
base = np.array([1 , 2 , 3 ])
593
588
594
- try :
595
- cat > base
596
- except TypeError as e:
597
- print (" TypeError:" , str (e))
589
+ @okexcept no_traceback
590
+ cat > base
598
591
599
592
np.asarray(cat) > base
600
593
@@ -765,6 +758,7 @@ Setting values in a categorical column (or ``Series``) works as long as the
765
758
value is included in the ``categories ``:
766
759
767
760
.. ipython :: python
761
+ :flake8- group: Ignore
768
762
769
763
idx = pd.Index([" h" , " i" , " j" , " k" , " l" , " m" , " n" ])
770
764
cats = pd.Categorical([" a" , " a" , " a" , " a" , " a" , " a" , " a" ], categories = [" a" , " b" ])
@@ -773,21 +767,18 @@ value is included in the ``categories``:
773
767
774
768
df.iloc[2 :4 , :] = [[" b" , 2 ], [" b" , 2 ]]
775
769
df
776
- try :
777
- df.iloc[2 :4 , :] = [[" c" , 3 ], [" c" , 3 ]]
778
- except TypeError as e:
779
- print (" TypeError:" , str (e))
770
+ @okexcept no_traceback
771
+ df.iloc[2 :4 , :] = [[" c" , 3 ], [" c" , 3 ]]
780
772
781
773
Setting values by assigning categorical data will also check that the ``categories `` match:
782
774
783
775
.. ipython :: python
776
+ :flake8- group: Ignore
784
777
785
778
df.loc[" j" :" k" , " cats" ] = pd.Categorical([" a" , " a" ], categories = [" a" , " b" ])
786
779
df
787
- try :
788
- df.loc[" j" :" k" , " cats" ] = pd.Categorical([" b" , " b" ], categories = [" a" , " b" , " c" ])
789
- except TypeError as e:
790
- print (" TypeError:" , str (e))
780
+ @okexcept no_traceback
781
+ df.loc[" j" :" k" , " cats" ] = pd.Categorical([" b" , " b" ], categories = [" a" , " b" , " c" ])
791
782
792
783
Assigning a ``Categorical `` to parts of a column of other types will use the values:
793
784
@@ -1072,21 +1063,19 @@ object and not as a low-level NumPy array dtype. This leads to some problems.
1072
1063
NumPy itself doesn't know about the new ``dtype ``:
1073
1064
1074
1065
.. ipython :: python
1066
+ :flake8- group: Ignore
1075
1067
1076
- try :
1077
- np.dtype(" category" )
1078
- except TypeError as e:
1079
- print (" TypeError:" , str (e))
1068
+ @okexcept no_traceback
1069
+ np.dtype(" category" )
1080
1070
1081
1071
dtype = pd.Categorical([" a" ]).dtype
1082
- try :
1083
- np.dtype(dtype)
1084
- except TypeError as e:
1085
- print (" TypeError:" , str (e))
1072
+ @okexcept no_traceback
1073
+ np.dtype(dtype)
1086
1074
1087
1075
Dtype comparisons work:
1088
1076
1089
1077
.. ipython :: python
1078
+ :flake8- group: Ignore
1090
1079
1091
1080
dtype == np.str_
1092
1081
np.str_ == dtype
@@ -1102,13 +1091,12 @@ Using NumPy functions on a ``Series`` of type ``category`` should not work as ``
1102
1091
are not numeric data (even in the case that ``.categories `` is numeric).
1103
1092
1104
1093
.. ipython :: python
1094
+ :flake8- group: Ignore
1105
1095
1106
1096
s = pd.Series(pd.Categorical([1 , 2 , 3 , 4 ]))
1107
- try :
1108
- np.sum(s)
1109
- # same with np.log(s),...
1110
- except TypeError as e:
1111
- print (" TypeError:" , str (e))
1097
+ @okexcept no_traceback
1098
+ np.sum(s)
1099
+ # same with np.log(s),...
1112
1100
1113
1101
.. note ::
1114
1102
If such a function works, please file a bug at https://github.com/pandas-dev/pandas!
0 commit comments