Clip a pandas DataFrame column to inclusive lower and upper bounds

Quick Overview

Write a pandas function that caps a numeric column to an inclusive lower and upper bound while keeping every row, the index and the column name. It tests vectorized pandas operations and careful handling of missing values and boundary cases.

Clip a pandas DataFrame column to inclusive lower and upper bounds

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `clip_series(df, col, low, high)`, which caps the numeric column `col` to the inclusive range `[low, high]`. Values below `low` become `low`, values above `high` become `high`, and all other values are unchanged. The function must meet these requirements: - Return a `pd.Series` with the same index and name as `df[col]`. - Keep every row: no dropping, filtering or reordering. - The returned values must match the expected values to floating-point tolerance (`numpy.testing.assert_allclose` on `.values`). Example: ```python df = pd.DataFrame({"x": [1.0, 5.0, 10.0]}) clip_series(df, "x", 2.0, 8.0) # 0 2.0 # 1 5.0 # 2 8.0 # Name: x, dtype: float64 ``` ```hint One vectorized call Look for a pandas method that bounds every value at once, then check what it does with missing values. ``` ### Constraints & Assumptions - `low <= high`. - `col` is a numeric column of `df`. ### What a Strong Answer Covers - A vectorized approach that returns a new Series with the original index and name and leaves `df` unchanged. - Correct treatment of values exactly equal to a bound, since the range is inclusive. - What happens to missing values in the column. - Why a hand-written mask or a Python loop is slower and easier to get wrong. ### Follow-up Questions - How would you pick `low` and `high` from the data, for example the 1st and 99th percentiles, and on which data split should you compute them? - When is capping outliers a better choice than dropping them, and how does capping change the mean and the variance? - How would you clip each group to its own bounds, such as per-country percentiles? - What should the function do if a caller passes `low > high`?

Overview: Write a pandas function that caps a numeric column to an inclusive lower and upper bound while keeping every row, the index and the column name. It tests vectorized pandas operations and careful handling of missing values and boundary cases.

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

Implement clip_series(df, col, low, high), which caps the numeric column col to the inclusive range [low, high]. Values below low become low, values above high become high, and all other values are unchanged.

The function must meet these requirements:

  • Return a pd.Series with the same index and name as df[col] .
  • Keep every row: no dropping, filtering or reordering.
  • The returned values must match the expected values to floating-point tolerance ( numpy.testing.assert_allclose on .values ).

Example:

df = pd.DataFrame({"x": [1.0, 5.0, 10.0]})
clip_series(df, "x", 2.0, 8.0)
# 0    2.0
# 1    5.0
# 2    8.0
# Name: x, dtype: float64

Constraints & Assumptions

  • low <= high .
  • col is a numeric column of df .

What a Strong Answer Covers Guidance

  • A vectorized approach that returns a new Series with the original index and name and leaves df unchanged.
  • Correct treatment of values exactly equal to a bound, since the range is inclusive.
  • What happens to missing values in the column.
  • Why a hand-written mask or a Python loop is slower and easier to get wrong.

Follow-up Questions Guidance

  • How would you pick low and high from the data, for example the 1st and 99th percentiles, and on which data split should you compute them?
  • When is capping outliers a better choice than dropping them, and how does capping change the mean and the variance?
  • How would you clip each group to its own bounds, such as per-country percentiles?
  • What should the function do if a caller passes low > high ?