Convert columns of the DataFrame to category dtype. .. warning:: This method eagerly computes the categories of the chosen columns. Parameters ---------- columns : list, optional A list of column names to convert to categoricals. By default any
(self, columns=None, index=None, split_every=None, **kwargs)
| 3755 | return self._comparison_op(expr.EQFrame, other, level, axis) |
| 3756 | |
| 3757 | def categorize(self, columns=None, index=None, split_every=None, **kwargs): |
| 3758 | """Convert columns of the DataFrame to category dtype. |
| 3759 | |
| 3760 | .. warning:: This method eagerly computes the categories of the chosen columns. |
| 3761 | |
| 3762 | Parameters |
| 3763 | ---------- |
| 3764 | columns : list, optional |
| 3765 | A list of column names to convert to categoricals. By default any |
| 3766 | column with an object dtype is converted to a categorical, and any |
| 3767 | unknown categoricals are made known. |
| 3768 | index : bool, optional |
| 3769 | Whether to categorize the index. By default, object indices are |
| 3770 | converted to categorical, and unknown categorical indices are made |
| 3771 | known. Set True to always categorize the index, False to never. |
| 3772 | split_every : int, optional |
| 3773 | Group partitions into groups of this size while performing a |
| 3774 | tree-reduction. If set to False, no tree-reduction will be used. |
| 3775 | kwargs |
| 3776 | Keyword arguments are passed on to compute. |
| 3777 | """ |
| 3778 | df = self |
| 3779 | meta = df._meta |
| 3780 | if columns is None: |
| 3781 | columns = list(meta.select_dtypes(["object", "string", "category"]).columns) |
| 3782 | elif is_scalar(columns): |
| 3783 | columns = [columns] |
| 3784 | |
| 3785 | # Filter out known categorical columns |
| 3786 | columns = [ |
| 3787 | c |
| 3788 | for c in columns |
| 3789 | if not (is_categorical_dtype(meta[c]) and has_known_categories(meta[c])) |
| 3790 | ] |
| 3791 | |
| 3792 | if index is not False: |
| 3793 | if is_categorical_dtype(meta.index): |
| 3794 | index = not has_known_categories(meta.index) |
| 3795 | elif index is None: |
| 3796 | index = str(meta.index.dtype) in ("object", "string") |
| 3797 | |
| 3798 | # Nothing to do |
| 3799 | if not len(columns) and index is False: |
| 3800 | return df |
| 3801 | |
| 3802 | from dask.dataframe.dask_expr._collection import new_collection |
| 3803 | |
| 3804 | # Eagerly compute the categories |
| 3805 | categories, index = new_collection( |
| 3806 | GetCategories(self, columns=columns, index=index, split_every=split_every) |
| 3807 | ).compute() |
| 3808 | |
| 3809 | # Some operations like get_dummies() rely on the order of categories |
| 3810 | categories = {k: v.sort_values() for k, v in categories.items()} |
| 3811 | |
| 3812 | # Categorize each partition |
| 3813 | return new_collection(Categorize(self, categories, index)) |
| 3814 |