diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3c82aac..8ef63ff 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,12 +15,19 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.7", "3.8", "3.9", "3.10"] + python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"] + pandas-version: ["latest"] + include: + # oldest pandas with DataFrame.map, and the last 2.x series + - python-version: "3.11" + pandas-version: "2.1.*" + - python-version: "3.12" + pandas-version: "2.2.*" steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - name: Install dependencies @@ -28,10 +35,11 @@ jobs: python -m pip install --upgrade pip python -m pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + if [ "${{ matrix.pandas-version }}" != "latest" ]; then pip install "pandas==${{ matrix.pandas-version }}" "numpy<2"; fi - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics - name: Test with unittest run: | - python -m unittest + python -m unittest -v diff --git a/plot_likert/plot_likert.py b/plot_likert/plot_likert.py index d9e86f9..d3b173b 100644 --- a/plot_likert/plot_likert.py +++ b/plot_likert/plot_likert.py @@ -7,7 +7,8 @@ Note: the data must be strings -for a float: scores.applymap(int).applymap(str) +for a float: scores.map(int).map(str) +(with pandas < 2.1, use DataFrame.applymap instead of DataFrame.map) """ @@ -46,6 +47,18 @@ class PlotLikertError(ValueError): pass +def _map_elements(df: pd.DataFrame, func) -> pd.DataFrame: + """ + Apply a function to every element of a DataFrame. + + DataFrame.applymap was deprecated in pandas 2.1 (renamed to DataFrame.map) + and removed in pandas 3.0, so use whichever one is available. + """ + if hasattr(pd.DataFrame, "map"): + return df.map(func) + return df.applymap(func) # pandas < 2.1 + + def plot_counts( counts: pd.DataFrame, scale: Scale, @@ -254,18 +267,14 @@ def validate(value): f"A response was found with value `{value}`, which is not one of the values in the provided scale: {scale}. If this is unexpected, you might want to double-check for extra whitespace, capitalization, spelling, or type (int versus str)." ) - try: - df.map(validate) - except AttributeError: # for compatibility with Pandas < 2.1.0 - df.applymap(validate) + _map_elements(df, validate) # fix long questions for printing old_labels = list(df) new_labels = ["\n".join(wrap(str(l), label_max_width)) for l in old_labels] - if pd.__version__ >= "1.5.0": - df = df.set_axis(new_labels, axis=1, copy=True) - else: - df = df.set_axis(new_labels, axis=1, inplace=False) + # set_axis returns a new object by default (pandas >= 1.0); the `inplace` keyword + # was removed in pandas 2.0 and `copy` is deprecated in pandas 3.0, so pass neither + df = df.set_axis(new_labels, axis=1) counts_unordered = df.apply(lambda row: row.value_counts()) counts = counts_unordered.reindex(scale).T @@ -299,12 +308,9 @@ def likert_percentages( "In your data, not all questions have the same number of responses. i.e., different numbers of people answered each question. Therefore, the percentages aren't directly comparable: X% for one question represents a different number of responses than X% for another question, yet they will appear the same in the percentage graph. This may be misleading to your reader." ) - try: - return counts.apply(lambda row: row / row.sum(), axis=1).map(lambda v: 100 * v) - except AttributeError: # for compatibility with Pandas < 2.1.0 - return counts.apply(lambda row: row / row.sum(), axis=1).applymap( - lambda v: 100 * v - ) + return _map_elements( + counts.apply(lambda row: row / row.sum(), axis=1), lambda v: 100 * v + ) def _compute_counts_percentage(counts: pd.DataFrame) -> pd.DataFrame: @@ -331,10 +337,7 @@ def likert_response(df: pd.DataFrame, scale: Scale) -> pd.DataFrame: orginal data. """ for i in range(0, len(scale)): - try: - df = df.map(lambda x: scale[i] if str(i) in x else x) - except AttributeError: # for compatibility with Pandas < 2.1.0 - df = df.map(lambda x: scale[i] if str(i) in x else x) + df = _map_elements(df, lambda x: scale[i] if str(i) in x else x) return df diff --git a/tests/test_pandas_compat.py b/tests/test_pandas_compat.py new file mode 100644 index 0000000..6fa6315 --- /dev/null +++ b/tests/test_pandas_compat.py @@ -0,0 +1,154 @@ +""" +Regression tests for the data-handling functions and for compatibility with +current pandas versions (2.1+ and 3.x: DataFrame.map instead of applymap, +positional Series lookups via .iloc, the new default string dtype, +and no deprecated keyword arguments). +""" + +import unittest +import warnings + +import matplotlib + +matplotlib.use("Agg") # no GUI needed for the tests +import matplotlib.pyplot as plt +import numpy as np +import pandas as pd + +import plot_likert +from plot_likert.scales import agree + + +def sample_responses(index=None): + return pd.DataFrame( + { + "Q1": [agree[0], agree[1], agree[2], agree[3], agree[4], agree[4]], + "Q2": [agree[4], agree[4], agree[3], agree[2], agree[2], agree[0]], + }, + index=index, + ) + + +class PandasWarningsAsErrors(unittest.TestCase): + """Fail on any deprecation warning raised while running a test.""" + + def setUp(self): + self._warnings = warnings.catch_warnings() + self._warnings.__enter__() + warnings.simplefilter("error", DeprecationWarning) + warnings.simplefilter("error", FutureWarning) + try: # pandas 3 has its own deprecation warning classes + from pandas.errors import PandasChangeWarning + + warnings.simplefilter("error", PandasChangeWarning) + except ImportError: + pass + + def tearDown(self): + self._warnings.__exit__(None, None, None) + plt.close("all") + + +class TestLikertCounts(PandasWarningsAsErrors): + def test_counts_dataframe(self): + counts = plot_likert.likert_counts(sample_responses(), agree) + self.assertEqual(list(counts.columns), list(agree)) + self.assertEqual(list(counts.index), ["Q1", "Q2"]) + self.assertEqual(counts.loc["Q1"].tolist(), [1, 1, 1, 1, 2]) + self.assertEqual(counts.loc["Q2"].tolist(), [1, 0, 2, 1, 2]) + + def test_counts_series(self): + counts = plot_likert.likert_counts(sample_responses()["Q1"], agree) + self.assertEqual(counts.loc["Q1"].tolist(), [1, 1, 1, 1, 2]) + + def test_counts_with_missing_values(self): + df = sample_responses() + df.loc[0, "Q1"] = np.nan + counts = plot_likert.likert_counts(df, agree) + self.assertEqual(counts.loc["Q1"].tolist(), [0, 1, 1, 1, 2]) + + def test_counts_explicit_string_dtype(self): + counts = plot_likert.likert_counts(sample_responses().astype("string"), agree) + self.assertEqual(counts.loc["Q2"].tolist(), [1, 0, 2, 1, 2]) + + def test_long_labels_are_wrapped(self): + df = sample_responses().rename(columns={"Q1": "a " * 30}) + counts = plot_likert.likert_counts(df, agree, label_max_width=10) + self.assertIn("\n", counts.index[0]) + + def test_input_is_not_modified(self): + df = sample_responses() + plot_likert.likert_counts(df, agree, label_max_width=1) + self.assertEqual(list(df.columns), ["Q1", "Q2"]) + + def test_invalid_value_raises(self): + df = sample_responses() + df.loc[0, "Q1"] = "Maybe" + with self.assertRaises(plot_likert.PlotLikertError): + plot_likert.likert_counts(df, agree) + + def test_numeric_responses_with_numeric_scale(self): + df = pd.DataFrame({"Q1": [1, 2, 3, 4, 5, 5]}) + counts = plot_likert.likert_counts(df, [1, 2, 3, 4, 5]) + self.assertEqual(counts.loc["Q1"].tolist(), [1, 1, 1, 1, 2]) + + +class TestLikertPercentages(PandasWarningsAsErrors): + def test_percentages(self): + pct = plot_likert.likert_percentages(sample_responses(), agree) + self.assertAlmostEqual(pct.loc["Q1", agree[4]], 100 * 2 / 6) + np.testing.assert_allclose(pct.sum(axis=1).to_numpy(), [100, 100]) + + def test_percentages_non_integer_index(self): + # pandas 3: Series[0] is a label lookup, so the first row must be read with .iloc + df = sample_responses(index=list("abcdef")) + pct = plot_likert.likert_percentages(df, agree) + np.testing.assert_allclose(pct.sum(axis=1).to_numpy(), [100, 100]) + + def test_unequal_response_counts_warn(self): + df = sample_responses() + df.loc[0, "Q1"] = np.nan + with self.assertWarns(UserWarning): + plot_likert.likert_percentages(df, agree) + + +class TestLikertResponse(PandasWarningsAsErrors): + def test_numeric_codes_replaced_by_scale(self): + df = pd.DataFrame({"Q1": ["1", "2", "3"], "Q2": ["3", "3", "1"]}) + result = plot_likert.likert_response(df, ["zero", "one", "two", "three"]) + self.assertEqual(result["Q1"].tolist(), ["one", "two", "three"]) + self.assertEqual(result["Q2"].tolist(), ["three", "three", "one"]) + + +class TestPlotting(PandasWarningsAsErrors): + def test_plot_likert(self): + ax = plot_likert.plot_likert(sample_responses(), agree) + self.assertEqual(len(ax.patches), 2 * (len(agree) + 1)) + + def test_plot_likert_percentages_and_bar_labels(self): + ax = plot_likert.plot_likert( + sample_responses(), agree, plot_percentage=True, bar_labels=True + ) + self.assertEqual(ax.get_xlabel(), "Percentage of Responses") + + def test_plot_likert_series(self): + ax = plot_likert.plot_likert(sample_responses()["Q2"], agree) + self.assertEqual(len(ax.patches), len(agree) + 1) + + def test_plot_likert_format_scale(self): + df = pd.DataFrame({"Q1": ["1", "2", "3", "4", "5"]}) + ax = plot_likert.plot_likert(df, agree, format_scale=["NA"] + list(agree)) + self.assertEqual(ax.get_xlabel(), "Number of Responses") + + def test_plot_counts_non_integer_index(self): + counts = pd.DataFrame( + [[1, 2, 4, 2, 1], [2, 2, 2, 2, 2]], + columns=agree, + index=["first question", "second question"], + ) + ax = plot_likert.plot_counts(counts, agree, compute_percentages=True) + self.assertEqual(ax.get_xlabel(), "Percentage of Responses") + + +if __name__ == "__main__": + unittest.main()