AttributeError module 'statsmodels.formula.api' has no attribute 'OLS'
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This error usually comes from mixing up two different Statsmodels interfaces. The formula API exposes lowercase helper functions such as ols, while the matrix-based API exposes the uppercase OLS class. If you call statsmodels.formula.api.OLS, Python raises an AttributeError because that exact name is not defined there.
Formula API versus matrix API
Statsmodels offers two common ways to fit an ordinary least squares model:
- The formula interface, which looks like
y ~ x1 + x2 - The matrix interface, which takes explicit response and predictor arrays
Use the formula interface when you want R-style formulas and automatic design-matrix creation.
Notice the lowercase ols. That is the correct entry point from statsmodels.formula.api.
When to use uppercase OLS
The uppercase class belongs to the non-formula API. It works with arrays or pandas objects that you prepare manually.
Here the uppercase OLS is correct because the import came from statsmodels.api, not from statsmodels.formula.api.
Why the error happens
This failing pattern is the classic cause:
The module alias is fine, but the attribute name is wrong. The fix is simply:
That lowercase function builds the design matrix behind the scenes. By contrast, sm.OLS expects you to pass y and X separately.
How to choose between them
Use smf.ols if your data is already in a DataFrame and a formula expresses the model clearly. It is concise, readable, and convenient for categorical variables, interactions, and quick experiments.
Use sm.OLS if you need explicit control over the predictor matrix, if the data is already split into arrays, or if you want to manage the intercept yourself. This API is more verbose, but it is very explicit.
Both approaches fit the same kind of model. They differ mainly in how you specify the inputs.
Common Pitfalls
The most common mistake is the uppercase-versus-lowercase mismatch. smf.ols exists, smf.OLS does not.
Another issue appears when switching from the formula API to the matrix API and forgetting the intercept. The formula version includes one automatically unless you remove it from the formula, while sm.OLS requires you to add a constant column yourself with sm.add_constant.
A more subtle problem is shadowing the library with a local file. If your project contains a file named statsmodels.py, Python may import that file instead of the installed package, leading to strange attribute errors. Check your working directory if the obvious fix does not work.
Finally, verify the environment you are actually running. Notebook kernels, virtual environments, and IDE interpreters can point at different installations, which makes debugging package behavior harder than it should be.
Summary
- '
statsmodels.formula.apiexposesols, notOLS.' - Use
smf.ols("y ~ x", data=df)for the formula interface. - Use
sm.OLS(y, X)for the matrix interface fromstatsmodels.api. - Remember to add a constant manually when using
sm.OLS. - If the fix still fails, check for environment or import shadowing problems.

