Skip to main content

drgpy

drgpy is a Python library for assigning a combination of diagnosis and procedure codes to Diagnosis Related Groups (MS-DRG) that is used in Medicare inpatient reimbursement today.

The default version is MS-DRG v43.1, effective April 1, 2026. Bundled versions range from v36 through v43.1.

Published distributions contain deterministic compressed runtime snapshots for each supported version. The original CMS text files remain in the Git repository for auditing, regeneration, and parser validation, but are not included in wheels or source distributions.

Installing

Installing from the source:

$ git clone git@github.com:yubin-park/drgpy.git
$ cd drgpy
$ python -m pip install --editable .

Or, simply using pip:

$ pip install drgpy

Development

Install the standardized development dependency group and run the tests:

$ python -m venv .venv
$ source .venv/bin/activate
$ python -m pip install --upgrade pip
$ python -m pip install --editable . --group dev
$ python -m pytest

Build and validate the source and wheel distributions:

$ python -m build
$ python -m twine check dist/*

Regenerate runtime snapshots after updating the CMS source files:

$ python -m tools.build_runtime_data

Generated snapshots must be committed together with their corresponding raw-data or parser changes. Tests verify that regeneration is deterministic and that the snapshots exactly represent the checked-in CMS text files.

See Architecture for diagrams of the CMS-data build pipeline, runtime feature extraction, MDC evaluation, grouping precedence, and test organization.

Validation Samples

Validation uses minimized synthetic and real-world-class samples that isolate specific CMS rule behavior. Fixtures should contain only the inputs required to reproduce the expected grouping and should not include identifying or operational metadata.

Cases requiring unavailable inputs such as age or birth weight should be tracked separately rather than treated as grouper mismatches. A successful claim sample comparison is useful regression evidence, but does not establish complete parity with the official CMS grouper.

File Structure

  • drgpy/: The package source code is located here.
    • data/: Raw CMS files retained in Git but excluded from distributions.
    • runtime_data/: Generated compressed maps included in distributions.
    • msdrg.py: The main file for the MS-DRG logic.
    • _mdcsrdr.py: A script that reads/parses mdcs_xx_xx.txt data files.
    • _appndxrdr.py: A script that reads/parses appendix_xx.txt data files.
    • _mdcs0007.py: logics for MDC00 - MDC07
    • _mdcs0811.py: logics for MDC08 - MDC11
    • _mdcs1221.py: logics for MDC12 - MDC21
    • _mdcs2225.py: logics for MDC22 - MDC25
  • tests/: test scripts to check the validity of the outputs.
  • docs/architecture.md: conceptual and implementation architecture guide.
  • tools/build_runtime_data.py: deterministic runtime-snapshot generator.
  • LICENSE.txt: Apache 2.0.
  • README.md: This README file.
  • pyproject.toml: package metadata, build configuration, and development dependency groups.

Code Examples

drgpy is really simple to use. Please see some examples below. NOTE that all functions used below have docstrings. If you want to see the input parameter specifications, please type print(<instance>.<function>.__doc__).

Date-Based Version Selection

Use the wrapper to select the version effective on the supplied discharge date:

>>> from drgpy.msdrg_allvers import DRGEngineAllVers
>>> de = DRGEngineAllVers()
>>> print(de.get_drg.__doc__)

        Return the corresponding DRG code for the diagnoses and procedures

        Parameters
        ----------
        dx_lst : list
                A list of ICD-10 diagnosis codes
        pr_lst : list
                A list of ICD-10 procedure codes
        date: str
                YYYY-MM-DD format
                Depending on the date of the claim,
                the engine will choose the appropriate version.
                For example, 2025-10-01 selects v43 and
                2026-04-01 selects v43.1.
        gender: str
                "F" or "M"
        is_alive: boolean
                if the patient is alive at discharge (True)

Each Version Separately

NOTE that this usage doesn't require the date field.

>>> from drgpy.msdrg import DRGEngine
>>> de = DRGEngine(version="v43.1")
>>> print(de.get_drg.__doc__)

        Return the corresponding DRG code for the diagnoses and procedures

        Parameters
        ----------
        dx_lst : list
                A list of ICD-10 diagnosis codes
        pr_lst : list
                A list of ICD-10 procedure codes
        gender: str
                "F" or "M"
        is_alive: boolean
                if the patient is alive at discharge (True)
>>>
>>> de.get_drg(["B20"],[])
'977'
>>> de.get_drg([], ["02HA0RS"])
'983'
>>> de.get_drg([], ["02HA0RS", "02PA0RZ"])
'002'
>>>

Calling DRGEngine() without a version uses the latest bundled version. POA indicators may be supplied as a list aligned with the diagnoses or as a mapping keyed by diagnosis code. Missing POA values default to "Y" for backward compatibility.

from drgpy.msdrg import DRGEngine

engine = DRGEngine()
drg = engine.get_drg(
    ["I21A1", "E0800"],
    [],
    poa=["Y", "N"],
)

Input Requirements and Limitations

The first diagnosis in dx_lst is treated as the principal diagnosis. An admitting diagnosis is not used separately. Procedure order and the order of secondary diagnoses do not affect grouping.

drgpy currently supports sex, alive-at-discharge status, patient discharge status, and POA indicators. Sex-sensitive MDCs require the correct gender value. The historical default is "F", so callers should always pass "M" or "F" explicitly when grouping production records.

engine.get_drg(["N400"], [], gender="M")
# "726" — Benign Prostatic Hypertrophy without MCC

Age and birth weight are not currently accepted, so diagnosis and procedure codes alone are not sufficient to reproduce every result from the official CMS grouper. drgpy should be treated as an open implementation of the bundled decision tables, not as a replacement for CMS validation software.

Outcome Simulation

Use get_code_drg_candidates to inspect the DRGs directly referenced by a single diagnosis or procedure in the bundled CMS value sets. These are broad rule candidates, not final grouped outcomes.

engine.get_code_drg_candidates("A419", code_type="diagnosis")
engine.get_code_drg_candidates("0SG0071", code_type="procedure")

Use simulate_drg_permutations to group the same code set once for each diagnosis selected as principal. All remaining diagnoses are treated as secondary diagnoses. Procedure order and secondary-diagnosis order are not permuted because they do not change grouping semantics.

simulations = engine.simulate_drg_permutations(
    ["I469", "A021"],
    ["B2151ZZ"],
)

possible_drgs = engine.get_possible_drgs(
    ["I469", "A021"],
    ["B2151ZZ"],
)

Each simulation contains the selected principal diagnosis, secondary diagnoses, procedures, selected DRG, and all matching DRGs before hierarchy selection. These helpers support coding scenario review and audit workflows; they do not determine documentation or billing appropriateness.

Please refer to the test scripts under the tests/ folder if you want to see other example use cases.

Raw Data Change Log

  1. For v38+, in mdcs_00_07.txt, edit
NON-OPERATING ROOM PROCEDURES
02H63JZ*

to

NON-OPERATING ROOM PROCEDURES

  02H63JZ*
  1. For any version, in mdcs_08_11.txt, remove
To qualify as bilateral or multiple joint procedures you must have at least one code from two different lower extremity sites as listed below.
Examples: left hip  and right hip - bilateral; left hip and left knee - multiple;  left hip and right ankle - multiple; left knee and right knee - bilateral
  1. In mdcs_00_07.txt, remove
COMBINATION OF CODES IN THE NEXT FOUR LISTS
...

Alos, in v40, duplicate sections exist for this part.

  1. In mdcs_12_21.txt, remove
Principal or secondary diagnosis of newborn or neonate,with other significant problems, not assigned to DRG 789 through 793 or 795
  1. In mdcs_12_21.txt, for v38+ DRG 768 and 798, edit
NON-OPERATING ROOM PROCEDURES
10D07Z3* Extraction of Products of Conception, Low Forceps, Via Natural or Artificial Opening

to

NON-OPERATING ROOM PROCEDURES

  10D07Z3* Extraction of Products of Conception, Low Forceps, Via Natural or Artificial Opening
  1. In mdcs_12_21.txt, for DRG 807,
NON-OPERATING ROOM PROCEDURES
10D07Z3*

to

NON-OPERATING ROOM PROCEDURES

  10D07Z3*
  1. For v36, in appendix_D_E.txt,

Removed 10D17Z9 14 768 10D18Z9 14 796

As the DRG definition say other OR procedures except for these two above, but these are included as OR procedures in the appendix. Rather than changing the algorithm to deal with the discrepancy, we edit the underlying data to maintain consistency.

  1. Any versions, in mdcs_08_11.txt,

There are group-level or conditions for 456&457&458, e.g., one of... "and" with one of...

Their categories are renamed as EXTENSIVE FUSION PART AB12..

License

Apache 2.0

Authors

Contributor Contribution
Yubin Park, PhD (@yubin-park) Original library, v36–v40 data and grouper logic
Shuo Yang (@syangdh) v41 and v42 CMS data and initial modern-version routing
Nikolaos Vergos (@nvergos) v43 data, modern parser and grouper concordance fixes
Svdmln (@Svdmln) Package-data and importlib.resources improvements

References

Metadata

Release files for drgpy 0.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 drgpy 0.2.1
File Size Uploaded Provenance
drgpy-0.2.1.tar.gz 3.1 MB Without provenance Details

Built distribution (wheel)

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

Total release size: 6.2 MB

Release files / drgpy-0.2.1.tar.gz

Download URL drgpy-0.2.1.tar.gz
Size 3.1 MB
Tags Source
SHA-256 checksum
How to use checksums
b06e7b5a6db0fc8fe11925b0e45c75b359d41b390bf81965917b5db729f33fa2
BLAKE2b-256 checksum
How to use checksums
b23cae29f4cc55167a5a4647803b9e75fc6e051aa17c8b2c3ec076f390b3fd6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.2

Release files / drgpy-0.2.1-py3-none-any.whl

Download URL drgpy-0.2.1-py3-none-any.whl
Size 3.1 MB
Tags Python 3
SHA-256 checksum
How to use checksums
63fc3691ad2b0722578b5c0ea02fa6708462eb30bad53dc197a8f526115cbee6
BLAKE2b-256 checksum
How to use checksums
9004656e6e24adb3d4a6500cb4bc4329ade74078b7dec4d71d853c2f49e0ea59
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.2

Security for drgpy 0.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

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