Maternity Services Data Set (MSDS)#
This page is a work in progress.
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[1], line 5
3 sys.path.append(os.path.abspath('../../../../scripts/'))
4 from data_doc_helper import NHSEDataSet as DS, last_modified
----> 5 ds = DS("MSDS")
6 last_modified()
File /opt/build/repo/ukllc_book/docs/scripts/data_doc_helper.py:1052, in NHSEDataSet.__init__(self, dataset)
1050 self.apa_cite, self.dl_cites = cites(self.doi)
1051 self.latest_v = get_latest_dsvs(self.dataset)
-> 1052 self.participants = cohort_total(self.dataset)
File /opt/build/repo/ukllc_book/docs/scripts/data_doc_helper.py:1041, in NHSEDataSet.__init__.<locals>.cohort_total(ds)
1039 return "N/A - Dataset comprises of multiple auxiliary tables"
1040 else:
-> 1041 df = md.get_nhse_cohort_counts(ds)
1042 df["count"] = df["count"].apply(lambda x: int(x.replace("<10", "0")))
1043 return str(df["count"].sum())
File /opt/build/repo/ukllc_book/docs/scripts/mdapi_functions.py:42, in get_nhse_cohort_counts(ds)
40 data = r.text
41 pj = json.loads(data)
---> 42 return pd.json_normalize(pj)[["cohort", "count"]]
File ~/.local/share/mise/installs/python/3.13.5/lib/python3.13/site-packages/pandas/core/frame.py:4113, in DataFrame.__getitem__(self, key)
4111 if is_iterator(key):
4112 key = list(key)
-> 4113 indexer = self.columns._get_indexer_strict(key, "columns")[1]
4115 # take() does not accept boolean indexers
4116 if getattr(indexer, "dtype", None) == bool:
File ~/.local/share/mise/installs/python/3.13.5/lib/python3.13/site-packages/pandas/core/indexes/base.py:6212, in Index._get_indexer_strict(self, key, axis_name)
6209 else:
6210 keyarr, indexer, new_indexer = self._reindex_non_unique(keyarr)
-> 6212 self._raise_if_missing(keyarr, indexer, axis_name)
6214 keyarr = self.take(indexer)
6215 if isinstance(key, Index):
6216 # GH 42790 - Preserve name from an Index
File ~/.local/share/mise/installs/python/3.13.5/lib/python3.13/site-packages/pandas/core/indexes/base.py:6261, in Index._raise_if_missing(self, key, indexer, axis_name)
6259 if nmissing:
6260 if nmissing == len(indexer):
-> 6261 raise KeyError(f"None of [{key}] are in the [{axis_name}]")
6263 not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique())
6264 raise KeyError(f"{not_found} not in index")
KeyError: "None of [Index(['cohort', 'count'], dtype='object')] are in the [columns]"