From 5dce873e55d8b33e66c855b5925a1a05872ec8d1 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sat, 3 Dec 2022 22:22:48 +1100 Subject: [PATCH] Add helper function for making dtype ordinal (ordered category) --- yli/__init__.py | 1 + yli/utils.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) 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