statsmodels.tools.tools.add_constant

statsmodels.tools.tools.add_constant(data, prepend=True, has_constant='skip')[source]

Add a column of ones to an array.

Parameters:
dataarray_like

A column-ordered design matrix.

prependbool

If true, the constant is in the first column. Else the constant is appended (last column).

has_constantstr {‘raise’, ‘add’, ‘skip’}

Behavior if data already has a constant. The default will return data without adding another constant. If ‘raise’, will raise an error if any column has a constant value. Using ‘add’ will add a column of 1s if a constant column is present.

Returns:
array_like

The original values with a constant (column of ones) as the first or last column. Returned value type depends on input type.

Notes

When the input is a pandas Series or DataFrame, the added column’s name is ‘const’.


Last update: Nov 14, 2024