Skip to content

Fix compatibility with pandas 3 - #58

Open
Klapperhorn wants to merge 1 commit into
nmalkin:masterfrom
Klapperhorn:pandas3-compat
Open

Klapperhorn wants to merge 1 commit into
nmalkin:masterfrom
Klapperhorn:pandas3-compat

Conversation

@Klapperhorn

Copy link
Copy Markdown

pandas 3.0 removed DataFrame.applymap, so the current PyPI release (0.5.0) fails with AttributeError: 'DataFrame' object has no attribute 'applymap'. Master already switched to DataFrame.map (ce15502), but that change has not been released yet, and a few smaller things remain:

  • likert_counts called set_axis(..., copy=True). In pandas 3 the copy keyword is deprecated (Pandas4Warning, to be removed in pandas 4), and the string comparison pd.__version__ >= "1.5.0" used to choose the branch is fragile. set_axis returns a new object by default since pandas 1.0, so the call is now just df.set_axis(new_labels, axis=1).
  • The applymap fallback in likert_response called .map in both branches, so pandas < 2.1 would still fail there. All three element-wise calls now go through a small helper, _map_elements(), which uses DataFrame.map when available and applymap otherwise. This also avoids except AttributeError around user callbacks.
  • Module docstring updated (scores.map(int).map(str)).

Tests:

  • New tests/test_pandas_compat.py covers likert_counts (DataFrame, Series, NaN, string dtype, wrapping, invalid values, numeric scales), likert_percentages (including a non-integer index), likert_response, and plotting with the Agg backend. pandas/NumPy deprecation warnings are turned into errors inside these tests.
  • CI matrix updated: Python 3.9-3.13 with the latest pandas, plus pandas 2.1 and 2.2. Actions bumped to checkout@v4 / setup-python@v5; Python 3.7 is no longer available on ubuntu-latest.

Tested locally: pandas 3.1.1 (Python 3.12, NumPy 2.5, Matplotlib 3.11), pandas 2.2.3 and pandas 2.1.4. All 20 tests pass. The applymap fallback was also checked by simulating pandas < 2.1.

I've used plot-likert a lot over the last few years, both for analysing survey data in my research and in teaching, and it has saved me and my students a lot of time. Thank you for creating and maintaining it! A release with this fix (e.g. 0.5.1) would help everyone who has moved to pandas 3.

🤖 Generated with Claude Code

- likert_counts: drop the `copy` keyword from set_axis (deprecated in
  pandas 3, Pandas4Warning) and the string version comparison
- route all element-wise calls through _map_elements(), which uses
  DataFrame.map (pandas >= 2.1) and falls back to applymap; this also
  fixes the fallback in likert_response, which called .map twice
- update module docstring (applymap -> map)
- add tests/test_pandas_compat.py (counts, percentages, likert_response,
  plotting; deprecation warnings are treated as errors)
- CI: Python 3.9-3.13 with latest pandas, plus pandas 2.1 and 2.2;
  bump actions to checkout@v4 / setup-python@v5

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant