@@ -361,20 +361,16 @@ Renaming categories is done by using the
361361Categories must be unique or a ``ValueError `` is raised:
362362
363363.. ipython :: python
364+ :okexcept: no_traceback
364365
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 ])
369367
370368 Categories must also not be ``NaN `` or a ``ValueError `` is raised:
371369
372370.. ipython :: python
371+ :okexcept: no_traceback
373372
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])
378374
379375 Appending new categories
380376~~~~~~~~~~~~~~~~~~~~~~~~
@@ -577,11 +573,9 @@ Equality comparisons work with any list-like object of same length and scalars:
577573 This doesn't work because the categories are not the same:
578574
579575.. ipython :: python
576+ :okexcept: no_traceback
580577
581- try :
582- cat > cat_base2
583- except TypeError as e:
584- print (" TypeError:" , str (e))
578+ cat > cat_base2
585579
586580 If you want to do a "non-equality" comparison of a categorical series with a list-like object
587581which is not categorical data, you need to be explicit and convert the categorical data back to
@@ -591,10 +585,8 @@ the original values:
591585
592586 base = np.array([1 , 2 , 3 ])
593587
594- try :
595- cat > base
596- except TypeError as e:
597- print (" TypeError:" , str (e))
588+ @okexcept no_traceback # noqa: E999
589+ cat > base
598590
599591 np.asarray(cat) > base
600592
@@ -773,21 +765,17 @@ value is included in the ``categories``:
773765
774766 df.iloc[2 :4 , :] = [[" b" , 2 ], [" b" , 2 ]]
775767 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 ]]
780770
781771 Setting values by assigning categorical data will also check that the ``categories `` match:
782772
783773.. ipython :: python
784774
785775 df.loc[" j" :" k" , " cats" ] = pd.Categorical([" a" , " a" ], categories = [" a" , " b" ])
786776 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" ])
791779
792780 Assigning a ``Categorical `` to parts of a column of other types will use the values:
793781
@@ -1073,16 +1061,12 @@ NumPy itself doesn't know about the new ``dtype``:
10731061
10741062.. ipython :: python
10751063
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" )
10801066
10811067 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)
10861070
10871071 Dtype comparisons work:
10881072
@@ -1104,11 +1088,9 @@ are not numeric data (even in the case that ``.categories`` is numeric).
11041088.. ipython :: python
11051089
11061090 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),...
11121094
11131095 .. note ::
11141096 If such a function works, please file a bug at https://github.com/pandas-dev/pandas!
0 commit comments