Skip to main content
`Ctrl`+`K`

# pandas.Series.cat

Series.cat()

  Accessor object for categorical properties of the Series values.

  Parameters:
    data Series or CategoricalIndex

      The object to which the categorical accessor is attached.

  See also

  `Series.dt`

    Accessor object for datetimelike properties of the Series values.

  `Series.sparse`

    Accessor for sparse matrix data types.

  Examples

      >>> s = pd.Series(list("abbccc")).astype("category")
      >>> s
      0    a
      1    b
      2    b
      3    c
      4    c
      5    c
      dtype: category
      Categories (3, str): ['a', 'b', 'c']

      >>> s.cat.categories
      Index(['a', 'b', 'c'], dtype='str')

      >>> s.cat.rename_categories(list("cba"))
      0    c
      1    b
      2    b
      3    a
      4    a
      5    a
      dtype: category
      Categories (3, str): ['c', 'b', 'a']

      >>> s.cat.reorder_categories(list("cba"))
      0    a
      1    b
      2    b
      3    c
      4    c
      5    c
      dtype: category
      Categories (3, str): ['c', 'b', 'a']

      >>> s.cat.add_categories(["d", "e"])
      0    a
      1    b
      2    b
      3    c
      4    c
      5    c
      dtype: category
      Categories (5, str): ['a', 'b', 'c', 'd', 'e']

      >>> s.cat.remove_categories(["a", "c"])
      0    NaN
      1      b
      2      b
      3    NaN
      4    NaN
      5    NaN
      dtype: category
      Categories (1, str): ['b']

      >>> s1 = s.cat.add_categories(["d", "e"])
      >>> s1.cat.remove_unused_categories()
      0    a
      1    b
      2    b
      3    c
      4    c
      5    c
      dtype: category
      Categories (3, str): ['a', 'b', 'c']

      >>> s.cat.set_categories(list("abcde"))
      0    a
      1    b
      2    b
      3    c
      4    c
      5    c
      dtype: category
      Categories (5, str): ['a', 'b', 'c', 'd', 'e']

      >>> s.cat.as_ordered()
      0    a
      1    b
      2    b
      3    c
      4    c
      5    c
      dtype: category
      Categories (3, str): ['a' < 'b' < 'c']

      >>> s.cat.as_unordered()
      0    a
      1    b
      2    b
      3    c
      4    c
      5    c
      dtype: category
      Categories (3, str): ['a', 'b', 'c']