{"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,"search_vec":"'-3.12':48 '1.1.0':35 '2024':44 '3.9':47 'algorithm':22 'cadenc':40 'causal':14,50 'causal-infer':49 'cem':4,8,21 'coarsen':1,5,24 'current':32 'dec':43 'depend':31 'exact':2,6,27 'implement':19 'infer':15,51 'last':41 'librari':12 'match':3,7,17,28,52 'model':30 'python':11,46 'reduc':29 'releas':39,42 'requir':45 'stabl':38 'statist':53 'variabl':25 'version':33 'via':16","created_at":"2026-05-09T05:51:18.007481+00:00","updated_at":"2026-05-09T05:51:18.007481+00:00","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,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/cem/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["data"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-29","last_verified":"2026-06-29","next_check":"2026-07-29","install_tag":null}}