Skip to main content
v2026.11,986 entries · CC-BY 4.0

PySAL: Python Spatial Analysis Library, Packages and Moran’s I Example

PySAL is an open-source Python spatial data science library. See its packages by layer, install steps, a checked Moran’s I example and how to cite it.

Written and maintained by CASRAI Editorial Board

Published

On this page

PySAL (the Python Spatial Analysis Library) is an open-source collection of Python packages for spatial data science: it builds spatial weights, tests whether values cluster in space, fits spatial regression models and classifies values for maps. Since version 2.0.0 it is a meta-package that installs a family of affiliated packages rather than one monolithic library, and it supports Python 3 only. This guide maps those packages by layer, shows how to install the current release, walks through a Moran’s I calculation with the arithmetic checked, and explains how to cite the library.

What PySAL is and who uses it

The project’s own manual describes PySAL as “a collection of affiliated geographic data science packages” and groups the work into four areas: explore (exploratory analysis of spatial and spatio-temporal data), viz (visualising patterns, clusters, outliers and hot spots), model (linear, generalised linear, generalised additive and nonlinear spatial models) and lib (the foundation: graph construction, spatial weights, alpha shapes, spatial indices and input/output). It is released under a BSD 3-Clause license, so it can be used inside funded projects, institutional pipelines and teaching material without a licensing fee.

Researchers in geography, planning, public health, criminology, ecology and the social sciences reach for it when a question has a place component: do high values sit next to high values, is a relationship between two variables the same everywhere, how segregated is a city, how unequal is a region. In a research-methods workflow it sits alongside a desktop tool such as GeoDa, which offers a point-and-click route to the same family of exploratory spatial methods, and below a mapping layer described in the guide to geovisualization.

PySAL packages by layer

The project’s GitHub README lists the packages that make up the meta-package, grouped by the same four areas as the manual. The data table below compares each package’s layer, its latest PyPI release and the minimum Python version it declares. Reading it by layer gives the practical order of use: start in lib, analyse in explore, estimate in model, display in viz.

  • lib: libpysal holds the spatial weights classes and the readers and example datasets that every other package builds on.
  • explore: esda, giddy, inequality, momepy, pointpats, segregation and spaghetti. The esda package, used in the worked example below, is where global and local spatial autocorrelation statistics live.
  • model: mgwr, spglm, spint, spreg, tobler, access, spopt and spml.
  • viz: mapclassify and splot.

Because the packages are versioned independently, a project should record the version of each package it imports, not only the version of the pysal meta-package. The table shows why: esda and libpysal carry their own release numbers, and not every package declares the same minimum Python version.

How to install PySAL and which Python version it needs

The README gives two routes: conda install pysal from the conda-forge channel, or pip install pysal. On PyPI the current meta-package release is 26.7, uploaded on 31 July 2026, and it declares requires_python >=3.12; the esda (2.10.0) and libpysal (4.15.1) releases carry the same minimum. A researcher on an older interpreter therefore needs a newer environment, or an earlier release pinned in a lockfile.

Python 2 support ended with the 1.x line: the README states that since 2.0.0 PySAL supports Python 3 only and that Python 2 users can install version 1.14.3. Scripts written against the old single-module layout, such as pysal.weights or pysal.esda, generally need their imports changed to the new packages (libpysal.weights, esda).

Worked example: Moran’s I with esda

Moran’s I is the standard global test for spatial autocorrelation, and the esda documentation illustrates it with example data shipped in libpysal. The class signature is Moran(y, w, transformation='r', permutations=999, two_tailed=True), where y is the variable, w a spatial weights object, 'r' row-standardises the weights, and permutations=999 sets the number of random permutations behind the simulated p-value.

import numpy as np
import libpysal
import esda

w = libpysal.io.open(libpysal.examples.get_path("stl.gal")).read()
f = libpysal.io.open(libpysal.examples.get_path("stl_hom.txt"))
y = np.array(f.by_col["HR8893"])
mi = esda.moran.Moran(y, w)
print(mi.I, mi.EI, mi.p_norm)

The documentation reports, for the homicide-rate variable HR8893 on the St. Louis-area contiguity weights, I = 0.244, an expected value EI = −0.012987 and a normal-approximation p-value p_norm = 0.000271. A second documentation example, the SIDR74 variable in the sids2 data, gives I = 0.248 and p_norm = 0.000116.

The expected value can be checked by hand. Under no spatial autocorrelation, E[I] = −1 / (n − 1). Setting −1 / (n − 1) = −0.012987 gives n − 1 = 1 / 0.012987 ≈ 77.0, so the example has n = 78 observations, and −1/77 = −0.012987 matches the reported value. The observed I = 0.244 is therefore about 0.257 above its expectation (0.244 − (−0.012987) = 0.256987), which is what the small p-value of 0.000271 flags as unlikely under randomness. The attributes p_sim and z_sim hold the permutation-based p-value and z-score for the same test. For the statistic’s formula and a hand calculation, see the separate guide to Moran’s I.

