statistics (PyPI backport for Python 2.x)
This PyPI package provides a backport of the Python 3.4 `statistics` module for Python 2.x environments. It offers functions for calculating mathematical statistics of numeric (real-valued) data, mirroring the functionality introduced in Python 3.4's standard library. For Python 3.4 and newer, the `statistics` module is built directly into Python and this PyPI package is not needed and generally should not be installed.
Common errors
-
AttributeError: module 'statistics' has no attribute 'fmean'
cause You have likely installed the PyPI `statistics` backport on a Python 3 environment, which only includes features up to Python 3.4's standard library module. `fmean` was introduced in Python 3.8.fixUninstall the PyPI package: `pip uninstall statistics`. Then, ensure your Python environment is 3.8 or newer and try again; `fmean` will be available from the built-in module. -
ModuleNotFoundError: No module named 'statistics'
cause You are likely running Python 2.x or an older Python 3 version (prior to 3.4) and have not installed the PyPI `statistics` package.fixFor Python 2.x or Python < 3.4, install the PyPI package: `pip install statistics`. For Python 3.4+, this error should not occur, as `statistics` is built-in; verify your Python version and environment. -
TypeError: ('median_grouped() takes at most 2 arguments (3 given)',)cause This error occurs in older versions of `statistics.median_grouped` (like the Python 3.4 implementation in the PyPI backport) when attempting to pass the `interval` argument, which was introduced in Python 3.8. It indicates you're using an older version of the `statistics` module.fixIf on Python 3.8+, uninstall the PyPI `statistics` package (`pip uninstall statistics`) to ensure you're using the built-in, more recent version. If on an older Python version, you may need to manually calculate or upgrade your Python version to access newer features.
Warnings
- breaking The PyPI `statistics` package is a backport specifically for Python 2.x environments, mimicking Python 3.4's built-in module. It has not been updated since 2014 and Python 2.x reached End-of-Life in January 2020. Installing this package on Python 3.4+ is generally incorrect and can lead to unexpected behavior, missing features, or module conflicts, as Python 3.4+ includes the `statistics` module in its standard library.
- gotcha The PyPI `statistics` package only provides features up to Python 3.4's `statistics` module. If installed in a Python 3 environment, it will *shadow* the standard library module, meaning newer functions (e.g., `fmean`, `geometric_mean`, `quantiles` from Python 3.8+; `covariance`, `correlation` from Python 3.10+) will be unavailable or raise `AttributeError`.
- deprecated This package's primary purpose was to provide `statistics` functionality to Python 2.x, which is now an unsupported and End-of-Life version of Python. Using this package means relying on an unmaintained codebase for an unsupported Python version.
Install
-
pip install statistics
Imports
- statistics
import statistics
Quickstart
import statistics
data = [1.5, 2.5, 3.5, 4.5, 5.5]
mean_value = statistics.mean(data)
median_value = statistics.median(data)
mode_value = statistics.mode(data)
stdev_value = statistics.stdev(data)
print(f"Mean: {mean_value}")
print(f"Median: {median_value}")
print(f"Mode: {mode_value}")
print(f"Standard Deviation: {stdev_value}")