Skip to main content

GeoThai python-library

Every province, district, subdistrict and postal code of Thailand, in one typed Python package with search.

👉 Website · 👉 Data and changelog

Features

  • 77 provinces, 928 districts and 7,436 subdistricts with Thai and English names and DOPA codes
  • Postal codes per subdistrict, and a lookup from a postal code to its subdistricts
  • Search by Thai or English name, with or without spaces, hyphens or the word for the level
  • Frozen dataclasses with a docstring on every field, and Literal types for every code
  • Data loads on the first call, so import geothai stays fast
  • No dependencies

Install

pip install geothai

The package needs Python 3.11 or later.

Usage

>>> from geothai import address, by_postal_code, districts, match, province, search, subdistricts
>>> province("84")
Province(code='84', name_th='สุราษฎร์ธานี', name_en='Surat Thani')
>>> len(districts("84"))
19
>>> [s.name_en for s in subdistricts("8405")]
['Ko Pha-ngan', 'Ban Tai', 'Ko Tao']
>>> [s.name_th for s in by_postal_code("84280")]
['เกาะพะงัน', 'บ้านใต้']
>>> a = address("840503")
>>> f"{a.subdistrict.name_th} {a.district.name_th} {a.province.name_th}"
'เกาะเต่า เกาะพะงัน สุราษฎร์ธานี'
>>> [f"{r.level} {r.area.name_th}" for r in search("phangan")]
['district เกาะพะงัน', 'subdistrict เกาะพะงัน']
>>> match("ต.ตลาด", district="อ.เมือง", province="สุราษฎร์ธานี")[0].subdistrict.code
'840101'

API

Function Returns
provinces() All 77 provinces, sorted by code
province(code) One province, or None
districts(province_code) The districts of a province, empty for an unknown code
district(code) One district, or None
subdistricts(district_code) The subdistricts of a district, empty for an unknown code
subdistrict(code) One subdistrict, or None
address(subdistrict_code) The subdistrict with its district and province, or None
by_postal_code(postal_code) The subdistricts that use a postal code
search(query, *, limit=10) Provinces, districts and subdistricts whose name has the query
match(subdistrict, *, district, province, postal_code) The addresses that fit a hand-written address
DATA_VERSION The GeoThai/data release in this package, such as '2026.10.22'

Every function takes codes as strings and returns a new list. The areas are frozen dataclasses, so you can use them as dictionary keys.

match() is for addresses typed by hand, such as old records or form input. It reads short forms such as ต., อ. and จ., the word เมือง for a capital district, and กทม for Bangkok. Names must match in full. One result means the address is clear. More than one means you need another argument: 795 subdistrict names are used more than once. None means no current subdistrict has the name.

The areas that DOPA abolished have their own module:

>>> from geothai.abolished import abolished_areas
>>> bang_na = next(a for a in abolished_areas() if a.code == "104701")
>>> bang_na.abolished_on
'2022-04-22'
>>> bang_na.successors
('104702', '104703')
>>> bang_na.successors_basis
'notice'

successors lists the codes that took an abolished area's place. successors_basis says how sure the link is: "notice" comes from a Royal Gazette notice, with the date and source in successors_in_force and successors_source. "name" is a match by Thai name for codes DOPA dropped in 2006, likely but not proven. When no successor is known, successors is empty.

For a name from an old record that match() does not find, match_abolished() looks in the abolished subdistricts:

>>> from geothai.abolished import match_abolished
>>> [a.successors for a in match_abolished("แขวงบางนา", district="เขตบางนา", province="กทม")]
[('104702', '104703')]

Types

from geothai.types import Province, ProvinceCode, SearchResult, Subdistrict
Type Content
Province, District, Subdistrict, Abolished One area. Each field has its description from the data schema
ProvinceCode, DistrictCode, SubdistrictCode Every code in use, for autocomplete and checks in your editor
Address, SearchResult What address() and search() return

The functions take str, not the code types. A data release can remove a code, and that must not break your type check in a patch update.

Versions

A new GeoThai/data release ships as a patch version. A change to the API gets a minor or major version. DATA_VERSION tells you which data you have. The changes in each data release are at geothai.fasu.dev/changelog.

Upgrading from v2

Version 3 is a rewrite on new data. Version 2 did not load its data after install, so code that used it did not run.

v2 v3
get_all_provinces() provinces()
get_province_by_code(10) province("10")
get_all_districts(), get_all_subdistricts() districts(code), subdistricts(code)
get_district_by_code(), get_subdistrict_by_code() district(), subdistrict()
get_postal_codes_by_code(code) by_postal_code(code)
get_provinces_by_criterion(...) and the others A list comprehension on the list

The v2 code is on the archive branch.

Why?

The data comes from GeoThai/data. To see how it is built and checked, read How it is built.

Address forms in Thailand need the same four lists, and most copies of them are old: merged postal codes, abolished subdistricts, English names on the wrong area. This package ships the data that GeoThai checks against official sources, with types your editor can read.

License

The code is MIT. The data is CC BY 4.0 © 2026 Pongsakorn Thipayanate.

If you show the data, credit "GeoThai". Source credits and the license of thailand-geography-json are in NOTICE.

Metadata

Release files for geothai 3.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for geothai 3.2.1
File Size Uploaded Provenance Actions
geothai-3.2.1.tar.gz 176.5 kB With provenance Details for geothai-3.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for geothai 3.2.1
File Interpreter ABI Platform Actions
geothai-3.2.1-py3-none-any.whl Python 3 none any Details for geothai-3.2.1-py3-none-any.whl

Total release size: 355.6 kB

Release files / geothai-3.2.1.tar.gz

Download URL geothai-3.2.1.tar.gz
Size 176.5 kB
Tags Source
SHA-256 checksum
How to use checksums
93231cda5c44758e0b19947bab32a64185c19858dccf134338b3541bf296c470
BLAKE2b-256 checksum
How to use checksums
130f96e53d9c74ba8c751ff6076b001c69f2fbe34e2a15440e0a336f3c1dbcdd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.

Transparency log

Release files / geothai-3.2.1-py3-none-any.whl

Download URL geothai-3.2.1-py3-none-any.whl
Size 179.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d4d41349c0c8ad07a579956b95d39f7b191426b5681daf66ab1ee522122d8e9b
BLAKE2b-256 checksum
How to use checksums
a61ba5d8a9c02c8f7e5c87e73151613cdf5b02eebdf784daab6a1452fecb065c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.

Transparency log

Security for geothai 3.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for geothai-3.2.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 23, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

3.2.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page