Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Module

df.cov

Computes pairwise covariance among DataFrame columns and returns the results as a covariance matrix. Missing values are excluded, and min_periods can require a minimum number of observations per pair.

How it works

Call DataFrame.cov to create a covariance matrix describing pairwise covariance among DataFrame columns. Missing values are excluded pairwise, and min_periods can require enough observations before a result is produced.

Syntax

DataFrame.cov(min_periods=None, ddof=1, numeric_only=False)

Parameters

NameTypeDescription
min_periodsint, optionalMinimum number of observations required per pair of columns to have a valid result.
ddofint, default 1Delta degrees of freedom; the divisor is N - ddof.
numeric_onlybool, default FalseWhether to include only float, int, or boolean data.

Returns

DataFrame: the covariance matrix of the DataFrame's series.

Key points

  • The method calculates covariance for every eligible pair of columns and omits missing observations.
  • The result is a square DataFrame indexed and labeled by the source columns.
  • Use min_periods to impose a minimum valid-observation requirement independently for each pair.
  • min_periods accepts an integer specifying the required observation count for each pair.
  • ddof selects the degrees-of-freedom adjustment used in the covariance divisor.
  • Set numeric_only to restrict the calculation to numeric and boolean columns.

Common mistakes

  • The ddof parameter's documented limitation applies when the DataFrame contains NaN values.
  • Pairs with fewer than min_periods valid observations produce NaN rather than a covariance value.
  • Changing ddof changes the divisor, so it changes the scale of the covariance values.

Example: Compute a numeric covariance matrix

import pandas as pd

sales = pd.DataFrame({
    "online": [10, 12, 9, 15],
    "store": [8, 11, 10, 13],
    "region": ["N", "N", "S", "S"],
})

covariance = sales.cov(numeric_only=True)
print(covariance)

Computes the covariance matrix for the numeric columns while excluding the text column.

When to use it

Measure pairwise co-movement

Assess how numeric measures vary together in exploratory or time-series analysis.

Enforce an observation threshold

Prevent sparse column pairs from producing misleading estimates.

Select numeric inputs

Exclude labels or other nonnumeric columns from the matrix.

Interview questions

QWhat does DataFrame.cov return?

DataFrame.cov returns a DataFrame whose entries are pairwise covariances between the original columns. The calculation excludes NA and null values.

Follow-up: How can insufficient data be handled? Set min_periods so each pair needs a required number of observations; pairs below that threshold return NaN.

QWhat is min_periods used for?

min_periods specifies the minimum number of observations required for each column pair to receive a valid result. It is useful when pairwise missing data could otherwise leave too few observations.

Follow-up: What happens when the threshold is not met? The corresponding matrix entry is NaN.

QHow do ddof and numeric_only affect the calculation?

ddof is the delta degrees of freedom, and pandas divides by N - ddof. numeric_only controls whether only float, integer, and boolean columns are included.

Follow-up: What default should be remembered? ddof defaults to 1, while numeric_only defaults to False in the current documentation.

  • pandas.Series.cov
  • pandas.DataFrame.corr

Source: pandas.DataFrame.cov — pandas 3.0.6 documentation. Checked 2026-10-15.