Statsmodels Library Structure and Imports

Finding an import in Statsmodels starts with the model or helper you want to use. The package groups models, tests and tools into subpackages by subject, while an API module collects selected objects for convenient use.

I’ll show how to locate a subpackage and choose between an API import and a direct path to a module.

TL;DR

Choose a Statsmodels entry point by the job: common models, formulas, time series or one named function.

  • Use statsmodels.api for common cross-sectional models and tools.
  • Use statsmodels.formula.api for supported formula models.
  • Use a domain API or direct module path for time series or a specific object.

What Is the Statsmodels Package Structure?

Statsmodels groups statistical models, tests and tools into subpackages, then offers API modules for common workflows.

A package is an importable collection of Python modules. A subpackage groups modules by subject, such as regression or time series. An API module gathers selected objects for convenient use.

A direct module path identifies where a class or function is defined. An API can expose a short alias for a class whose defining module sits deeper in the package tree.

The phrase “Python statistics module” can also refer to Python’s standard-library statistics module, which calculates summaries such as mean or median. Statsmodels is a separate library for fitting statistical models and related analysis.

Import pathTypical purposeExample
statsmodels.apiCommon cross-sectional modelssm.OLS
statsmodels.formula.apiFormula-based modelssmf.ols
statsmodels.tsa.apiTime-series methods and modelsTime-series API objects
Direct module pathSpecific class or functionstatsmodels.tsa.arima.model.ARIMA

The official API reference describes separate entry points for cross-sectional, time-series and formula workflows. See its current list before assuming a name is exposed through a particular alias.

How to Find and Import a Statsmodels Module

Use the analysis domain to narrow the package family, then select its API or exact module.

Step 1: Import an entry point that fits the workflow

The main API is handy when exploring common classes. Formula models have a separate interface, while time-series work has its own API. I installed Statsmodels 0.15.0 in a workspace virtual environment and imported examples from these paths.

import statsmodels
import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.stats.diagnostic import het_breuschpagan

print(f"statsmodels {statsmodels.__version__}")
print(f"OLS via API: {sm.OLS.__module__}")
print(f"formula API: {smf.ols.__module__}")
print(f"ARIMA direct path: {ARIMA.__module__}")
print(f"diagnostic path: {het_breuschpagan.__module__}")

Run the saved source file with .venv/bin/python structure_demo.py. Its output was:

statsmodels 0.15.0
OLS via API: statsmodels.regression.linear_model
formula API: statsmodels.base.model
ARIMA direct path: statsmodels.tsa.arima.model
diagnostic path: statsmodels.stats.diagnostic
Terminal output showing Statsmodels import paths and defining modules

The OLS alias is exposed through the API but defined in the regression module. ARIMA and the diagnostic helper also report their specific modules. The __module__ attribute therefore helps reveal where an imported name originates.

Step 2: Match a subpackage to the analysis

The module index narrows a broad topic to the package family containing the classes you want.

SubpackageSubjectExample module
regressionLinear models such as OLS, WLS, GLS and GLSARstatsmodels.regression.linear_model
discreteBinary, categorical and count outcomesstatsmodels.discrete.discrete_model
genmodGeneralized linear models and estimating equationsstatsmodels.genmod.generalized_linear_model
tsaTime-series models, tests and helpersstatsmodels.tsa.arima.model
statsStatistical tests and diagnosticsstatsmodels.stats.diagnostic
toolsShared utilitiesstatsmodels.tools.tools
graphicsModel and statistical plotsstatsmodels.graphics.regressionplots

For a linear model, from statsmodels.regression.linear_model import OLS names the family and module directly. The shorter sm.OLS form works when the standard API is already imported.

These names are examples, not a complete inventory. The official module index lists modules, and the API reference lists objects collected into the main interfaces.

Step 3: Use formulas only for supported models

The formula interface describes a model with a string and named data columns. Import statsmodels.formula.api as smf to distinguish its formula functions from classes exposed through sm.

Check the official API reference for model support. AskPython’s formula guide covers syntax and a worked example. This page narrows the import path.

Step 4: Follow a model family to an example

After finding the package family, use a model-focused guide for data arguments and fitting details. AskPython explains Logit and Probit and linear mixed-effects models in separate tutorials.

