@@ -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,11 +573,9 @@ 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
@@ -591,10 +585,8 @@ the original values:
591
585
592
586
base = np.array([1 , 2 , 3 ])
593
587
594
- try :
595
- cat > base
596
- except TypeError as e:
597
- print (" TypeError:" , str (e))
588
+ @okexcept no_traceback # noqa: E999
589
+ cat > base
598
590
599
591
np.asarray(cat) > base
600
592
@@ -773,21 +765,17 @@ value is included in the ``categories``:
773
765
774
766
df.iloc[2 :4 , :] = [[" b" , 2 ], [" b" , 2 ]]
775
767
df
776
- try :
777
- df.iloc[2 :4 , :] = [[" c" , 3 ], [" c" , 3 ]]
778
- except TypeError as e:
779
- print (" TypeError:" , str (e))
768
+ @okexcept no_traceback # noqa: E999
769
+ df.iloc[2 :4 , :] = [[" c" , 3 ], [" c" , 3 ]]
780
770
781
771
Setting values by assigning categorical data will also check that the ``categories `` match:
782
772
783
773
.. ipython :: python
784
774
785
775
df.loc[" j" :" k" , " cats" ] = pd.Categorical([" a" , " a" ], categories = [" a" , " b" ])
786
776
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))
777
+ @okexcept no_traceback # noqa: E999
778
+ df.loc[" j" :" k" , " cats" ] = pd.Categorical([" b" , " b" ], categories = [" a" , " b" , " c" ])
791
779
792
780
Assigning a ``Categorical `` to parts of a column of other types will use the values:
793
781
@@ -1073,16 +1061,12 @@ NumPy itself doesn't know about the new ``dtype``:
1073
1061
1074
1062
.. ipython :: python
1075
1063
1076
- try :
1077
- np.dtype(" category" )
1078
- except TypeError as e:
1079
- print (" TypeError:" , str (e))
1064
+ @okexcept no_traceback # noqa: E999
1065
+ np.dtype(" category" )
1080
1066
1081
1067
dtype = pd.Categorical([" a" ]).dtype
1082
- try :
1083
- np.dtype(dtype)
1084
- except TypeError as e:
1085
- print (" TypeError:" , str (e))
1068
+ @okexcept no_traceback # noqa: E999
1069
+ np.dtype(dtype)
1086
1070
1087
1071
Dtype comparisons work:
1088
1072
@@ -1104,11 +1088,9 @@ are not numeric data (even in the case that ``.categories`` is numeric).
1104
1088
.. ipython :: python
1105
1089
1106
1090
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))
1091
+ @okexcept no_traceback # noqa: E999
1092
+ np.sum(s)
1093
+ # same with np.log(s),...
1112
1094
1113
1095
.. note ::
1114
1096
If such a function works, please file a bug at https://github.com/pandas-dev/pandas!
0 commit comments