{"id":27640,"library":"cem","title":"Coarsened Exact Matching (CEM)","description":"Coarsened Exact Matching (CEM) is a Python library for causal inference via matching. It implements the CEM algorithm that coarsens variables to exact matching, reducing model dependence. Current version is 1.1.0, with a stable release cadence (last release Dec 2024). Requires Python 3.9-3.12.","status":"active","version":"1.1.0","language":"python","source_language":"en","source_url":"https://github.com/rdrr1990/cem","tags":["causal-inference","matching","statistics"],"install":[{"cmd":"pip install cem","lang":"bash","label":"PyPI"}],"dependencies":[],"imports":[{"note":"The library does not export a CEM class at the top level; use the module-level function cem.cem() or cem.match() instead.","wrong":"from cem import CEM","symbol":"cem","correct":"import cem"}],"quickstart":{"code":"import cem\nimport pandas as pd\n\n# Sample data (replace with your own)\ndf = pd.DataFrame({\n    'treat': [1,1,0,0,1,0],\n    'x1': [5.1, 4.9, 5.0, 6.2, 5.5, 5.8],\n    'x2': [3.5, 3.0, 3.6, 2.9, 3.7, 3.2]\n})\n\n# Perform CEM (coarsen x1 and x2 automatically)\nresult = cem.match(data=df, treatment='treat', coarsen=['x1', 'x2'])\nprint(result['matched_data'].head())","lang":"python","description":"Basic CEM matching using default auto-coarsening."},"warnings":[{"fix":"Always capture the returned dict, e.g., `result = cem.match(...)`","message":"The `cem.match()` function modifies the input DataFrame in-place? No, but the returned dict contains a copy of matched data. Be careful not to rely on side effects.","severity":"gotcha","affected_versions":"all"},{"fix":"Use `import cem` and `cem.match()`","message":"The old top-level import `from cem import cem` is deprecated in favor of `import cem` and calling `cem.match()`. The old import still works but may be removed in a future version.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Ensure no NaN values in columns used for treatment or coarsening.","message":"Missing values in treatment or matching variables cause silent dropping of rows. The library does not raise an error; it prints a warning but continues.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-09T00:00:00.000Z","next_check":"2026-08-07T00:00:00.000Z","problems":[{"fix":"Use `cem.match()` instead of `cem.cem()`.","cause":"Trying to call `cem.cem()` after `import cem` but the function is `cem.match()` for the main matching routine.","error":"AttributeError: module 'cem' has no attribute 'cem'"},{"fix":"Ensure your treatment column contains both 0 and 1 (or more groups).","cause":"The treatment variable has only one unique value (all treated or all control).","error":"ValueError: The number of groups must be >1"},{"fix":"Upgrade to cem >= 1.0.0 or use `cutpoints` directly.","cause":"Using an older version of cem. The `coarsen` parameter was added in version 1.0.0.","error":"TypeError: match() got an unexpected keyword argument 'coarsen'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}