Add helper function for making dtype ordinal (ordered category)
This commit is contained in:
parent
0fa261498a
commit
5dce873e55
@ -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
|
||||||
|
16
yli/utils.py
16
yli/utils.py
@ -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
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user