def _as_categorical_type(index_ops: IndexOpsLike, dtype: CategoricalDtype, spark_type: DataType) -> IndexOpsLike:
    'Cast `index_ops` to categorical dtype, given `dtype` and `spark_type`.'
    assert isinstance(dtype, CategoricalDtype)
    if (dtype.categories is None):
        (codes, uniques) = index_ops.factorize()
        return codes._with_new_scol(codes.spark.column, field=codes._internal.data_fields[0].copy(dtype=CategoricalDtype(categories=uniques)))
    else:
        categories = dtype.categories
        if (len(categories) == 0):
            scol = SF.lit((- 1))
        else:
            kvs = chain(*[(SF.lit(category), SF.lit(code)) for (code, category) in enumerate(categories)])
            map_scol = F.create_map(*kvs)
            scol = F.coalesce(map_scol[index_ops.spark.column], SF.lit((- 1)))
        return index_ops._with_new_scol(scol.cast(spark_type), field=index_ops._internal.data_fields[0].copy(dtype=dtype, spark_type=spark_type, nullable=False))
Cast `index_ops` to categorical dtype, given `dtype` and `spark_type`.
def _as_categorical_type(index_ops: IndexOpsLike, dtype: CategoricalDtype, spark_type: DataType) -> IndexOpsLike:
    
    assert isinstance(dtype, CategoricalDtype)
    if (dtype.categories is None):
        (codes, uniques) = index_ops.factorize()
        return codes._with_new_scol(codes.spark.column, field=codes._internal.data_fields[0].copy(dtype=CategoricalDtype(categories=uniques)))
    else:
        categories = dtype.categories
        if (len(categories) == 0):
            scol = SF.lit((- 1))
        else:
            kvs = chain(*[(SF.lit(category), SF.lit(code)) for (code, category) in enumerate(categories)])
            map_scol = F.create_map(*kvs)
            scol = F.coalesce(map_scol[index_ops.spark.column], SF.lit((- 1)))
        return index_ops._with_new_scol(scol.cast(spark_type), field=index_ops._internal.data_fields[0].copy(dtype=dtype, spark_type=spark_type, nullable=False))