Path: blob/main/crates/polars-python/src/conversion/categorical.rs
7889 views
use std::sync::Arc;12use polars_dtype::categorical::{CatSize, Categories};3use pyo3::{pyclass, pymethods};45#[pyclass(frozen)]6#[repr(transparent)]7#[derive(Clone)]8pub struct PyCategories {9categories: Arc<Categories>,10}1112impl PyCategories {13pub fn categories(&self) -> &Arc<Categories> {14&self.categories15}16}1718#[pymethods]19impl PyCategories {20#[new]21pub fn __init__(name: String, namespace: String, physical: String) -> Self {22Self {23categories: Categories::new(name.into(), namespace.into(), physical.parse().unwrap()),24}25}2627#[staticmethod]28pub fn global_categories() -> Self {29Self {30categories: Categories::global(),31}32}3334#[staticmethod]35pub fn random(namespace: String, physical: String) -> Self {36Self {37categories: Categories::random(namespace.into(), physical.parse().unwrap()),38}39}4041pub fn __eq__(&self, other: &Self) -> bool {42Arc::ptr_eq(&self.categories, &other.categories)43}4445pub fn __hash__(&self) -> u64 {46self.categories.hash()47}4849pub fn name(&self) -> &str {50self.categories.name()51}5253pub fn namespace(&self) -> &str {54self.categories.namespace()55}5657pub fn physical(&self) -> &str {58self.categories.physical().as_str()59}6061pub fn get_cat(&self, s: &str) -> Option<CatSize> {62self.categories.mapping().get_cat(s)63}6465pub fn cat_to_str(&self, cat: CatSize) -> Option<String> {66Some(self.categories.mapping().cat_to_str(cat)?.to_owned())67}6869pub fn is_global(&self) -> bool {70self.categories.is_global()71}72}7374impl From<Arc<Categories>> for PyCategories {75fn from(categories: Arc<Categories>) -> Self {76Self { categories }77}78}798081