@@ -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,24 +573,21 @@ 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
588582the original values:
589583
590584.. ipython :: python
585+ :flake8- group: Ignore
591586
592587 base = np.array([1 , 2 , 3 ])
593588
594- try :
595- cat > base
596- except TypeError as e:
597- print (" TypeError:" , str (e))
589+ @okexcept no_traceback
590+ cat > base
598591
599592 np.asarray(cat) > base
600593
@@ -765,6 +758,7 @@ Setting values in a categorical column (or ``Series``) works as long as the
765758value is included in the ``categories ``:
766759
767760.. ipython :: python
761+ :flake8- group: Ignore
768762
769763 idx = pd.Index([" h" , " i" , " j" , " k" , " l" , " m" , " n" ])
770764 cats = pd.Categorical([" a" , " a" , " a" , " a" , " a" , " a" , " a" ], categories = [" a" , " b" ])
@@ -773,21 +767,18 @@ value is included in the ``categories``:
773767
774768 df.iloc[2 :4 , :] = [[" b" , 2 ], [" b" , 2 ]]
775769 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 ]]
780772
781773 Setting values by assigning categorical data will also check that the ``categories `` match:
782774
783775.. ipython :: python
776+ :flake8- group: Ignore
784777
785778 df.loc[" j" :" k" , " cats" ] = pd.Categorical([" a" , " a" ], categories = [" a" , " b" ])
786779 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" ])
791782
792783 Assigning a ``Categorical `` to parts of a column of other types will use the values:
793784
@@ -1072,21 +1063,19 @@ object and not as a low-level NumPy array dtype. This leads to some problems.
10721063NumPy itself doesn't know about the new ``dtype ``:
10731064
10741065.. ipython :: python
1066+ :flake8- group: Ignore
10751067
1076- try :
1077- np.dtype(" category" )
1078- except TypeError as e:
1079- print (" TypeError:" , str (e))
1068+ @okexcept no_traceback
1069+ np.dtype(" category" )
10801070
10811071 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)
10861074
10871075 Dtype comparisons work:
10881076
10891077.. ipython :: python
1078+ :flake8- group: Ignore
10901079
10911080 dtype == np.str_
10921081 np.str_ == dtype
@@ -1102,13 +1091,12 @@ Using NumPy functions on a ``Series`` of type ``category`` should not work as ``
11021091are not numeric data (even in the case that ``.categories `` is numeric).
11031092
11041093.. ipython :: python
1094+ :flake8- group: Ignore
11051095
11061096 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),...
11121100
11131101 .. note ::
11141102 If such a function works, please file a bug at https://github.com/pandas-dev/pandas!
0 commit comments