From 37ec33c8476ed422284207e33ed123125d7cd348 Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Thu, 6 Jun 2024 17:07:21 -0400 Subject: [PATCH 1/2] move handling of deprecated parameters to deprecations module. --- tableone/deprecations.py | 26 ++++++++++++++++++++++++++ tableone/tableone.py | 36 ++++++------------------------------ 2 files changed, 32 insertions(+), 30 deletions(-) create mode 100644 tableone/deprecations.py diff --git a/tableone/deprecations.py b/tableone/deprecations.py new file mode 100644 index 0000000..2e21cbc --- /dev/null +++ b/tableone/deprecations.py @@ -0,0 +1,26 @@ +import warnings + +# display deprecation warnings +warnings.simplefilter('always', DeprecationWarning) + + +def deprecated_parameter(parameter, parameter_name, message, version=None): + """ + Raise warning for a deprecated parameter. + + Parameters: + - parameter: parameter to be removed. + - parameter: name of parameter to be removed. + - message: str, message to insert into deprecated warning. + - version: str, version when the parameter was deprecated (optional). + + Usage: + deprecated_parameter(parameter=label, parameter_name='level, new_name='rename', version='0.7') + """ + if parameter: + if version: + version_message = f"version {version}" + else: + version_message = "a future version" + warnings.warn(f"{parameter_name} is deprecated and will be removed in {version_message}. {message}", + category=DeprecationWarning, stacklevel=2) diff --git a/tableone/tableone.py b/tableone/tableone.py index a8ea638..1ce56c0 100644 --- a/tableone/tableone.py +++ b/tableone/tableone.py @@ -10,12 +10,10 @@ import pandas as pd from tabulate import tabulate +from tableone.deprecations import deprecated_parameter from tableone.statistics import Statistics from tableone.validators import DataValidator, InputValidator, InputError -# display deprecation warnings -warnings.simplefilter('always', DeprecationWarning) - def load_dataset(name: str) -> pd.DataFrame: """ @@ -215,7 +213,10 @@ def __init__(self, data: pd.DataFrame, tukey_test: bool = False, pval_threshold: Optional[float] = None) -> None: - self._handle_deprecations(labels, rename, isnull, pval_test_name, remarks) + deprecated_parameter(labels, "labels", "Use 'rename' instead") + deprecated_parameter(isnull, "isnull", "Use 'missing' instead") + deprecated_parameter(pval_test_name, "pval_test_name", "Use 'htest_name' instead") + deprecated_parameter(remarks, "remarks", "Use test names instead (e.g. diptest = True)") if not columns: columns = data.columns.values # type: ignore @@ -225,7 +226,7 @@ def __init__(self, data: pd.DataFrame, self.data_validator.validate(data, columns) # type: ignore self.input_validator = InputValidator() - self.input_validator.validate(groupby, nonnormal, min_max, pval_adjust, order, # type: ignore + self.input_validator.validate(groupby, nonnormal, min_max, pval_adjust, order, # type: ignore pval, columns, categorical, continuous) # type: ignore # nonnormal should be a list @@ -373,31 +374,6 @@ def __init__(self, data: pd.DataFrame, if display_all: self._set_display_options() - def _handle_deprecations(self, labels, rename, isnull, pval_test_name, remarks): - """ - Raise deprecation warnings. - """ - if labels is not None: - warnings.warn("The 'labels' argument of TableOne() is deprecated and will be removed in a future version. " - "Use 'rename' instead.", DeprecationWarning, stacklevel=3) - - if labels is not None and rename is not None: - raise TypeError("TableOne received both 'labels' and 'rename'. Please use only 'rename'.") - - if isnull is not None: - warnings.warn("The 'isnull' argument is deprecated; use 'missing' instead.", - DeprecationWarning, stacklevel=3) - - # pval_test_name is now htest_name - if pval_test_name: - warnings.warn("The pval_test_name argument is deprecated; use htest_name instead.", - DeprecationWarning, stacklevel=3) - - if remarks: - warnings.warn("The 'remarks' argument is deprecated; specify tests " - "by name instead (e.g. diptest = True)", - DeprecationWarning, stacklevel=2) - def __str__(self) -> str: return self.tableone.to_string() + self._generate_remarks('\n') From 1604585cd9aa0d222823cbdd8add2f9c39c98e1d Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Thu, 6 Jun 2024 17:09:39 -0400 Subject: [PATCH 2/2] labels argument was changed to rename. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7785847..60650fe 100644 --- a/README.md +++ b/README.md @@ -101,13 +101,13 @@ continuous = ['Age', 'SysABP', 'Height', 'Weight'] ```python groupby = 'death' nonnormal = ['Age'] -labels={'death': 'mortality'} +rename={'death': 'mortality'} ``` 7. Create an instance of TableOne with the input arguments: ```python -mytable = TableOne(data, columns=columns, categorical=categorical, continuous=continuous, groupby=groupby, nonnormal=nonnormal, rename=labels, pval=False) +mytable = TableOne(data, columns=columns, categorical=categorical, continuous=continuous, groupby=groupby, nonnormal=nonnormal, rename=rename, pval=False) ``` 8. Display the table using the `tabulate` method. The `tablefmt` argument allows the table to be displayed in multiple formats, including "github", "grid", "fancy_grid", "rst", "html", and "latex".