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

SciPy · Function

stats.shapiro

Performs the Shapiro-Wilk test for normality. It returns the test statistic and p-value, with options for axis-wise computation, NaN handling, and dimension preservation.

How it works

shapiro evaluates whether sample data are consistent with a normal distribution by returning the Shapiro-Wilk statistic and its associated p-value. It can reduce an array along a chosen axis, control NaN handling, and preserve reduced dimensions when requested.

Syntax

scipy.stats.shapiro(x, *, axis=None, nan_policy='propagate', keepdims=False)

Parameters

NameTypeDescription
xarray_likeArray of sample data; it must contain at least three observations.
axisint or None, default NoneAxis along which to compute the statistic; None ravels the input.
nan_policy{'propagate', 'omit', 'raise'}, default 'propagate'NaN behavior: propagate, omit, or raise.
keepdimsbool, default FalseWhether reduced axes remain as dimensions of size one.

Returns

A result containing statistic, the Shapiro-Wilk test statistic, and pvalue, the hypothesis-test p-value.

Key points

  • Use shapiro to assess whether sample observations are compatible with a normal distribution.
  • The null hypothesis is that the data came from a normal distribution.
  • The default axis=None flattens the input before running one test.
  • Choosing an axis produces one statistic and p-value for each slice along that axis.
  • keepdims=True leaves reduced dimensions in the result, which supports broadcasting with the original array.
  • The returned p-value quantifies the test result under the normality null hypothesis.

Common mistakes

  • The input needs at least three observations; shorter samples cannot be tested.
  • For samples larger than 5000, interpret the p-value cautiously even though the W statistic remains accurate.

Example: Test a sample for normality

import numpy as np
from scipy.stats import shapiro

sample = np.array([1.2, 1.8, 2.0, 2.4, 2.7, 3.1])
result = shapiro(sample)
print(result.statistic, result.pvalue)

Runs the normality test on a one-dimensional sample and prints its statistic and p-value.

When to use it

Check a normality assumption

Validate a small numeric sample before applying a method whose assumptions include normality.

Run axis-wise tests

Analyze matrix data by testing each row or column independently.

Handle missing values

Process arrays containing missing observations with an explicit NaN policy.

Interview questions

QWhat does scipy.stats.shapiro test, and what does it return?

It tests the null hypothesis that the data was drawn from a normal distribution. The result contains the test statistic and p-value.

Follow-up: What does a small p-value indicate? It provides evidence against the normality null hypothesis, subject to the chosen significance level.

QHow does axis affect the calculation?

With axis=None, the input is flattened before computation. With an integer axis, the test is computed independently for each slice along that axis.

Follow-up: How can output dimensions be retained? Set keepdims=True so reduced axes remain with length one.

QHow are missing values handled?

nan_policy controls NaNs: propagate returns NaN for affected slices, omit excludes NaNs, and raise raises ValueError when a NaN is present.

Follow-up: When is omit potentially problematic? If omission leaves too few observations, the slice may not provide a valid test.

  • scipy.stats.normaltest
  • scipy.stats.anderson
  • scipy.stats.mstats.normaltest

Source: shapiro — SciPy v1.18.0 Manual. Checked 2026-10-15.