Reading the output

Result Reading
I clearly above E[I] with a small p-value Similar values tend to be neighbours (positive spatial autocorrelation), as in the 0.244 example
I near E[I] (about −1/(n−1)) No evidence of spatial pattern in this variable under these weights
I clearly below E[I] with a small p-value Dissimilar values tend to be neighbours (negative spatial autocorrelation)
p_norm versus p_sim disagree The normal approximation and the permutation test give different answers; report the permutation value and its number of permutations

Assumptions and common mistakes

The result of any PySAL autocorrelation test depends on the weights object passed in, so the weights are part of the method, not a technical detail. Choosing contiguity versus distance-based neighbours, or row-standardised versus binary weights, can change I. State the weights type in the methods section and keep the code that built them.

  • Treating I as a correlation coefficient. Its expectation under randomness is −1/(n−1), not zero, and it is interpreted against that benchmark.
  • Reporting a p-value without saying which one. p_norm and p_sim are different procedures; name the one you report and the permutation count.
  • Ignoring islands. Observations with no neighbours in the weights can distort or block results; inspect the weights object before testing.
  • Mixing coordinate systems. Distance-based weights assume sensible projected coordinates; see the guide to pyproj for coordinate transformations.
  • Importing old module paths. Code written for PySAL 1.x will not run unchanged on the current release.
  • Not recording versions. Package versions differ, as the data table shows; record all of them.

Reporting a PySAL analysis so others can reproduce it

Journals and funders increasingly expect computational methods to be rerunnable, and a spatial analysis has more moving parts than most because the weights, the data projection and the software versions all shape the result. A reproducible PySAL methods paragraph states five things:

  1. the dataset and its coordinate reference system, including any reprojection applied before distances were computed;
  2. how spatial weights were built (contiguity or distance, the neighbour definition, whether they were row-standardised) and how islands were handled;
  3. the statistic and the package that provides it, for example Moran’s I from esda;
  4. the inference method, whether the normal approximation or a permutation test, with the number of permutations;
  5. the version of every package imported, from pip list or a conda environment export, deposited with the code in a repository.

Depositing the script and environment file next to the data lets a reviewer reproduce I, EI and the p-value exactly, which is the same standard applied to the documentation example above.

PySAL compared with GeoDa

PySAL and GeoDa overlap in exploratory spatial data analysis. The practical difference is the interface: PySAL is a scripted library, so the whole analysis, from weights construction to the statistic, can live in version-controlled code that others can rerun, while GeoDa is a desktop application suited to interactive exploration. Many projects explore interactively first and then script the final analysis for reproducibility.

How to cite PySAL

The manual’s recommended citation is Rey and Anselin (2007). In APA 7th style:

Rey, S. J., & Anselin, L. (2007). PySAL: A Python library of spatial analytical methods. Review of Regional Studies, 37(1), 5–27. https://doi.org/10.52324/001c.8285

In the text, cite as (Rey & Anselin, 2007). Also state the version of each package used (for example esda 2.10.0, libpysal 4.15.1) and the date of the installation, since the analysis can depend on them.

Sources

Data: PySAL meta-package components by layer, latest PyPI release and minimum Python version

The packages listed in the PySAL GitHub README grouped by layer (lib, explore, model, viz), with each package’s latest PyPI release number and declared requires_python value read from the PyPI JSON API on 2026-11-17.

PySAL meta-package components by layer, latest PyPI release and minimum Python version
PackageLayerLatest PyPI versionDeclared Python requirement
libpysallib4.15.1>=3.12
esdaexplore2.10.0>=3.12
giddyexplore2.3.9>=3.12
inequalityexplore1.2.0>=3.12
momepyexplore1.0.0>=3.12
pointpatsexplore2.6.0>=3.12
segregationexplore2.6.0>=3.12
spaghettiexplore1.7.6>=3.10
mgwrmodel2.2.1>3.5
spglmmodel1.1.0>=3.9
spintmodel1.1.0>=3.12
spregmodel1.9.2>=3.12
toblermodel0.14.0>=3.12
accessmodel1.1.10.post3>=3.11
spoptmodel0.8.0>=3.12
spmlmodel0.2.2>=3.12
mapclassifyviz2.11.0>=3.12
splotviz1.1.7

Sources

Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · full access

Ask about PySAL: Python Spatial Analysis Library, Packages and Moran’s I Example

Full access: large monthly usage, up to 10 source passages behind each answer, and MCP and API access for Claude Code, Codex and other CLIs. Cancel any time.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →