diff --git a/yli/__init__.py b/yli/__init__.py index 0609483..61cf681 100644 --- a/yli/__init__.py +++ b/yli/__init__.py @@ -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 .regress import OrdinalLogit, PenalisedLogit, logit_then_regress, regress, vif from .sig_tests import anova_oneway, auto_univariable, chi2, mannwhitney, pearsonr, spearman, ttest_ind +from .utils import as_ordinal def reload_me(): import importlib diff --git a/yli/utils.py b/yli/utils.py index 1e0da9d..6986dfa 100644 --- a/yli/utils.py +++ b/yli/utils.py @@ -135,6 +135,22 @@ def as_numeric(data): 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