Add a Bonus to Salaries Above a Threshold Without Mutating the DataFrame

Quick Overview

Write a pandas function that adds a bonus to every salary above a threshold and returns a new DataFrame while leaving the caller's frame untouched. It tests boolean masks with .loc, avoiding chained indexing and SettingWithCopyWarning, and keeping the index, column order and dtype stable.

Add a Bonus to Salaries Above a Threshold Without Mutating the DataFrame

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `bonus_high_earners(df, threshold, bonus)` that returns a new DataFrame where each row with `'salary'` above `threshold` has its salary increased by `bonus`. All other rows are unchanged. Requirements: - Return a new frame: the caller's `df` must be unchanged. - Increase `'salary'` by `bonus` only where `salary > threshold`. - Use one `.loc[mask, 'salary']` assignment on a copy (no chained indexing, no `SettingWithCopyWarning`). - Preserve the index, column order, and the `'salary'` dtype. Example: ```python df = pd.DataFrame({'name': ['A','B','C'], 'salary': [90, 110, 130]}) bonus_high_earners(df, threshold=100, bonus=10) # name salary # 0 A 90 # 1 B 120 # 2 C 140 # df is unchanged ``` ```hint Watch the dtype Think about what happens to an integer `salary` column when the value you add to it is not a whole number. ``` ### Constraints & Assumptions - `df` has a numeric `'salary'` column and may have any other columns, which must come back unchanged and in the same order. - The comparison is strict: a salary equal to `threshold` gets no bonus. - The index can hold any labels, and the output must keep it unchanged. ### What a Strong Answer Covers - How the caller's frame is protected from changes. - Selecting the rows and the column in a single indexing operation, and why chained indexing is unreliable. - The strict comparison against the threshold and what happens at the boundary and on missing salaries. - When the `'salary'` dtype could change, and how to keep it stable. ### Follow-up Questions - Why can chained indexing like `df[mask]['salary'] = ...` trigger `SettingWithCopyWarning`? - How could the salary dtype accidentally change, and how do you keep it stable? - How would you write the same update without any in-place assignment?

Overview: Write a pandas function that adds a bonus to every salary above a threshold and returns a new DataFrame while leaving the caller's frame untouched. It tests boolean masks with .loc, avoiding chained indexing and SettingWithCopyWarning, and keeping the index, column order and dtype stable.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement bonus_high_earners(df, threshold, bonus) that returns a new DataFrame where each row with 'salary' above threshold has its salary increased by bonus. All other rows are unchanged.

Requirements:

  • Return a new frame: the caller's df must be unchanged.
  • Increase 'salary' by bonus only where salary > threshold .
  • Use one .loc[mask, 'salary'] assignment on a copy (no chained indexing, no SettingWithCopyWarning ).
  • Preserve the index, column order, and the 'salary' dtype.

Example:

df = pd.DataFrame({'name': ['A','B','C'], 'salary': [90, 110, 130]})
bonus_high_earners(df, threshold=100, bonus=10)
#   name  salary
# 0    A      90
# 1    B     120
# 2    C     140
# df is unchanged

Constraints & Assumptions

  • df has a numeric 'salary' column and may have any other columns, which must come back unchanged and in the same order.
  • The comparison is strict: a salary equal to threshold gets no bonus.
  • The index can hold any labels, and the output must keep it unchanged.

What a Strong Answer Covers Guidance

  • How the caller's frame is protected from changes.
  • Selecting the rows and the column in a single indexing operation, and why chained indexing is unreliable.
  • The strict comparison against the threshold and what happens at the boundary and on missing salaries.
  • When the 'salary' dtype could change, and how to keep it stable.

Follow-up Questions Guidance

  • Why can chained indexing like df[mask]['salary'] = ... trigger SettingWithCopyWarning ?
  • How could the salary dtype accidentally change, and how do you keep it stable?
  • How would you write the same update without any in-place assignment?