Return OLS coefficient p-values as a named pandas Series with statsmodels

Quick Overview

Write a pandas and statsmodels function that regresses one column on all the others with an intercept and returns the two-sided coefficient p-values as a named Series. Tests careful handling of column order, the intercept label and how OLS p-values behave when predictors are collinear.

Return OLS coefficient p-values as a named pandas Series with statsmodels

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `coefficient_pvalue_series(df, target)`: regress column `target` on all other columns of `df` (in existing order) via OLS with an intercept, and return a `pd.Series` of two-sided p-values per coefficient. `sm.OLS(...).fit().pvalues` on `sm.add_constant` predictors yields the required index (prepends `'const'`). The result must meet two requirements: - The index must be `['const', <predictors in df order>]`. - The values are float p-values, and the Series name is `'pvalue'`. ```python # df columns: ['x1', 'x2', 'x3', 'y'], target='y' out = coefficient_pvalue_series(df, 'y') out.index # Index(['const', 'x1', 'x2', 'x3']) out.name # 'pvalue' ``` ```hint Compare every attribute `assert_series_equal` checks the name as well as the values and index. Look at what name the Series from `.pvalues` carries before you return it. ``` ### Constraints & Assumptions - `df` holds the target column plus numeric predictor columns, and every column other than `target` is a predictor. - The output is checked on a fixed 20-row DataFrame with `pandas.testing.assert_series_equal` at `rtol=1e-8`, comparing index, name and values. ### What a Strong Answer Covers - Separating the target from the predictors while keeping the predictors in their original left-to-right order. - Adding the intercept so that it is labeled `'const'` and comes first in the index. - Fitting OLS and returning the two-sided p-values renamed to `'pvalue'`. - How the p-values come from the coefficient t-statistics and the residual degrees of freedom. - Edge cases such as a predictor that is already constant, non-numeric columns and collinear predictors. ### Follow-up Questions - How would you add BH/FDR-adjusted p-values alongside the raw ones? - Why can a p-value be unreliable when predictors are collinear? - How would you report p-values based on heteroskedasticity-robust standard errors?

Overview: Write a pandas and statsmodels function that regresses one column on all the others with an intercept and returns the two-sided coefficient p-values as a named Series. Tests careful handling of column order, the intercept label and how OLS p-values behave when predictors are collinear.

General logo
General
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement coefficient_pvalue_series(df, target): regress column target on all other columns of df (in existing order) via OLS with an intercept, and return a pd.Series of two-sided p-values per coefficient. sm.OLS(...).fit().pvalues on sm.add_constant predictors yields the required index (prepends 'const').

The result must meet two requirements:

  • The index must be ['const', <predictors in df order>] .
  • The values are float p-values, and the Series name is 'pvalue' .
# df columns: ['x1', 'x2', 'x3', 'y'], target='y'
out = coefficient_pvalue_series(df, 'y')
out.index  # Index(['const', 'x1', 'x2', 'x3'])
out.name   # 'pvalue'

Constraints & Assumptions

  • df holds the target column plus numeric predictor columns, and every column other than target is a predictor.
  • The output is checked on a fixed 20-row DataFrame with pandas.testing.assert_series_equal at rtol=1e-8 , comparing index, name and values.

What a Strong Answer Covers Guidance

  • Separating the target from the predictors while keeping the predictors in their original left-to-right order.
  • Adding the intercept so that it is labeled 'const' and comes first in the index.
  • Fitting OLS and returning the two-sided p-values renamed to 'pvalue' .
  • How the p-values come from the coefficient t-statistics and the residual degrees of freedom.
  • Edge cases such as a predictor that is already constant, non-numeric columns and collinear predictors.

Follow-up Questions Guidance

  • How would you add BH/FDR-adjusted p-values alongside the raw ones?
  • Why can a p-value be unreliable when predictors are collinear?
  • How would you report p-values based on heteroskedasticity-robust standard errors?