Skip to content

Commit ce15502

Browse files
committed
Switch away from deprecated Pandas APIs
There are two APIs the library relies on, which are deprecated as of Pandas 2.1.0: 1. Series.__getitem__ treating keys as positions (should now use series.iloc[n] instead of series[n]) 2. DataFrame.applymap (should now use df.map instead) This commit updates the usage of those APIs but attempts to preserve backwards compatibility by reverting to the old behavior if something breaks.
1 parent b9257e6 commit ce15502

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

plot_likert/plot_likert.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,10 @@ def validate(value):
254254
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)."
255255
)
256256

257-
df.applymap(validate)
257+
try:
258+
df.map(validate)
259+
except AttributeError: # for compatibility with Pandas < 2.1.0
260+
df.applymap(validate)
258261

259262
# fix long questions for printing
260263
old_labels = list(df)
@@ -289,14 +292,19 @@ def likert_percentages(
289292
# Warn if the rows have different counts
290293
# If they do, the percentages shouldn't be compared.
291294
responses_per_question = counts.sum(axis=1)
292-
responses_to_first_question = responses_per_question[0]
295+
responses_to_first_question = responses_per_question.iloc[0]
293296
responses_same = responses_per_question == responses_to_first_question
294297
if not responses_same.all():
295298
warn(
296299
"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."
297300
)
298301

299-
return counts.apply(lambda row: row / row.sum(), axis=1).applymap(lambda v: 100 * v)
302+
try:
303+
return counts.apply(lambda row: row / row.sum(), axis=1).map(lambda v: 100 * v)
304+
except AttributeError: # for compatibility with Pandas < 2.1.0
305+
return counts.apply(lambda row: row / row.sum(), axis=1).applymap(
306+
lambda v: 100 * v
307+
)
300308

301309

302310
def _compute_counts_percentage(counts: pd.DataFrame) -> pd.DataFrame:
@@ -307,7 +315,7 @@ def _compute_counts_percentage(counts: pd.DataFrame) -> pd.DataFrame:
307315
# Warn if the rows have different counts
308316
# If they do, the percentages shouldn't be compared.
309317
responses_per_question = counts.sum(axis="columns")
310-
responses_to_first_question = responses_per_question[0]
318+
responses_to_first_question = responses_per_question.iloc[0]
311319
responses_same = responses_per_question == responses_to_first_question
312320
if not responses_same.all():
313321
warn(
@@ -323,7 +331,10 @@ def likert_response(df: pd.DataFrame, scale: Scale) -> pd.DataFrame:
323331
orginal data.
324332
"""
325333
for i in range(0, len(scale)):
326-
df = df.applymap(lambda x: scale[i] if str(i) in x else x)
334+
try:
335+
df = df.map(lambda x: scale[i] if str(i) in x else x)
336+
except AttributeError: # for compatibility with Pandas < 2.1.0
337+
df = df.map(lambda x: scale[i] if str(i) in x else x)
327338
return df
328339

329340

0 commit comments

Comments
 (0)