Add helper function for making dtype ordinal (ordered category)

This commit is contained in:
RunasSudo 2022-12-03 22:22:48 +11:00
parent 0fa261498a
commit 5dce873e55
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 17 additions and 0 deletions

View File

@ -21,6 +21,7 @@ from .distributions import beta_oddsratio, beta_ratio, hdi, transformed_dist
from .io import pickle_read_compressed, pickle_read_encrypted, pickle_write_compressed, pickle_write_encrypted from .io import pickle_read_compressed, pickle_read_encrypted, pickle_write_compressed, pickle_write_encrypted
from .regress import OrdinalLogit, PenalisedLogit, logit_then_regress, regress, vif from .regress import OrdinalLogit, PenalisedLogit, logit_then_regress, regress, vif
from .sig_tests import anova_oneway, auto_univariable, chi2, mannwhitney, pearsonr, spearman, ttest_ind from .sig_tests import anova_oneway, auto_univariable, chi2, mannwhitney, pearsonr, spearman, ttest_ind
from .utils import as_ordinal
def reload_me(): def reload_me():
import importlib import importlib

View File

@ -135,6 +135,22 @@ def as_numeric(data):
return data.astype('float64'), None return data.astype('float64'), None
def as_ordinal(data):
"""
Convert the data to an ordered category dtype
:param data: Data to convert
:type df: Series
:rtype: Series
"""
if data.dtype == 'category':
if data.cat.ordered:
return data
return data.cat.as_ordered()
return data.astype(pd.CategoricalDtype(ordered=True))
# ---------- # ----------
# Formatting # Formatting