I use a direct ARIMA path in the example because the printed __module__ value makes its defining location explicit.

The main API and a direct import solve different navigation problems. The API shortens common imports, which helps when a script uses several models from the same interface. A direct path names the module that defines one object, making the dependency clearer when a reader searches the module index or reviews unfamiliar code.

For example, an analyst who needs ordinary least squares can start with the main API. A project that uses a less common diagnostic may need the module index to locate its defining module. The import form should follow the object the analysis needs, not just the shortest spelling.

Formula functions take a different route because the formula string refers to columns by name. That can make a model specification readable when a dataset already has meaningful column labels. Array-based model calls instead pass the response and predictors as objects, which gives the code direct control over how those values are assembled.

Before choosing a formula interface, check that the specific model family supports formulas. The formula API is a collection of interfaces, not a guarantee that every class has a formula counterpart. The API reference is the place to confirm availability.

Time-series analysis has its own families because observations are ordered in time and model options depend on that structure. A forecasting model may accept order parameters or seasonal settings that do not appear in a basic cross-sectional regression. Start from the time-series guide, then follow its model-specific reference to the import path.

Subpackage names also help when the desired object is known but its import is not. Regression contains linear-model classes, discrete covers models for categorical outcomes, and generalized models address outcome distributions beyond the ordinary linear case. Diagnostics and graphics are separate because they support checking and explaining fitted results rather than specifying the model itself.

A useful lookup sequence is to identify the analysis, search the official module index for its family, and then open the API reference for the object. Confirm the exact capitalization because Python imports are case-sensitive. Copy the documented path into a small import check before building the rest of a script around it.

When a project has an established import style, consistency helps readers recognize where its models come from. A shared API alias can be sensible across a file that uses several standard models. A direct import is clearer to scan when only one specialized class is needed.

A short alias can conceal the defining module during code review. A long path exposes it. Neither form changes the statistical model.

Import Limits and Common Missteps

Importing the root package does not guarantee its nested APIs have been imported as attributes.

In version 0.15.0, I checked the root package alone. Both api and tsa attributes were absent. Import the documented API or exact submodule instead of relying on import order.

SymptomLikely causeNext check
AttributeError after only import statsmodelsNested namespace not importedImport statsmodels.api or the needed module
Name absent from smCurated API omits some public objectsSearch the module index and import directly
ols absent from smFormula interface used on standard APIImport statsmodels.formula.api
Old example fails at importExample may use an older path or versionCheck installed version against current stable docs

A report about an unavailable diagnostic attribute illustrates namespace confusion. Treat issue threads as reports of a problem, then verify the fix against the current module index.

The phrase “how to import statistics module in python” may refer to Python’s standard-library package. Its import is import statistics, which does not provide Statsmodels model classes.

For time-series imports, the current documentation says that some classes and functions must be imported from their corresponding modules. Check the time-series guide for the specific model family rather than assuming every name appears at one namespace level.

Direct paths make dependencies visible but can be longer to type. The curated API offers shorter names, while a specific submodule makes the source of an import unambiguous. Follow the convention already used by a project unless the documented path does not provide the object you need.

A missing name can also point to a version mismatch rather than a wrong concept. Check the installed package version before copying a path from an older post, then compare the name with the stable reference for that version.

Conclusion

Choose the import interface by workflow, and use a direct path when you need one precisely named object.

For current names and availability, consult the official Import Paths and Structure guide, API reference and module index.

FAQ

These answers distinguish Statsmodels import choices from similar Python package names.

Should I use statsmodels.api or a direct import?

Use statsmodels.api for convenient access to common objects. Use a direct import when you know the module path or need an object not exposed by that API.

Where do Statsmodels formula functions live?

Import statsmodels.formula.api, commonly as smf. It provides formula interfaces for supported models.

Is Statsmodels the same as Python’s statistics module?

No. Python’s standard-library statistics module provides functions such as mean and median. Statsmodels is a separate library for statistical models and related analysis.

Ninad
Ninad

A Python and PHP developer turned writer out of passion. Over the last 6+ years, he has written for brands including DigitalOcean, DreamHost, Hostinger, and many others. When not working, you'll find him tinkering with open-source projects, vibe coding, or on a mountain trail, completely disconnected from tech.

Articles: 136