Registry / data / lifelines

lifelines

JSON →
library0.30.3pypypi✓ verified 24d ago

lifelines is a comprehensive Python library for survival analysis, offering implementations of various models including Kaplan-Meier, Nelson-Aalen, and Cox proportional hazards regression. It is actively maintained with regular releases, providing tools for handling right, left, and interval censored data, and includes internal plotting methods for easy visualization. The current version is 0.30.3.

pip install lifelines
INSTALL
IMPORT
SIG · LIFELINES
L
lifelines
datapythonv0.30.3
Install
18.3s avg
Import
3563ms
Disk
413MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.30.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 3.600s · 405.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 18.3s · import 3.526s · 388MB
413MB installed
● package 413MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

KaplanMeierFitter
from lifelines import KaplanMeierFitter
CoxPHFitter
from lifelines import CoxPHFitter
WeibullFitter
from lifelines import WeibullFitter
NelsonAalenFitter
from lifelines import NelsonAalenFitter

This quickstart demonstrates fitting a Kaplan-Meier survival model and a Cox Proportional Hazards regression model. It generates synthetic duration and event data, then visualizes the Kaplan-Meier survival curve and prints a summary for the Cox model.

import numpy as np import pandas as pd import matplotlib.pyplot as plt from lifelines import KaplanMeierFitter, CoxPHFitter # --- Kaplan-Meier Fitter Example --- np.random.seed(42) num_samples = 100 T = np.random.exponential(scale=10, size=num_samples) # Durations E = np.random.binomial(n=1, p=0.7, size=num_samples) # Events (0=censored, 1=event) kmf = KaplanMeierFitter() kmf.fit(T, event_observed=E, label="Sample Survival Function") print("Kaplan-Meier Survival Function (first 5 rows):\n", kmf.survival_function_.head()) kmf.plot_survival_function() plt.title("Kaplan-Meier Survival Estimate") plt.xlabel("Time") plt.ylabel("Survival Probability") plt.grid(True) plt.show() # --- Cox Proportional Hazards Fitter Example --- data = pd.DataFrame({ 'T': T, # Duration 'E': E, # Event observed 'age': np.random.randint(20, 70, num_samples), 'sex': np.random.choice(, num_samples, p=[0.55, 0.45]) }) cph = CoxPHFitter() cph.fit(data, duration_col='T', event_col='E', formula="age + sex") print("\nCoxPHFitter Summary:\n") cph.print_summary()
Debug
Known issues
breakingThe `sklean_adaptor` module was removed in version 0.28.0. There is no direct replacement, simplifying the library's API.
fix
Remove any dependencies on `sklean_adaptor`. If scikit-learn compatibility is needed, consider `scikit-survival` or manual integration.
affects: >=0.28.0
breakingMinimum Python version requirements have increased. Version 0.28.0 dropped support for Python < 3.9, and version 0.30.3 requires Python >= 3.11.
fix
Ensure your Python environment is running version 3.11 or higher to use the latest `lifelines` releases.
affects: >=0.28.0, >=0.30.3
deprecatedThe `initial_beta` parameter in `CoxPHFitter.fit()` was renamed to `initial_point` to align with a more general concept across models.
fix
Update calls from `initial_beta=...` to `initial_point=...` when fitting CoxPH models.
affects: >=0.27.8
deprecatedThe `plot_covariate_groups` method was renamed to `plot_partial_effects_on_outcome` and its behavior for transformed variables changed with the introduction of R-like formulas.
fix
Use `plot_partial_effects_on_outcome` instead. Review the documentation for its usage, especially with formula-based models, as its behavior for transformed variables is different.
affects: Prior to ~0.27.0
gotchaWhen using `CoxPHFitter`, avoid including a column of all 1s (an explicit intercept) in your DataFrame or formula. The Cox model implicitly handles a baseline, and an explicit intercept can lead to warnings or convergence errors.
fix
Do not manually add an intercept column. `lifelines` handles the baseline hazard implicitly. For example, if using R-like formulas, simply list covariates without adding a `1`.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'lifelines'
The 'lifelines' library is not installed in the Python environment being used, or the environment in which it was installed is not active, particularly common in Jupyter notebooks with multiple kernels.
fix
Install the library using pip: `pip install lifelines` or `conda install -c conda-forge lifelines` if using Anaconda.
ValueError: This model does not allow for non-positive durations. Suggestion: add a small positive value to zero elements.
Survival analysis models in `lifelines` typically require positive durations, but the provided 'duration_col' contains zero or negative values.
fix
Ensure all values in your duration column are strictly positive. If there are zeros, add a small positive value (e.g., a tiny epsilon) to them, or filter out non-positive durations if they are not meaningful for your analysis.
ConvergenceError: Convergence halted due to matrix inversion problems. Suspicion is high `collinearity`.
This error in `CoxPHFitter` indicates issues during the iterative optimization process (Newton-Raphson), often due to high collinearity among covariates in the dataset, perfectly separated data, or a small dataset size when using cross-validation.
fix
To fix this, identify and remove highly correlated features, ensure there isn't perfect separation in your data, try adding a penalizer to the model (e.g., `CoxPHFitter(penalizer=0.1)`), or if using cross-validation, increase the `cv` folds or ensure sufficient data in each fold.
KeyError: "None of [Index(['At risk', 'Censored', 'Events'], dtype='object')] are in the [index]"
This error typically occurs when using `lifelines.plotting.add_at_risk_counts()` with an incompatible version of the Pandas library, where column names or indexing behavior have changed.
fix
Upgrade your Pandas library to a version compatible with your `lifelines` installation, or downgrade it if a specific older version of `lifelines` requires it. Checking the `lifelines` documentation for recommended Pandas versions is also helpful.
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
This Pandas-related `ValueError` happens when a `pandas.Series` object is used in a boolean context (e.g., in an `if` statement or as a direct argument) where a single boolean value or a scalar is expected, often when passing Series directly as arguments to `lifelines` fit methods without extracting their underlying values.
fix
When passing duration or event columns to `lifelines` fitters, extract the underlying NumPy array using `.values` or convert to a list, e.g., `kmf.fit(T.values, event_observed=E.values)` instead of `kmf.fit(T, event_observed=E)`.
Upgrade
Version history
0.30.3latest on PyPI · released Mar 5, 2026
Audit
Dependencies
numpyrequiredFundamental for numerical operations and data handling.
pandasrequiredCore for data structures (DataFrames) used throughout the library.
scipyrequiredUsed for scientific computing, including statistical functions.
matplotlibrequiredEssential for built-in plotting functionalities.
autograd-gammarequiredUsed for automatic differentiation in some models.
Agent activity
8 hits · last 30 days
node
6
Resources
lifelines — pip install lifelines · libregistry