Source code for medicaid_utils.topics.obgyn.hospitalization

import dask.dataframe as dd
import pandas as pd
import numpy as np
from ...filters.claims import dx_and_proc


[docs] def flag_preterm( df_claims: dd.DataFrame, cms_format: str = "MAX" ) -> dd.DataFrame: """ Detects preterm birth related hospitalization in claims New Column(s): - hosp_preterm: integer column, 1 when claim has codes denoting preterm birth and 0 otherwise Parameters ---------- df_claims: dd.DataFrame Claims dataframe cms_format: str CMS format identifier, default is 'MAX' Returns ------- dd.DataFrame """ dct_diag_codes = { "preterm": { "incl": { 9: ["6440", "6442", "7651", "7650", "7652"], 10: ["O600", "O601", "P07"], }, "excl": {9: ["76520", "76529"]}, } } df_claims = dx_and_proc.flag_diagnoses_and_procedures( dct_diag_codes, {}, df_claims, cms_format=cms_format ) df_claims = df_claims.rename(columns={"diag_preterm": "hosp_preterm"}) return df_claims
[docs] def flag_multiple_births(df_claims: dd.DataFrame) -> dd.DataFrame: """ Identifies multiple births New Column(s): - hosp_multiple_births: 0 or 1, 1 when claim has codes indicating a multiple birt hdelivery Parameters ---------- df_claims: dd.DataFrame Claims dataframe Returns ------- dd.DataFrame """ dct_diag_codes = { "multiple_births": { "incl": {9: ["V272", "V273", "V274", "V275", "V276", "V277"]} } } df_claims = dx_and_proc.flag_diagnoses_and_procedures( dct_diag_codes, {}, df_claims ) df_claims = df_claims.rename( columns={"diag_multiple_births": "hosp_multiple_births"} ) return df_claims
[docs] def flag_delivery_mode(df_claims: dd.DataFrame) -> dd.DataFrame: """ Identifies mode of birth New Column(s): - hosp_vag_dlvry: 0 or 1, 1 denotes vaginal delivery - hosp_csrn_dlvry: 0 or 1, 1 denotes caesarian delivery Parameters ---------- df_claims: dd.DataFrame Claims dataframe Returns ------- dd.DataFrame """ dct_proc_codes = { "vag_dlvry": { 1: [str(cd) for cd in range(59400, 59411)], 6: [str(cd) for cd in range(59400, 59411)], }, "csrn_dlvry": { 1: [str(cd) for cd in range(59510, 59516)] + [str(cd) for cd in range(59618, 59623)], 2: ["74"], 6: [str(cd) for cd in range(59510, 59516)] + [str(cd) for cd in range(59618, 59623)], }, } df_claims = dx_and_proc.flag_diagnoses_and_procedures( {}, dct_proc_codes, df_claims ) df_claims = df_claims.rename( columns={ "proc_vag_dlvry": "hosp_vag_dlvry", "proc_csrn_dlvry": "hosp_csrn_dlvry", } ) return df_claims
[docs] def flag_delivery( df_ip_claims: dd.DataFrame, cms_format: str = "MAX", ) -> dd.DataFrame: """ Detects normal and stillbirths related hospitalization in claims New Column(s): - hosp_delivery: integer column, 1 when claim denotes live or still birth and 0 otherwise Parameters ---------- df_ip_claims: dd.DataFrame IP claims dataframe cms_format : {'MAX', TAF'} CMS file format. Returns ------- dd.DataFrame """ dct_diag_codes = { "delivery": { "incl": { 9: [ "650", "64001", "64011", "64021", "64031", "64041", "64051", "64061", "64071", "64081", "64091", "64101", "64111", "64121", "64131", "64141", "64151", "64161", "64171", "64181", "64191", "64201", "64211", "64221", "64231", "64241", "64251", "64261", "64271", "64281", "64291", "64301", "64311", "64321", "64331", "64341", "64351", "64361", "64371", "64381", "64391", "64401", "64411", "64421", "64431", "64441", "64451", "64461", "64471", "64481", "64491", "64501", "64511", "64521", "64531", "64541", "64551", "64561", "64571", "64581", "64591", "64601", "64611", "64621", "64631", "64641", "64651", "64661", "64671", "64681", "64691", "64701", "64711", "64721", "64731", "64741", "64751", "64761", "64771", "64781", "64791", "64801", "64811", "64821", "64831", "64841", "64851", "64861", "64871", "64881", "64891", "64901", "64911", "64921", "64931", "64941", "64951", "64961", "64971", "64981", "64991", "65001", "65011", "65021", "65031", "65041", "65051", "65061", "65071", "65081", "65091", "65101", "65111", "65121", "65131", "65141", "65151", "65161", "65171", "65181", "65191", "65201", "65211", "65221", "65231", "65241", "65251", "65261", "65271", "65281", "65291", "65301", "65311", "65321", "65331", "65341", "65351", "65361", "65371", "65381", "65391", "65401", "65411", "65421", "65431", "65441", "65451", "65461", "65471", "65481", "65491", "65501", "65511", "65521", "65531", "65541", "65551", "65561", "65571", "65581", "65591", "65601", "65611", "65621", "65631", "65641", "65651", "65661", "65671", "65681", "65691", "65701", "65711", "65721", "65731", "65741", "65751", "65761", "65771", "65781", "65791", "65801", "65811", "65821", "65831", "65841", "65851", "65861", "65871", "65881", "65891", "65901", "65911", "65921", "65931", "65941", "65951", "65961", "65971", "65981", "65991", "66001", "66011", "66021", "66031", "66041", "66051", "66061", "66071", "66081", "66091", "66101", "66111", "66121", "66131", "66141", "66151", "66161", "66171", "66181", "66191", "66201", "66211", "66221", "66231", "66241", "66251", "66261", "66271", "66281", "66291", "66301", "66311", "66321", "66331", "66341", "66351", "66361", "66371", "66381", "66391", "66401", "66411", "66421", "66431", "66441", "66451", "66461", "66471", "66481", "66491", "66501", "66511", "66521", "66531", "66541", "66551", "66561", "66571", "66581", "66591", "66601", "66611", "66621", "66631", "66641", "66651", "66661", "66671", "66681", "66691", "66701", "66711", "66721", "66731", "66741", "66751", "66761", "66771", "66781", "66791", "66801", "66811", "66821", "66831", "66841", "66851", "66861", "66871", "66881", "66891", "66901", "66911", "66921", "66931", "66941", "66951", "66961", "66971", "66981", "66991", "67001", "67011", "67021", "67031", "67041", "67051", "67061", "67071", "67081", "67091", "67101", "67111", "67121", "67131", "67141", "67151", "67161", "67171", "67181", "67191", "67201", "67211", "67221", "67231", "67241", "67251", "67261", "67271", "67281", "67291", "67301", "67311", "67321", "67331", "67341", "67351", "67361", "67371", "67381", "67391", "67401", "67411", "67421", "67431", "67441", "67451", "67461", "67471", "67481", "67491", "67501", "67511", "67521", "67531", "67541", "67551", "67561", "67571", "67581", "67591", "67601", "67611", "67621", "67631", "67641", "67651", "67661", "67671", "67681", "67691", "64002", "64012", "64022", "64032", "64042", "64052", "64062", "64072", "64082", "64092", "64102", "64112", "64122", "64132", "64142", "64152", "64162", "64172", "64182", "64192", "64202", "64212", "64222", "64232", "64242", "64252", "64262", "64272", "64282", "64292", "64302", "64312", "64322", "64332", "64342", "64352", "64362", "64372", "64382", "64392", "64402", "64412", "64422", "64432", "64442", "64452", "64462", "64472", "64482", "64492", "64502", "64512", "64522", "64532", "64542", "64552", "64562", "64572", "64582", "64592", "64602", "64612", "64622", "64632", "64642", "64652", "64662", "64672", "64682", "64692", "64702", "64712", "64722", "64732", "64742", "64752", "64762", "64772", "64782", "64792", "64802", "64812", "64822", "64832", "64842", "64852", "64862", "64872", "64882", "64892", "64902", "64912", "64922", "64932", "64942", "64952", "64962", "64972", "64982", "64992", "65002", "65012", "65022", "65032", "65042", "65052", "65062", "65072", "65082", "65092", "65102", "65112", "65122", "65132", "65142", "65152", "65162", "65172", "65182", "65192", "65202", "65212", "65222", "65232", "65242", "65252", "65262", "65272", "65282", "65292", "65302", "65312", "65322", "65332", "65342", "65352", "65362", "65372", "65382", "65392", "65402", "65412", "65422", "65432", "65442", "65452", "65462", "65472", "65482", "65492", "65502", "65512", "65522", "65532", "65542", "65552", "65562", "65572", "65582", "65592", "65602", "65612", "65622", "65632", "65642", "65652", "65662", "65672", "65682", "65692", "65702", "65712", "65722", "65732", "65742", "65752", "65762", "65772", "65782", "65792", "65802", "65812", "65822", "65832", "65842", "65852", "65862", "65872", "65882", "65892", "65902", "65912", "65922", "65932", "65942", "65952", "65962", "65972", "65982", "65992", "66002", "66012", "66022", "66032", "66042", "66052", "66062", "66072", "66082", "66092", "66102", "66112", "66122", "66132", "66142", "66152", "66162", "66172", "66182", "66192", "66202", "66212", "66222", "66232", "66242", "66252", "66262", "66272", "66282", "66292", "66302", "66312", "66322", "66332", "66342", "66352", "66362", "66372", "66382", "66392", "66402", "66412", "66422", "66432", "66442", "66452", "66462", "66472", "66482", "66492", "66502", "66512", "66522", "66532", "66542", "66552", "66562", "66572", "66582", "66592", "66602", "66612", "66622", "66632", "66642", "66652", "66662", "66672", "66682", "66692", "66702", "66712", "66722", "66732", "66742", "66752", "66762", "66772", "66782", "66792", "66802", "66812", "66822", "66832", "66842", "66852", "66862", "66872", "66882", "66892", "66902", "66912", "66922", "66932", "66942", "66952", "66962", "66972", "66982", "66992", "67002", "67012", "67022", "67032", "67042", "67052", "67062", "67072", "67082", "67092", "67102", "67112", "67122", "67132", "67142", "67152", "67162", "67172", "67182", "67192", "67202", "67212", "67222", "67232", "67242", "67252", "67262", "67272", "67282", "67292", "67302", "67312", "67322", "67332", "67342", "67352", "67362", "67372", "67382", "67392", "67402", "67412", "67422", "67432", "67442", "67452", "67462", "67472", "67482", "67492", "67502", "67512", "67522", "67532", "67542", "67552", "67562", "67572", "67582", "67592", "67602", "67612", "67622", "67632", "67642", "67652", "67662", "67672", "67682", "67692", "V271", "V272", "V273", "V274", "V275", "V276", "V277", "V278", "V279", "V30", "V310", "V320", "V330", "V340", "V350", "V360", "V370", "V380", "V390", "V311", "V321", "V331", "V341", "V351", "V361", "V371", "V381", "V391", ], 10: [ "Z3769", "Z3751", "Z3750", "Z370", "Z381", "Z3866", "O364XX5", "Z382", "Z3861", "Z371", "Z3863", "Z3830", "Z374", "O364XX2", "Z387", "Z3760", "Z3753", "Z3868", "Z3801", "Z372", "Z3831", "Z3800", "Z3764", "O364XX9", "Z3759", "Z373", "Z3752", "Z385", "Z3754", "Z3865", "O364XX1", "Z384", "O364XX3", "O82", "Z377", "Z3862", "Z3869", "Z388", "Z379", "Z3864", "O364XX4", "Z3761", "O80", "Z3763", "Z3762", "Z390", ], } } } dct_column_values = {"diag_delivery": {"RCPNT_DLVRY_CD": [1]}} df_ip_claims = dx_and_proc.flag_diagnoses_and_procedures( dct_diag_codes, {}, df_ip_claims, dct_column_values=dct_column_values, cms_format=cms_format, ) # if cms_format == 'MAX': # df_ip_claims = df_ip_claims.assign( # diag_delivery=((dd.to_numeric(df_ip_claims['RCPNT_DLVRY_CD'], # errors='coerce') == 1) | # (df_ip_claims['diag_delivery'] == 1)).astype(int) # ) df_ip_claims = df_ip_claims.rename( columns={"diag_delivery": "hosp_delivery"} ) return df_ip_claims
[docs] def flag_abnormal_pregnancy( df_claims: dd.DataFrame, cms_format: str = "MAX" ) -> dd.DataFrame: """ Detects ectopic, molar, or abnormal pregnancy, spontaneous or induced abortion related hospitalization New Column(s): - hosp_abnormal_pregnancy: integer column, 1 when claim denotes ectopic, molar, or abnormal pregnancy, spontaneous or induced abortion and 0 otherwise Parameters ---------- df_claims: dd.DataFrame Claims dataframe cms_format: str CMS format identifier, default is 'MAX' Returns ------- dd.DataFrame """ dct_diag_codes = { "abnormal_pregnancy": { "incl": { 9: ["63"], 10: [ "O00", "O01", "O02", "O03", "O04", "O07", "O08", "A34", "Z332", ], } } } df_claims = dx_and_proc.flag_diagnoses_and_procedures( dct_diag_codes, {}, df_claims, cms_format=cms_format ) df_claims = df_claims.rename( columns={"diag_abnormal_pregnancy": "hosp_abnormal_pregnancy"} ) return df_claims
[docs] def flag_preconception_care( df_claims: dd.DataFrame, cms_format: str = "MAX" ) -> dd.DataFrame: """ Adds flag columns denoting presence of codes indicating pre-conception care New Columns: - hosp_preconception_care: 0 or 1, 1 when claim has codes indicating pre-conception care - hosp_preconception_care_contraceptive_services: 0 or 1, 1 when claim has codes indicating contraceptive services related pre-conception care - hosp_preconception_care_pregnancy_testing_and_counseling: 0 or 1, 1 when claim has codes indicating pregnancy testing and counseling related pre-conception care - hosp_preconception_care_achieving_pregnancy: 0 or 1, 1 when claim has codes indicating achieving pregnancy related pre-conception care - hosp_preconception_care_basic_infertility_services: 0 or 1, 1 when claim has codes indicating basic infertility services related pre-conception care - hosp_preconception_care_preconception_health_services: 0 or 1, 1 when claim has codes indicating preconception health services related pre-conception care - hosp_preconception_care_std_services: 0 or 1, 1 when claim has codes indicating STD services related pre-conception care - hosp_preconception_care_related_preventive_health_services: 0 or 1, 1 when claim has codes indicating preventive health services related pre-conception care Parameters ---------- df_claims: dd.DataFrame Claims dataframe cms_format : {'MAX', TAF'} CMS file format. Returns ------- dd.DataFrame """ dct_diag_codes = { "preconception_care_contraceptive_services": { "incl": { 9: [ "V2501", "V2541", "V259", "V2540", "V255", "V2543", "V2511", "V2512", "V2513", "V2542", "V2502", "V2549", "V2503", "V2504", "V252", "V258", "V2509", ], 10: [ "Z30011", "Z3041", "Z309", "Z3040", "Z3049", "Z30430", "Z30432", "Z30433", "Z30431", "Z30013", "Z30014", "Z30018", "Z30019", "Z3042", "Z3049", "Z30012", "Z3002", "Z302", "Z308", "Z3009", ], } }, "preconception_care_pregnancy_testing_and_counseling": { "incl": { 9: ["V7240", "V7241", "V7242"], 10: ["Z3200", "Z3202", "Z3201"], } }, "preconception_care_achieving_pregnancy": { "incl": {9: ["V2641"], 10: ["Z3161"]} }, "preconception_care_basic_infertility_services": { "incl": { 9: [ "6280", "6281", "6282", "6283", "6284", "6289", "6069", "V2621", ], 10: [ "N970", "E230", "N971", "N972", "N978", "N979", "N469", "Z3141", ], } }, "preconception_care_preconception_health_services": { "incl": { 9: [ "V2649", "V791", "V1582", "V778", "V653", "V6541", "V811", "V771", "V790", "V6542", ], 10: [ "Z3169", "Z1389", "Z87891", "Z1389", "Z713", "Z7189", "Z136", "Z131", "Z1389", "Z7141", "Z7142", "Z7151", "Z7152", "Z716", ], } }, "preconception_care_std_services": { "incl": { 9: [ "V016", "V028", "V1209", "V6540", "V6544", "V6545", "V655", "V692", "V7381", "V7388", "V7389", "V745", "V6544", "V6545", ], 10: [ "Z202", "Z224", "Z8619", "Z719", "Z717", "Z7189", "Z711", "Z7251", "Z7252", "Z7253", "Z1151", "Z118", "Z114", "Z1159", "Z113", "Z717", "Z7189", ], } }, "preconception_care_related_preventive_health_services": { "incl": { 9: ["V762", "V700", "V7231", "V7619"], 10: ["Z124", "Z0000", "Z0001", "Z01411", "Z01419", "Z1239"], } }, } dct_diag_codes["preconception_care"] = { "incl": { 9: [ code for codeset in [ service_codes["incl"][9] for service_codes in dct_diag_codes.values() ] for code in codeset ], 10: [ code for codeset in [ service_codes["incl"][10] for service_codes in dct_diag_codes.values() ] for code in codeset ], } } df_claims = dx_and_proc.flag_diagnoses_and_procedures( dct_diag_codes, {}, df_claims, cms_format=cms_format ) df_claims = df_claims.rename( columns={ f"diag_{service}": f"hosp_{service}" for service in dct_diag_codes } ) return df_claims
[docs] def flag_prenatal( df_claims: dd.DataFrame, cms_format: str = "MAX" ) -> dd.DataFrame: """ Adds flag columns denoting presence of codes indicating pre-natal care New Columns: - prenatal: 0 or 1, 1 when claim has codes indicating pre-natal care Parameters ---------- df_claims: dd.DataFrame Claims dataframe cms_format: str CMS format identifier, default is 'MAX' Returns ------- dd.DataFrame """ dct_diag_codes = { "prenatal": { "incl": { 9: ["V22", "V23"], 10: ["O09", "O3680", "Z331", "Z333", "Z34"], } } } df_claims = dx_and_proc.flag_diagnoses_and_procedures( dct_diag_codes, {}, df_claims, cms_format=cms_format ) df_claims = df_claims.rename(columns={"diag_prenatal": "hosp_prenatal"}) return df_claims
[docs] def flag_smm_events( df_ip_claims: dd.DataFrame, cms_format: str = "MAX" ) -> dd.DataFrame: """ Adds flags for SMM events within 90 days of delivery New Columns: - age_on_delivery (Age as on delivery admission date) - delivery_date (Delivery date) - smm_within_90_days_of_delivery (integer column, indicating an SSM related hospitalization within 90 days of delivery) - smm_no_blood_within_90_days_of_delivery (integer column, indicating an SSM related hospitalization excluding those related to blood transfusion within 90 days of delivery) Parameters ---------- df_ip_claims: dd.DataFrame IP claims dataframe cms_format : {'MAX', TAF'} CMS file format. Returns ------- dd.DataFrame """ dct_diag_codes = { "smm_myo": {"incl": {9: ["410"], 10: ["I21", "I22"]}}, "smm_aneurysm": {"incl": {9: ["441"], 10: ["I71", "I790"]}}, "smm_renal": { "incl": { 9: ["5845", "5846", "5847", "5848", "5849", "6693"], 10: ["N17", "O904"], } }, "smm_respiratory": { "incl": { 9: ["5185", "51881", "51882", "51884", "7991"], 10: [ "J80", "J951", "J952", "J953", "J9582", "J960", "J962", "J969", "R0603", "R092", ], } }, "smm_embolism": { "incl": { 9: ["6731"], 10: ["O88112", "O88113", "O88119", "O8812", "O8813"], } }, "smm_cardiac_arrest": { "incl": {9: ["42741", "42742", "4275"], 10: ["I46", "I490"]} }, "smm_coagulation": { "incl": { 9: ["2866", "2869", "6413", "6663"], 10: [ "D65", "D688", "D689", "O45002", "O45003", "O45009", "O45012", "O45013", "O45019", "O45022", "O45023", "O45029", "O45092", "O45093", "O45099", "O46002", "O46003", "O46009", "O46012", "O46013", "O46019", "O46022", "O46023", "O46029", "O46092", "O46093", "O46099", "O670", "O723", ], } }, "smm_eclampsia": {"incl": {9: ["6426"], 10: ["O15"]}}, "smm_heart_failure": { "incl": { 9: ["9971"], 10: [ "I97120", "I97121", "I97130", "I97131", "I97710", "I97711", ], } }, "smm_cerebrovascular": { "incl": { 9: [ "0463", "34839", "36234", "430", "431", "432", "433", "434", "435", "436", "437", "6715", "6740", "99702", ], 10: [ "A812", "G45", "G46", "G9349", "H340", "I60", "I61", "I62", "I6300", "I6301", "I631", "I632", "I633", "I634", "I635", "I636", "I638", "I639", "I65", "I66", "I67", "I68", "O2250", "O2252", "O2253", "I97810", "I97811", "I97820", "I97821", "O873", ], } }, "smm_edema": { "incl": { 9: [ "4280", "4281", "42820", "42821", "42823", "43830", "42831", "42833", "42840", "42841", "42843", "4289", "5184", ], 10: [ "I501", "I5020", "I5021", "I5023", "I5030", "I5031", "I5033", "I5040", "I5041", "I5043", "I50810", "I50811", "I50813", "I50814", "I5082", "I5083", "I5084", "I5089", "I509", "J810", ], } }, "smm_anesthesia": { "incl": { 9: ["6680", "6681", "6682", "9954", "99586"], 10: [ "O29112", "O29113", "O29114", "O29115", "O29116", "O29117", "O29118", "O29119", "O29122", "O29123", "O29124", "O29125", "O29126", "O29127", "O29128", "O29129", "O29192", "O29193", "O29194", "O29195", "O29196", "O29197", "O29198", "O29199", "O29212", "O29213", "O29214", "O29215", "O29216", "O29217", "O29218", "O29219", "O29292", "O29293", "O29294", "O29295", "O29296", "O29297", "O29298", "O29299", "O740", "O741", "O742", "O743", "O890", "O891", "O892", "T882XXA", "T883XXA", ], } }, "smm_sepsis": { "incl": { 9: ["038", "449", "6702", "78552", "99591", "99592", "99802"], 10: [ "A327", "A40", "A41", "I76", "O85", "O8604", "R6520", "R6521", "T8112XA", "T8144XA", ], } }, "smm_shock": { "incl": { 9: [ "6691", "78550", "78551", "78559", "9950", "9980", "99800", "99801", "99809", ], 10: [ "O751", "R57", "T782XXA", "T8110XA", "T8111XA", "T8119XA", "T886XXA", ], } }, "smm_sickle": { "incl": { 9: ["28242", "28262", "28264", "28269", "28952"], 10: [ "D5700", "D5701", "D5702", "D57211", "D57212", "D57219", "D57411", "D57412", "D57419", "D57811", "D57812", "D57819", ], } }, "smm_thrombotic": { "incl": { 9: ["4150", "4151", "6730", "6732", "6733", "6738"], 10: [ "I26", "O88012", "O88013", "O88019", "08802", "O8803", "O88212", "O88213", "O88219", "O8822", "O8823", "O88312", "O88313", "O88319", "O8832", "O8833", "O88812", "O88813", "O88819", "O8882", "O8883", "T800XXA", ], } }, } dct_proc_codes = { "smm_cardiac_rhythm": {2: ["996"], 7: ["5A12012", "5A2204Z"]}, "smm_transfusion": { 2: ["990"], 7: [ "30230H0", "30230K0", "30230L0", "30230M0", "30230N0", "30230P0", "30230R0", "30230T0", "30230H1", "30230K1", "30230L1", "30230M1", "30230N1", "30230P1", "30230R1", "30230T1", "30233H0", "30233K0", "30233L0", "30233M0", "30233N0", "30233P0", "30233R0", "30233T0", "30233H1", "30233K1", "30233L1", "30233M1", "30233N1", "30233P1", "30233R1", "30233T1", "30240H0", "30240K0", "30240L0", "30240M0", "30240N0", "30240P0", "30240R0", "30240T0", "30240H1", "30240K1", "30240L1", "30240M1", "30240N1", "30240P1", "30240R1", "30240T1", "30243H0", "30243K0", "30243L0", "30243M0", "30243N0", "30243P0", "30243R0", "30243T0", "30243H1", "30243K1", "30243L1", "30243M1", "30243N1", "30243P1", "30243R1", "30243T1", ], }, "smm_hysterectomy": { 2: [ "6839", "6849", "6859", "6869", "6879", "689", "683", "684", "685", "686", "687", "689", ], 7: ["0UT90ZL", "0UT90ZZ", "0UT97ZL", "0UT97ZZ"], }, "smm_tracheostomy": {2: ["311"], 7: ["0B110F4", "0B113F4", "0B114F4"]}, "smm_ventilation": {2: ["9670", "9671", "9672"]}, } df_ip_claims = dx_and_proc.flag_diagnoses_and_procedures( dct_diag_codes, dct_proc_codes, df_ip_claims, cms_format=cms_format ) df_ip_claims = df_ip_claims.rename( columns=dict( [(f"diag_{condn}", f"hosp_{condn}") for condn in dct_diag_codes] + [(f"proc_{condn}", f"hosp_{condn}") for condn in dct_proc_codes] ) ) df_ip_claims = df_ip_claims.assign( hosp_smm=df_ip_claims[ df_ip_claims.columns[ df_ip_claims.columns.str.startswith("hosp_smm_") ] ].max(axis=1), hosp_smm_no_blood=df_ip_claims[ [ col for col in df_ip_claims.columns if ( col.startswith("hosp_smm_") and (col != "hosp_smm_transfusion") ) ] ].max(axis=1), ) admsn_col_name = ( "admsn_date" if "admsn_date" in df_ip_claims.columns else "srvc_bgn_date" ) df_ip_claims = flag_delivery(df_ip_claims, cms_format=cms_format) pdf_dobs = ( df_ip_claims.groupby(df_ip_claims.index.name)[["birth_date"]] .max() .compute() ) pdf_benes = pd.concat( [ df_ip_claims.loc[df_ip_claims["hosp_smm"] == 1][[admsn_col_name]] .compute() .groupby(df_ip_claims.index.name)[admsn_col_name] .apply(list) .to_frame() .rename(columns={admsn_col_name: "smm_dates"}), df_ip_claims.loc[df_ip_claims["hosp_smm_no_blood"] == 1][ [admsn_col_name] ] .compute() .groupby(df_ip_claims.index.name)[admsn_col_name] .apply(list) .to_frame() .rename(columns={admsn_col_name: "smm_no_blood_dates"}), df_ip_claims.loc[df_ip_claims["hosp_delivery"] == 1][ [admsn_col_name] ] .compute() .groupby(df_ip_claims.index.name)[admsn_col_name] .apply(list) .to_frame() .rename(columns={admsn_col_name: "delivery_date"}), ], axis=1, ) pdf_benes = pdf_benes.explode("delivery_date") pdf_benes = pdf_benes.merge( pdf_dobs, left_index=True, right_index=True, how="left" ) pdf_benes = pdf_benes.assign( age_on_delivery=( pdf_benes["delivery_date"] - pdf_benes["birth_date"] ).dt.days.fillna(0) / 365.25 ) pdf_benes = pdf_benes.assign( smm_within_90_days_of_delivery=pdf_benes.apply( lambda x: int( any( (y <= x["delivery_date"] + pd.Timedelta(days=90)) & (y >= x["delivery_date"]) for y in x["smm_dates"] ) if isinstance(x["smm_dates"], list) else False ), axis=1, ), smm_no_blood_within_90_days_of_delivery=pdf_benes.apply( lambda x: int( any( (y <= x["delivery_date"] + pd.Timedelta(days=90)) & (y >= x["delivery_date"]) for y in x["smm_no_blood_dates"] ) if isinstance(x["smm_no_blood_dates"], list) else False ), axis=1, ), ) pdf_benes = pdf_benes[ [ "age_on_delivery", "delivery_date", "smm_within_90_days_of_delivery", "smm_no_blood_within_90_days_of_delivery", ] ] return pdf_benes
[docs] def calculate_conception( df_claims: dd.DataFrame, ) -> (dd.DataFrame, dd.DataFrame): """ Estimates conception date based on type of delivery and delivery date. Conception date is calculated as, - 75 days before date of abortive claims - 255 days before full term deliveries - 230 days before pre-term deliveries - 45 days before pre-natal claims New columns: - conception_date: Date of conception. Parameters ---------- df_claims: dd.DataFrame Claims dataframe that has indicator columns for type of deliveries, viz., hosp_abnormal_pregnancy, hosp_birth, hosp_preterm, and hosp_prenatal Returns ------- dd.DataFrame """ lst_hosp_pregnancy_col = [ "hosp_abnormal_pregnancy", "hosp_birth", "hosp_preterm", "hosp_prenatal", ] lst_missing_col = [ col for col in lst_hosp_pregnancy_col if col not in df_claims.columns ] if len(lst_missing_col) > 0: df_claims = df_claims.assign( **{col: 0 for col in lst_missing_col} ) lst_conception_col = [ "abortive", "full_term_delivery", "preterm_delivery", "prenatal", ] df_claims = df_claims.assign( abortive=(df_claims["hosp_abnormal_pregnancy"] == 1).astype(int), full_term_delivery=( (df_claims["hosp_abnormal_pregnancy"] == 0) & (df_claims["hosp_birth"] == 1) & (df_claims["hosp_preterm"] == 0) ).astype(int), preterm_delivery=( (df_claims["hosp_abnormal_pregnancy"] == 0) & (df_claims["hosp_birth"] == 1) & (df_claims["hosp_preterm"] == 1) ).astype(int), prenatal=( (df_claims["hosp_abnormal_pregnancy"] == 0) & (df_claims["hosp_birth"] == 0) & (df_claims["hosp_prenatal"] == 1) ).astype(int), ) df_conception = df_claims.loc[ df_claims[lst_conception_col].sum(axis=1) == 1 ] df_conception = df_conception.map_partitions( lambda pdf: pdf.assign( conception_date=pdf["service_date"] - np.select( [ (pdf["abortive"] == 1), (pdf["full_term_delivery"] == 1), (pdf["preterm_delivery"] == 1), (pdf["prenatal"] == 1), ], [ pd.Timedelta(days=75), pd.Timedelta(days=270) - pd.Timedelta(days=15), pd.Timedelta(days=245) - pd.Timedelta(days=15), pd.Timedelta(days=45), ], default=np.nan, ) ) ) df_conception = df_conception.drop(lst_conception_col, axis=1) return df_conception