Find numeric DataFrame columns with zero variance and undefined correlations

Quick Overview

Write a pandas function that lists the numeric columns of a DataFrame whose Pearson correlations are undefined because the column has zero variance. It tests why zero variance breaks correlation, handling of constant and all-missing columns, and filtering columns by dtype.

Find numeric DataFrame columns with zero variance and undefined correlations

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `undefined_correlation_columns(df)`, which takes a pandas DataFrame and returns an ascending-sorted list of `str` naming every numeric column with zero variance. These are the columns whose Pearson correlations come out as `NaN`. The function must follow these rules: - Non-numeric columns are ignored and never appear in the output. - Include columns that are constant across all their non-null rows. - Include columns that are entirely `NaN`. Example: ```python df = pd.DataFrame({ "a": [1, 2, 3], # has variance "b": [5, 5, 5], # constant -> undefined "c": [np.nan] * 3, # all NaN -> undefined "name": ["x", "y", "z"] # non-numeric -> ignored }) undefined_correlation_columns(df) # -> ["b", "c"] ``` ```hint A cheaper check than corr() Building the full correlation matrix and scanning it for NaN answers a slightly different question, because a NaN there can have more than one cause. Look for a per-column property you can count directly. ``` ### Constraints & Assumptions - "Numeric" means the columns `df.select_dtypes(include="number")` returns: integer and float columns, including the nullable `Int64` and `Float64` dtypes. Bool, string, category and datetime columns are not numeric here. - Column names are converted with `str` before sorting. - The frame can be empty or have zero rows. ### What a Strong Answer Covers - Why zero variance makes the Pearson correlation undefined. - One check that catches both the constant case and the all-NaN case, including columns with a single non-null value. - How non-numeric columns are excluded, and which dtypes count as numeric. - The output format: a sorted list of strings, possibly empty. - The cost of the check compared with building the full correlation matrix. ### Follow-up Questions - How would you detect near-constant columns instead of exactly constant ones? - A column has variance overall, yet its correlation with one other column is still `NaN`. How can that happen? - A bool column is `True` on every row. Should the function report it, and what would you change to include it?

Overview: Write a pandas function that lists the numeric columns of a DataFrame whose Pearson correlations are undefined because the column has zero variance. It tests why zero variance breaks correlation, handling of constant and all-missing columns, and filtering columns by dtype.

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

Write undefined_correlation_columns(df), which takes a pandas DataFrame and returns an ascending-sorted list of str naming every numeric column with zero variance. These are the columns whose Pearson correlations come out as NaN.

The function must follow these rules:

  • Non-numeric columns are ignored and never appear in the output.
  • Include columns that are constant across all their non-null rows.
  • Include columns that are entirely NaN .

Example:

df = pd.DataFrame({
    "a": [1, 2, 3],        # has variance
    "b": [5, 5, 5],        # constant -> undefined
    "c": [np.nan] * 3,     # all NaN -> undefined
    "name": ["x", "y", "z"]  # non-numeric -> ignored
})
undefined_correlation_columns(df)  # -> ["b", "c"]

Constraints & Assumptions

  • "Numeric" means the columns df.select_dtypes(include="number") returns: integer and float columns, including the nullable Int64 and Float64 dtypes. Bool, string, category and datetime columns are not numeric here.
  • Column names are converted with str before sorting.
  • The frame can be empty or have zero rows.

What a Strong Answer Covers Guidance

  • Why zero variance makes the Pearson correlation undefined.
  • One check that catches both the constant case and the all-NaN case, including columns with a single non-null value.
  • How non-numeric columns are excluded, and which dtypes count as numeric.
  • The output format: a sorted list of strings, possibly empty.
  • The cost of the check compared with building the full correlation matrix.

Follow-up Questions Guidance

  • How would you detect near-constant columns instead of exactly constant ones?
  • A column has variance overall, yet its correlation with one other column is still NaN . How can that happen?
  • A bool column is True on every row. Should the function report it, and what would you change to include it?