Best Python code snippet using pandera_python
test_excel_integration.py
Source:test_excel_integration.py
...34def verify_aez_data(obj, verify, cohort):35 """Verified tables in AEZ Data."""36 if cohort == 2018:37 verify['AEZ Data'] = [38 ('A48:H53', obj.ae.get_land_distribution().reset_index().iloc[:6, :], None, None),39 ('A55:H58', obj.ae.get_land_distribution().reset_index().iloc[6:, :], None, None)40 ]41 elif cohort == 2019:42 # Cohort 2019 added more solutions which shifted rows downward43 verify['AEZ Data'] = [44 ('A53:H58', obj.ae.get_land_distribution().reset_index().iloc[:6, :], None, None),45 ('A60:H63', obj.ae.get_land_distribution().reset_index().iloc[6:, :], None, None)46 ]47 elif cohort == 2020:48 # Eight Thermal Moisture Regimes49 verify['AEZ Data'] = [50 ('A53:J58', obj.ae.get_land_distribution().reset_index().iloc[:6, :], None, None),51 ('A60:J63', obj.ae.get_land_distribution().reset_index().iloc[6:, :], None, None)52 ]53 else:54 raise ValueError(f"unknown cohort {cohort}")55 return verify56def _get_interpolation_trend_masks(func):57 """If the TAM/Adoption data being analyzed is very close to linear, then the 2nd/3rd order58 polynomial and exponential curve fits degenerate to where only the x^1 and constant terms59 matter and the higher order terms do not.60 For example in biochar, Excel and Python both come up with {x}=1.57e+07 & const=1.049e+0961 For degree2, Python comes up with -1.15e-09 while Excel decides it is -1.32e-09, but62 it doesn't matter because they are 16 orders of magnitude less than the {x} term.63 If the data is very close to linear, skip comparing the higher order curve fits.64 """65 degree2 = func(trend='Degree2')66 d2_mask = d3_mask = exp_mask = None67 if abs(degree2.loc[2015, 'x'] / degree2.loc[2015, 'x^2']) > 1e12:68 d2_mask = degree2.reset_index(drop=True).copy(deep=True)69 d2_mask.loc[:, :] = False70 d2_mask['x^2'] = True71 d3_mask = func(trend='Degree3').reset_index(drop=True).copy(deep=True)72 d3_mask.loc[:, :] = False73 d3_mask['x^2'] = True74 d3_mask['x^3'] = True75 exp_mask = func(trend='Exponential').reset_index(76 drop=True).copy(deep=True)77 exp_mask.loc[:, :] = False78 exp_mask['e^x'] = True79 return (d2_mask, d3_mask, exp_mask)80def verify_tam_data(obj, verify):81 """Verified tables in TAM Data."""82 func = functools.partial(obj.tm.forecast_trend, region='World')83 (d2_mask, d3_mask, exp_mask) = _get_interpolation_trend_masks(func=func)84 verify['TAM Data'] = [85 ('W46:Y94', obj.tm.forecast_min_max_sd(region='World').reset_index(drop=True), None, None),86 ('AA46:AC94', obj.tm.forecast_low_med_high(region='World').reset_index(drop=True), None, None),87 ('BX50:BZ96', obj.tm.forecast_trend(region='World', trend='Linear').reset_index(drop=True), None, None),88 ('CE50:CH96', obj.tm.forecast_trend(region='World', trend='Degree2').reset_index(drop=True), d2_mask, None),89 ('CM50:CQ96', obj.tm.forecast_trend(region='World', trend='Degree3').reset_index(drop=True), d3_mask, None),90 ('CV50:CX96', obj.tm.forecast_trend(region='World', trend='Exponential').reset_index(drop=True), exp_mask, None),91 ('DZ45:EA91', obj.tm.ref_tam_per_region().reset_index().loc[:, ['Year', 'World']], None, None),92 # TODO Figure out PDS TAM handling93 ('W164:Y212', obj.tm.forecast_min_max_sd(region='OECD90').reset_index(drop=True), None, None),94 ('AA164:AC212', obj.tm.forecast_low_med_high(region='OECD90').reset_index(drop=True), None, None),95 ('BX168:BZ214', obj.tm.forecast_trend(region='OECD90', trend='Linear').reset_index(drop=True), None, None),96 ('CE168:CH214', obj.tm.forecast_trend(region='OECD90', trend='Degree2').reset_index(drop=True), None, None),97 ('CM168:CQ214', obj.tm.forecast_trend(region='OECD90', trend='Degree3').reset_index(drop=True), None, None),98 ('CV168:CX214', obj.tm.forecast_trend(region='OECD90', trend='Exponential').reset_index(drop=True), None, None),99 ('DZ163:EA209', obj.tm.ref_tam_per_region().reset_index().loc[:, ['Year', 'OECD90']], None, None),100 ('W228:Y276', obj.tm.forecast_min_max_sd(region='Eastern Europe').reset_index(drop=True), None, None),101 ('AA228:AC276', obj.tm.forecast_low_med_high(region='Eastern Europe').reset_index(drop=True), None, None),102 ('BX232:BZ278', obj.tm.forecast_trend(region='Eastern Europe', trend='Linear').reset_index(drop=True), None, None),103 ('CE232:CH278', obj.tm.forecast_trend(region='Eastern Europe', trend='Degree2').reset_index(drop=True), None, None),104 ('CM232:CQ278', obj.tm.forecast_trend(region='Eastern Europe', trend='Degree3').reset_index(drop=True), None, None),105 ('CV232:CX278', obj.tm.forecast_trend(region='Eastern Europe', trend='Exponential').reset_index(drop=True), None, None),106 ('DZ227:EA273', obj.tm.ref_tam_per_region().reset_index().loc[:, ['Year', 'Eastern Europe']], None, None),107 ('W291:Y339', obj.tm.forecast_min_max_sd(region='Asia (Sans Japan)').reset_index(drop=True), None, None),108 ('AA291:AC339', obj.tm.forecast_low_med_high(region='Asia (Sans Japan)').reset_index(drop=True), None, None),109 ('BX295:BZ341', obj.tm.forecast_trend(region='Asia (Sans Japan)', trend='Linear').reset_index(drop=True), None, None),110 ('CE295:CH341', obj.tm.forecast_trend(region='Asia (Sans Japan)', trend='Degree2').reset_index(drop=True), None, None),111 ('CM295:CQ341', obj.tm.forecast_trend(region='Asia (Sans Japan)', trend='Degree3').reset_index(drop=True), None, None),112 ('CV295:CX341', obj.tm.forecast_trend(region='Asia (Sans Japan)', trend='Exponential').reset_index(drop=True), None, None),113 ('DZ290:EA336', obj.tm.ref_tam_per_region().reset_index().loc[:, ['Year', 'Asia (Sans Japan)']], None, None),114 ('W354:Y402', obj.tm.forecast_min_max_sd(region='Middle East and Africa').reset_index(drop=True), None, None),115 ('AA354:AC402', obj.tm.forecast_low_med_high(region='Middle East and Africa').reset_index(drop=True), None, None),116 ('BX358:BZ404', obj.tm.forecast_trend(region='Middle East and Africa', trend='Linear').reset_index(drop=True), None, None),117 ('CE358:CH404', obj.tm.forecast_trend(region='Middle East and Africa', trend='Degree2').reset_index(drop=True), None, None),118 ('CM358:CQ404', obj.tm.forecast_trend(region='Middle East and Africa', trend='Degree3').reset_index(drop=True), None, None),119 ('CV358:CX404', obj.tm.forecast_trend(region='Middle East and Africa', trend='Exponential').reset_index(drop=True), None, None),120 ('DZ353:EA399', obj.tm.ref_tam_per_region().reset_index().loc[:, ['Year', 'Middle East and Africa']], None, None),121 ('W417:Y465', obj.tm.forecast_min_max_sd(region='Latin America').reset_index(drop=True), None, None),122 ('AA417:AC465', obj.tm.forecast_low_med_high(region='Latin America').reset_index(drop=True), None, None),123 ('BX421:BZ467', obj.tm.forecast_trend(region='Latin America', trend='Linear').reset_index(drop=True), None, None),124 ('CE421:CH467', obj.tm.forecast_trend(region='Latin America', trend='Degree2').reset_index(drop=True), None, None),125 ('CM421:CQ467', obj.tm.forecast_trend(region='Latin America', trend='Degree3').reset_index(drop=True), None, None),126 ('CV421:CX467', obj.tm.forecast_trend(region='Latin America', trend='Exponential').reset_index(drop=True), None, None),127 ('DZ416:EA462', obj.tm.ref_tam_per_region().reset_index().loc[:, ['Year', 'Latin America']], None, None),128 ('W480:Y528', obj.tm.forecast_min_max_sd(region='China').reset_index(drop=True), None, None),129 ('AA480:AC528', obj.tm.forecast_low_med_high(region='China').reset_index(drop=True), None, None),130 ('BX484:BZ530', obj.tm.forecast_trend(region='China', trend='Linear').reset_index(drop=True), None, None),131 ('CE484:CH530', obj.tm.forecast_trend(region='China', trend='Degree2').reset_index(drop=True), None, None),132 ('CM484:CQ530', obj.tm.forecast_trend(region='China', trend='Degree3').reset_index(drop=True), None, None),133 ('CV484:CX530', obj.tm.forecast_trend(region='China', trend='Exponential').reset_index(drop=True), None, None),134 ('DZ479:EA525', obj.tm.ref_tam_per_region().reset_index().loc[:, ['Year', 'China']], None, None),135 ('W544:Y592', obj.tm.forecast_min_max_sd(region='India').reset_index(drop=True), None, None),136 ('AA544:AC592', obj.tm.forecast_low_med_high(region='India').reset_index(drop=True), None, None),137 ('BX548:BZ594', obj.tm.forecast_trend(region='India', trend='Linear').reset_index(drop=True), None, None),138 ('CE548:CH594', obj.tm.forecast_trend(region='India', trend='Degree2').reset_index(drop=True), None, None),139 ('CM548:CQ594', obj.tm.forecast_trend(region='India', trend='Degree3').reset_index(drop=True), None, None),140 ('CV548:CX594', obj.tm.forecast_trend(region='India', trend='Exponential').reset_index(drop=True), None, None),141 ('DZ543:EA589', obj.tm.ref_tam_per_region().reset_index().loc[:, ['Year', 'India']], None, None),142 ('W608:Y656', obj.tm.forecast_min_max_sd(region='EU').reset_index(drop=True), None, None),143 ('AA608:AC656', obj.tm.forecast_low_med_high(region='EU').reset_index(drop=True), None, None),144 ('BX612:BZ658', obj.tm.forecast_trend(region='EU', trend='Linear').reset_index(drop=True), None, None),145 ('CE612:CH658', obj.tm.forecast_trend(region='EU', trend='Degree2').reset_index(drop=True), None, None),146 ('CM612:CQ658', obj.tm.forecast_trend(region='EU', trend='Degree3').reset_index(drop=True), None, None),147 ('CV612:CX658', obj.tm.forecast_trend(region='EU', trend='Exponential').reset_index(drop=True), None, None),148 ('DZ607:EA653', obj.tm.ref_tam_per_region().reset_index().loc[:, ['Year', 'EU']], None, None),149 ('W673:Y721', obj.tm.forecast_min_max_sd(region='USA').reset_index(drop=True), None, None),150 ('AA673:AC721', obj.tm.forecast_low_med_high(region='USA').reset_index(drop=True), None, None),151 ('BX677:BZ723', obj.tm.forecast_trend(region='USA', trend='Linear').reset_index(drop=True), None, None),152 ('CE677:CH723', obj.tm.forecast_trend(region='USA', trend='Degree2').reset_index(drop=True), None, None),153 ('CM677:CQ723', obj.tm.forecast_trend(region='USA', trend='Degree3').reset_index(drop=True), None, None),154 ('CV677:CX723', obj.tm.forecast_trend(region='USA', trend='Exponential').reset_index(drop=True), None, None),155 ('DZ672:EA718', obj.tm.ref_tam_per_region().reset_index().loc[:, ['Year', 'USA']], None, None),156 ]157 return verify158def verify_tam_data_eleven_sources(obj, verify):159 """Verified tables in TAM Data, with smaller source data area.160 Some solutions, first noticed with ImprovedCookStoves, have a smaller set of161 columns to hold data sources and this shifts all of the rest of the columns to162 the left. This test specifies the columns for this narrower layout.163 """164 func = functools.partial(obj.tm.forecast_trend, region='World')165 (d2_mask, d3_mask, exp_mask) = _get_interpolation_trend_masks(func=func)166 verify['TAM Data'] = [167 ('S46:U94', obj.tm.forecast_min_max_sd(region='World').reset_index(drop=True), None, None),168 ('W46:Y94', obj.tm.forecast_low_med_high(region='World').reset_index(drop=True), None, None),169 ('BT50:BV96', obj.tm.forecast_trend(region='World', trend='Linear').reset_index(drop=True), None, None),170 ('CA50:CD96', obj.tm.forecast_trend(region='World', trend='Degree2').reset_index(drop=True), d2_mask, None),171 ('CI50:CM96', obj.tm.forecast_trend(region='World', trend='Degree3').reset_index(drop=True), d3_mask, None),172 ('CR50:CT96', obj.tm.forecast_trend(region='World', trend='Exponential').reset_index(drop=True), exp_mask, None),173 # ('DV45:DW91', obj.tm.forecast_trend(region='World', ).reset_index().loc[:, ['Year', 'adoption']], None), first year differs174 # TODO Figure out PDS TAM handling175 ('S164:U212', obj.tm.forecast_min_max_sd(region='OECD90').reset_index(drop=True), None, None),176 ('W164:Y212', obj.tm.forecast_low_med_high(region='OECD90').reset_index(drop=True), None, None),177 ('BT168:BV214', obj.tm.forecast_trend(region='OECD90', trend='Linear').reset_index(drop=True), None, None),178 ('CA168:CD214', obj.tm.forecast_trend(region='OECD90', trend='Degree2').reset_index(drop=True), None, None),179 ('CI168:CM214', obj.tm.forecast_trend(region='OECD90', trend='Degree3').reset_index(drop=True), None, None),180 ('CR168:CT214', obj.tm.forecast_trend(region='OECD90', trend='Exponential').reset_index(drop=True), None, None),181 # ('DV163:DW209', obj.tm.forecast_trend(region='OECD90', ).reset_index().loc[:, ['Uear', 'adoption']], None), first year differs182 ('S228:U276', obj.tm.forecast_min_max_sd(region='Eastern Europe').reset_index(drop=True), None, None),183 ('W228:Y276', obj.tm.forecast_low_med_high(region='Eastern Europe').reset_index(drop=True), None, None),184 ('BT232:BV278', obj.tm.forecast_trend(region='Eastern Europe', trend='Linear').reset_index(drop=True), None, None),185 ('CA232:CD278', obj.tm.forecast_trend(region='Eastern Europe', trend='Degree2').reset_index(drop=True), None, None),186 ('CI232:CM278', obj.tm.forecast_trend(region='Eastern Europe', trend='Degree3').reset_index(drop=True), None, None),187 ('CR232:CT278', obj.tm.forecast_trend(region='Eastern Europe', trend='Exponential').reset_index(drop=True), None, None),188 # ('DV227:DW273', obj.tm.forecast_trend(region='Eastern Europe', ).reset_index().loc[:, ['Uear', 'adoption']], None), first year differs189 ('S291:U339', obj.tm.forecast_min_max_sd(region='Asia (Sans Japan)').reset_index(drop=True), None, None),190 ('W291:Y339', obj.tm.forecast_low_med_high(region='Asia (Sans Japan)').reset_index(drop=True), None, None),191 ('BT295:BV341', obj.tm.forecast_trend(region='Asia (Sans Japan)', trend='Linear').reset_index(drop=True), None, None),192 ('CA295:CD341', obj.tm.forecast_trend(region='Asia (Sans Japan)', trend='Degree2').reset_index(drop=True), None, None),193 ('CI295:CM341', obj.tm.forecast_trend(region='Asia (Sans Japan)', trend='Degree3').reset_index(drop=True), None, None),194 ('CR295:CT341', obj.tm.forecast_trend(region='Asia (Sans Japan)', trend='Exponential').reset_index(drop=True), None, None),195 # ('DV290:DW336', obj.tm.forecast_trend(region='Asia (Sans Japan)', ).reset_index().loc[:, ['Uear', 'adoption']], None), first year differs196 ('S354:U402', obj.tm.forecast_min_max_sd(region='Middle East and Africa').reset_index(drop=True), None, None),197 ('W354:Y402', obj.tm.forecast_low_med_high(region='Middle East and Africa').reset_index(drop=True), None, None),198 ('BT358:BV404', obj.tm.forecast_trend(region='Middle East and Africa', trend='Linear').reset_index(drop=True), None, None),199 ('CA358:CD404', obj.tm.forecast_trend(region='Middle East and Africa', trend='Degree2').reset_index(drop=True), None, None),200 ('CI358:CM404', obj.tm.forecast_trend(region='Middle East and Africa', trend='Degree3').reset_index(drop=True), None, None),201 ('CR358:CT404', obj.tm.forecast_trend(region='Middle East and Africa', trend='Exponential').reset_index(drop=True), None, None),202 # ('DV353:DW399', obj.tm.forecast_trend(region='Middle East and Africa', ).reset_index().loc[:, ['Uear', 'adoption']], None), first year differs203 ('S417:U465', obj.tm.forecast_min_max_sd(region='Latin America').reset_index(drop=True), None, None),204 ('W417:Y465', obj.tm.forecast_low_med_high(region='Latin America').reset_index(drop=True), None, None),205 ('BT421:BV467', obj.tm.forecast_trend(region='Latin America', trend='Linear').reset_index(drop=True), None, None),206 ('CA421:CD467', obj.tm.forecast_trend(region='Latin America', trend='Degree2').reset_index(drop=True), None, None),207 ('CI421:CM467', obj.tm.forecast_trend(region='Latin America', trend='Degree3').reset_index(drop=True), None, None),208 ('CR421:CT467', obj.tm.forecast_trend(region='Latin America', trend='Exponential').reset_index(drop=True), None, None),209 # ('DV416:DW465', obj.tm.forecast_trend(region='Latin America', ).reset_index().loc[:, ['Uear', 'adoption']], None), first year differs210 ('S480:U528', obj.tm.forecast_min_max_sd(region='China').reset_index(drop=True), None, None),211 ('W480:Y528', obj.tm.forecast_low_med_high(region='China').reset_index(drop=True), None, None),212 ('BT484:BV530', obj.tm.forecast_trend(region='China', trend='Linear').reset_index(drop=True), None, None),213 ('CA484:CD530', obj.tm.forecast_trend(region='China', trend='Degree2').reset_index(drop=True), None, None),214 ('CI484:CM530', obj.tm.forecast_trend(region='China', trend='Degree3').reset_index(drop=True), None, None),215 ('CR484:CT530', obj.tm.forecast_trend(region='China', trend='Exponential').reset_index(drop=True), None, None),216 # ('DV479:DW525', obj.tm.forecast_trend(region='China', ).reset_index().loc[:, ['Uear', 'adoption']], None), first year differs217 ('S544:U592', obj.tm.forecast_min_max_sd(region='India').reset_index(drop=True), None, None),218 ('W544:Y592', obj.tm.forecast_low_med_high(region='India').reset_index(drop=True), None, None),219 ('BT548:BV594', obj.tm.forecast_trend(region='India', trend='Linear').reset_index(drop=True), None, None),220 ('CA548:CD594', obj.tm.forecast_trend(region='India', trend='Degree2').reset_index(drop=True), None, None),221 ('CI548:CM594', obj.tm.forecast_trend(region='India', trend='Degree3').reset_index(drop=True), None, None),222 ('CR548:CT594', obj.tm.forecast_trend(region='India', trend='Exponential').reset_index(drop=True), None, None),223 # ('DV543:DW591', obj.tm.forecast_trend(region='India', ).reset_index().loc[:, ['Uear', 'adoption']], None), first year differs224 ('S608:U656', obj.tm.forecast_min_max_sd(region='EU').reset_index(drop=True), None, None),225 ('W608:Y656', obj.tm.forecast_low_med_high(region='EU').reset_index(drop=True), None, None),226 ('BT612:BV658', obj.tm.forecast_trend(region='EU', trend='Linear').reset_index(drop=True), None, None),227 ('CA612:CD658', obj.tm.forecast_trend(region='EU', trend='Degree2').reset_index(drop=True), None, None),228 ('CI612:CM658', obj.tm.forecast_trend(region='EU', trend='Degree3').reset_index(drop=True), None, None),229 ('CR612:CT658', obj.tm.forecast_trend(region='EU', trend='Exponential').reset_index(drop=True), None, None),230 # ('DV607:DW653', obj.tm.forecast_trend(region='EU', ).reset_index().loc[:, ['Uear', 'adoption']], None), first year differs231 ('S673:U721', obj.tm.forecast_min_max_sd(region='USA').reset_index(drop=True), None, None),232 ('W673:Y721', obj.tm.forecast_low_med_high(region='USA').reset_index(drop=True), None, None),233 ('BT677:BV723', obj.tm.forecast_trend(region='USA', trend='Linear').reset_index(drop=True), None, None),234 ('CA677:CD723', obj.tm.forecast_trend(region='USA', trend='Degree2').reset_index(drop=True), None, None),235 ('CI677:CM723', obj.tm.forecast_trend(region='USA', trend='Degree3').reset_index(drop=True), None, None),236 ('CR677:CT723', obj.tm.forecast_trend(region='USA', trend='Exponential').reset_index(drop=True), None, None),237 # ('DV672:DW718', obj.tm.forecast_trend(region='USA', ).reset_index().loc[:, ['Uear', 'adoption']], None), first year differs238 ]239 return verify240def verify_adoption_data(obj, verify):241 """Verified tables in Adoption Data."""242 func = functools.partial(obj.ad.adoption_trend, region='World')243 (d2_mask, d3_mask, exp_mask) = _get_interpolation_trend_masks(func=func)244 verify['Adoption Data'] = [245 ('X46:Z94', obj.ad.adoption_min_max_sd(region='World').reset_index(drop=True), None, None),246 ('AB46:AD94', obj.ad.adoption_low_med_high(region='World').reset_index(drop=True), None, None),247 ('BY50:CA96', obj.ad.adoption_trend(region='World', trend='Linear').reset_index(drop=True), None, None),248 ('CF50:CI96', obj.ad.adoption_trend(region='World', trend='Degree2').reset_index(drop=True), d2_mask, None),249 ('CN50:CR96', obj.ad.adoption_trend(region='World', trend='Degree3').reset_index(drop=True), d3_mask, None),250 ('CW50:CY96', obj.ad.adoption_trend(region='World', trend='Exponential').reset_index(drop=True), exp_mask, None),251 #('EA45:EB91', obj.ad.adoption_trend(region='World').reset_index().loc[:, ['Year', 'adoption']], None),252 ('X106:Z154', obj.ad.adoption_min_max_sd(region='OECD90').reset_index(drop=True), None, None),253 ('AB106:AD154', obj.ad.adoption_low_med_high(region='OECD90').reset_index(drop=True), None, None),254 ('BY110:CA156', obj.ad.adoption_trend(region='OECD90', trend='Linear').reset_index(drop=True), None, None),255 ('CF110:CI156', obj.ad.adoption_trend(region='OECD90', trend='Degree2').reset_index(drop=True), None, None),256 ('CN110:CR156', obj.ad.adoption_trend(region='OECD90', trend='Degree3').reset_index(drop=True), None, None),257 ('CW110:CY156', obj.ad.adoption_trend(region='OECD90', trend='Exponential').reset_index(drop=True), None, None),258 #('EA105:EB151', obj.ad.adoption_trend(region='OECD90').reset_index().loc[:, ['Year', 'adoption']], None),259 ('X170:Z218', obj.ad.adoption_min_max_sd(region='Eastern Europe').reset_index(drop=True), None, None),260 ('AB170:AD218', obj.ad.adoption_low_med_high(region='Eastern Europe').reset_index(drop=True), None, None),261 ('BY174:CA220', obj.ad.adoption_trend(region='Eastern Europe', trend='Linear').reset_index(drop=True), None, None),262 ('CF174:CI220', obj.ad.adoption_trend(region='Eastern Europe', trend='Degree2').reset_index(drop=True), None, None),263 ('CN174:CR220', obj.ad.adoption_trend(region='Eastern Europe', trend='Degree3').reset_index(drop=True), None, None),264 ('CW174:CY220', obj.ad.adoption_trend(region='Eastern Europe', trend='Exponential').reset_index(drop=True), None, None),265 #('EA169:EB217', obj.ad.adoption_trend(region='Eastern Europe').reset_index().loc[:, ['Year', 'adoption']], None),266 ('X233:Z281', obj.ad.adoption_min_max_sd(region='Asia (Sans Japan)').reset_index(drop=True), None, None),267 ('AB233:AD281', obj.ad.adoption_low_med_high(region='Asia (Sans Japan)').reset_index(drop=True), None, None),268 ('BY237:CA283', obj.ad.adoption_trend(region='Asia (Sans Japan)', trend='Linear').reset_index(drop=True), None, None),269 ('CF237:CI283', obj.ad.adoption_trend(region='Asia (Sans Japan)', trend='Degree2').reset_index(drop=True), None, None),270 ('CN237:CR283', obj.ad.adoption_trend(region='Asia (Sans Japan)', trend='Degree3').reset_index(drop=True), None, None),271 ('CW237:CY283', obj.ad.adoption_trend(region='Asia (Sans Japan)', trend='Exponential').reset_index(drop=True), None, None),272 #('EA232:EB278', obj.ad.adoption_trend(region='Asia (Sans Japan)').reset_index().loc[:, ['Year', 'adoption']], None),273 ('X296:Z344', obj.ad.adoption_min_max_sd(region='Middle East and Africa').reset_index(drop=True), None, None),274 ('AB296:AD344', obj.ad.adoption_low_med_high(region='Middle East and Africa').reset_index(drop=True), None, None),275 ('BY300:CA346', obj.ad.adoption_trend(region='Middle East and Africa', trend='Linear').reset_index(drop=True), None, None),276 ('CF300:CI346', obj.ad.adoption_trend(region='Middle East and Africa', trend='Degree2').reset_index(drop=True), None, None),277 ('CN300:CR346', obj.ad.adoption_trend(region='Middle East and Africa', trend='Degree3').reset_index(drop=True), None, None),278 ('CW300:CY346', obj.ad.adoption_trend(region='Middle East and Africa', trend='Exponential').reset_index(drop=True), None, None),279 #('EA295:EB341', obj.ad.adoption_trend(region='Middle East and Africa').reset_index().loc[:, ['Year', 'adoption']], None),280 ('X359:Z407', obj.ad.adoption_min_max_sd(region='Latin America').reset_index(drop=True), None, None),281 ('AB359:AD407', obj.ad.adoption_low_med_high(region='Latin America').reset_index(drop=True), None, None),282 ('BY363:CA409', obj.ad.adoption_trend(region='Latin America', trend='Linear').reset_index(drop=True), None, None),283 ('CF363:CI409', obj.ad.adoption_trend(region='Latin America', trend='Degree2').reset_index(drop=True), None, None),284 ('CN363:CR409', obj.ad.adoption_trend(region='Latin America', trend='Degree3').reset_index(drop=True), None, None),285 ('CW363:CY409', obj.ad.adoption_trend(region='Latin America', trend='Exponential').reset_index(drop=True), None, None),286 #('EA358:EB404', obj.ad.adoption_trend(region='Latin America').reset_index().loc[:, ['Year', 'adoption']], None),287 ('X422:Z470', obj.ad.adoption_min_max_sd(region='China').reset_index(drop=True), None, None),288 ('AB422:AD470', obj.ad.adoption_low_med_high(region='China').reset_index(drop=True), None, None),289 ('BY426:CA472', obj.ad.adoption_trend(region='China', trend='Linear').reset_index(drop=True), None, None),290 ('CF426:CI472', obj.ad.adoption_trend(region='China', trend='Degree2').reset_index(drop=True), None, None),291 ('CN426:CR472', obj.ad.adoption_trend(region='China', trend='Degree3').reset_index(drop=True), None, None),292 ('CW426:CY472', obj.ad.adoption_trend(region='China', trend='Exponential').reset_index(drop=True), None, None),293 #('EA421:EB467', obj.ad.adoption_trend(region='China').reset_index().loc[:, ['Year', 'adoption']], None),294 ('X486:Z534', obj.ad.adoption_min_max_sd(region='India').reset_index(drop=True), None, None),295 ('AB486:AD534', obj.ad.adoption_low_med_high(region='India').reset_index(drop=True), None, None),296 ('BY490:CA536', obj.ad.adoption_trend(region='India', trend='Linear').reset_index(drop=True), None, None),297 ('CF490:CI536', obj.ad.adoption_trend(region='India', trend='Degree2').reset_index(drop=True), None, None),298 ('CN490:CR536', obj.ad.adoption_trend(region='India', trend='Degree3').reset_index(drop=True), None, None),299 ('CW490:CY536', obj.ad.adoption_trend(region='India', trend='Exponential').reset_index(drop=True), None, None),300 #('EA485:EB531', obj.ad.adoption_trend(region='India').reset_index().loc[:, ['Year', 'adoption']], None),301 ('X550:Z598', obj.ad.adoption_min_max_sd(region='EU').reset_index(drop=True), None, None),302 ('AB550:AD598', obj.ad.adoption_low_med_high(region='EU').reset_index(drop=True), None, None),303 ('BY554:CA600', obj.ad.adoption_trend(region='EU', trend='Linear').reset_index(drop=True), None, None),304 ('CF554:CI600', obj.ad.adoption_trend(region='EU', trend='Degree2').reset_index(drop=True), None, None),305 ('CN554:CR600', obj.ad.adoption_trend(region='EU', trend='Degree3').reset_index(drop=True), None, None),306 ('CW554:CY600', obj.ad.adoption_trend(region='EU', trend='Exponential').reset_index(drop=True), None, None),307 #('EA549:EB595', obj.ad.adoption_trend(region='EU').reset_index().loc[:, ['Year', 'adoption']], None),308 ('X615:Z663', obj.ad.adoption_min_max_sd(region='USA').reset_index(drop=True), None, None),309 ('AB615:AD663', obj.ad.adoption_low_med_high(region='USA').reset_index(drop=True), None, None),310 ('BY619:CA665', obj.ad.adoption_trend(region='USA', trend='Linear').reset_index(drop=True), None, None),311 ('CF619:CI665', obj.ad.adoption_trend(region='USA', trend='Degree2').reset_index(drop=True), None, None),312 ('CN619:CR665', obj.ad.adoption_trend(region='USA', trend='Degree3').reset_index(drop=True), None, None),313 ('CW619:CY665', obj.ad.adoption_trend(region='USA', trend='Exponential').reset_index(drop=True), None, None),314 #('EA614:EB660', obj.ad.adoption_trend(region='USA').reset_index().loc[:, ['Year', 'adoption']], None, None),315 ]316 return verify317def verify_custom_adoption(obj, verify):318 """Verified tables in Custom * Adoption.319 Note: regional data is ignored as there are issues in the xls sheet that have320 not been replicated. See documentation of issues here:321 https://docs.google.com/document/d/19sq88J_PXY-y_EnqbSJDl0v9CdJArOdFLatNNUFhjEA/edit#heading=h.kjrqk1o5e46m322 """323 verify['Custom PDS Adoption'] = [324 ('A23:B71', obj.pds_ca.adoption_data_per_region()['World'].reset_index(), None, "Excel_NaN")325 ]326 # verify['Custom REF Adoption'] = [] # not yet implemented327 return verify328def verify_adoption_data_eleven_sources(obj, verify):329 """Verified tables in Adoption Data.330 Some solutions, first noticed with ImprovedCookStoves, have a smaller set of331 columns to hold data sources and this shifts all of the rest of the columns to332 the left. This test specifies the columns for this narrower layout.333 """334 func = functools.partial(obj.ad.adoption_trend, region='World')335 (d2_mask, d3_mask, exp_mask) = _get_interpolation_trend_masks(func=func)336 verify['Adoption Data'] = [337 ('S46:U94', obj.ad.adoption_min_max_sd(region='World').reset_index(drop=True), None, None),338 ('W46:Y94', obj.ad.adoption_low_med_high(region='World').reset_index(drop=True), None, None),339 ('BT50:BV96', obj.ad.adoption_trend(region='World', trend='Linear').reset_index(drop=True), None, None),340 ('CA50:CD96', obj.ad.adoption_trend(region='World', trend='Degree2').reset_index(drop=True), d2_mask, None),341 ('CI50:CM96', obj.ad.adoption_trend(region='World', trend='Degree3').reset_index(drop=True), d3_mask, None),342 ('CR50:CT96', obj.ad.adoption_trend(region='World', trend='Exponential').reset_index(drop=True), exp_mask, None),343 #('DV45:DW91', obj.ad.adoption_trend(region='World').reset_index().loc[:, ['Year', 'adoption']], None),344 ('S106:U154', obj.ad.adoption_min_max_sd(region='OECD90').reset_index(drop=True), None, None),345 ('W106:Y154', obj.ad.adoption_low_med_high(region='OECD90').reset_index(drop=True), None, None),346 ('BT110:BV156', obj.ad.adoption_trend(region='OECD90', trend='Linear').reset_index(drop=True), None, None),347 ('CA110:CD156', obj.ad.adoption_trend(region='OECD90', trend='Degree2').reset_index(drop=True), None, None),348 ('CI110:CM156', obj.ad.adoption_trend(region='OECD90', trend='Degree3').reset_index(drop=True), None, None),349 ('CR110:CT156', obj.ad.adoption_trend(region='OECD90', trend='Exponential').reset_index(drop=True), None, None),350 #('EA105:EB151', obj.ad.adoption_trend(region='OECD90').reset_index().loc[:, ['Year', 'adoption']], None),351 ('S170:U218', obj.ad.adoption_min_max_sd(region='Eastern Europe').reset_index(drop=True), None, None),352 ('W170:Y218', obj.ad.adoption_low_med_high(region='Eastern Europe').reset_index(drop=True), None, None),353 ('BT174:BV220', obj.ad.adoption_trend(region='Eastern Europe', trend='Linear').reset_index(drop=True), None, None),354 ('CA174:CD220', obj.ad.adoption_trend(region='Eastern Europe', trend='Degree2').reset_index(drop=True), None, None),355 ('CI174:CM220', obj.ad.adoption_trend(region='Eastern Europe', trend='Degree3').reset_index(drop=True), None, None),356 ('CR174:CT220', obj.ad.adoption_trend(region='Eastern Europe', trend='Exponential').reset_index(drop=True), None, None),357 #('EA169:EB217', obj.ad.adoption_trend(region='Eastern Europe').reset_index().loc[:, ['Year', 'adoption']], None),358 ('S233:U281', obj.ad.adoption_min_max_sd(region='Asia (Sans Japan)').reset_index(drop=True), None, None),359 ('W233:Y281', obj.ad.adoption_low_med_high(region='Asia (Sans Japan)').reset_index(drop=True), None, None),360 ('BT237:BV283', obj.ad.adoption_trend(region='Asia (Sans Japan)', trend='Linear').reset_index(drop=True), None, None),361 ('CA237:CD283', obj.ad.adoption_trend(region='Asia (Sans Japan)', trend='Degree2').reset_index(drop=True), None, None),362 ('CI237:CM283', obj.ad.adoption_trend(region='Asia (Sans Japan)', trend='Degree3').reset_index(drop=True), None, None),363 ('CR237:CT283', obj.ad.adoption_trend(region='Asia (Sans Japan)', trend='Exponential').reset_index(drop=True), None, None),364 #('EA232:EB278', obj.ad.adoption_trend(region='Asia (Sans Japan)').reset_index().loc[:, ['Year', 'adoption']], None),365 ('S296:U344', obj.ad.adoption_min_max_sd(region='Middle East and Africa').reset_index(drop=True), None, None),366 ('W296:Y344', obj.ad.adoption_low_med_high(region='Middle East and Africa').reset_index(drop=True), None, None),367 ('BT300:BV346', obj.ad.adoption_trend(region='Middle East and Africa', trend='Linear').reset_index(drop=True), None, None),368 ('CA300:CD346', obj.ad.adoption_trend(region='Middle East and Africa', trend='Degree2').reset_index(drop=True), None, None),369 ('CI300:CM346', obj.ad.adoption_trend(region='Middle East and Africa', trend='Degree3').reset_index(drop=True), None, None),370 ('CR300:CT346', obj.ad.adoption_trend(region='Middle East and Africa', trend='Exponential').reset_index(drop=True), None, None),371 #('EA295:EB341', obj.ad.adoption_trend(region='Middle East and Africa').reset_index().loc[:, ['Year', 'adoption']], None),372 ('S359:U407', obj.ad.adoption_min_max_sd(region='Latin America').reset_index(drop=True), None, None),373 ('W359:Y407', obj.ad.adoption_low_med_high(region='Latin America').reset_index(drop=True), None, None),374 ('BT363:BV409', obj.ad.adoption_trend(region='Latin America', trend='Linear').reset_index(drop=True), None, None),375 ('CA363:CD409', obj.ad.adoption_trend(region='Latin America', trend='Degree2').reset_index(drop=True), None, None),376 ('CI363:CM409', obj.ad.adoption_trend(region='Latin America', trend='Degree3').reset_index(drop=True), None, None),377 ('CR363:CT409', obj.ad.adoption_trend(region='Latin America', trend='Exponential').reset_index(drop=True), None, None),378 #('EA358:EB404', obj.ad.adoption_trend(region='Latin America').reset_index().loc[:, ['Year', 'adoption']], None),379 ('S422:U470', obj.ad.adoption_min_max_sd(region='China').reset_index(drop=True), None, None),380 ('W422:Y470', obj.ad.adoption_low_med_high(region='China').reset_index(drop=True), None, None),381 ('BT426:BV472', obj.ad.adoption_trend(region='China', trend='Linear').reset_index(drop=True), None, None),382 ('CA426:CD472', obj.ad.adoption_trend(region='China', trend='Degree2').reset_index(drop=True), None, None),383 ('CI426:CM472', obj.ad.adoption_trend(region='China', trend='Degree3').reset_index(drop=True), None, None),384 ('CR426:CT472', obj.ad.adoption_trend(region='China', trend='Exponential').reset_index(drop=True), None, None),385 #('EA421:EB467', obj.ad.adoption_trend(region='China').reset_index().loc[:, ['Year', 'adoption']], None),386 ('S486:U534', obj.ad.adoption_min_max_sd(region='India').reset_index(drop=True), None, None),387 ('W486:Y534', obj.ad.adoption_low_med_high(region='India').reset_index(drop=True), None, None),388 ('BT490:BV536', obj.ad.adoption_trend(region='India', trend='Linear').reset_index(drop=True), None, None),389 ('CA490:CD536', obj.ad.adoption_trend(region='India', trend='Degree2').reset_index(drop=True), None, None),390 ('CI490:CM536', obj.ad.adoption_trend(region='India', trend='Degree3').reset_index(drop=True), None, None),391 ('CR490:CT536', obj.ad.adoption_trend(region='India', trend='Exponential').reset_index(drop=True), None, None),392 #('EA485:EB531', obj.ad.adoption_trend(region='India').reset_index().loc[:, ['Year', 'adoption']], None),393 ('S550:U598', obj.ad.adoption_min_max_sd(region='EU').reset_index(drop=True), None, None),394 ('W550:Y598', obj.ad.adoption_low_med_high(region='EU').reset_index(drop=True), None, None),395 ('BT554:BV600', obj.ad.adoption_trend(region='EU', trend='Linear').reset_index(drop=True), None, None),396 ('CA554:CD600', obj.ad.adoption_trend(region='EU', trend='Degree2').reset_index(drop=True), None, None),397 ('CI554:CM600', obj.ad.adoption_trend(region='EU', trend='Degree3').reset_index(drop=True), None, None),398 ('CR554:CT600', obj.ad.adoption_trend(region='EU', trend='Exponential').reset_index(drop=True), None, None),399 #('EA549:EB595', obj.ad.adoption_trend(region='EU').reset_index().loc[:, ['Year', 'adoption']], None),400 ('S615:U663', obj.ad.adoption_min_max_sd(region='USA').reset_index(drop=True), None, None),401 ('W615:Y663', obj.ad.adoption_low_med_high(region='USA').reset_index(drop=True), None, None),402 ('BT619:BV665', obj.ad.adoption_trend(region='USA', trend='Linear').reset_index(drop=True), None, None),403 ('CA619:CD665', obj.ad.adoption_trend(region='USA', trend='Degree2').reset_index(drop=True), None, None),404 ('CI619:CM665', obj.ad.adoption_trend(region='USA', trend='Degree3').reset_index(drop=True), None, None),405 ('CR619:CT665', obj.ad.adoption_trend(region='USA', trend='Exponential').reset_index(drop=True), None, None),406 #('EA614:EB660', obj.ad.adoption_trend(region='USA').reset_index().loc[:, ['Year', 'adoption']], None),407 ]408 return verify409def verify_logistic_s_curve(obj, verify):410 """Verified tables in S-Curve Adoption."""411 verify['S-Curve Adoption'] = [412 ('A24:K70', obj.sc.logistic_adoption().reset_index(), None, None),413 ]414 return verify415def verify_bass_diffusion_s_curve(obj, verify):416 """Verified tables in S-Curve Adoption."""417 verify['S-Curve Adoption'] = [418 ('A130:K176', obj.sc.bass_diffusion_adoption().reset_index(), None, None),419 ]420 return verify421def verify_unit_adoption_calculations(obj, verify, include_regional_data=True, soln_type='RRS'):422 """Verified tables in Unit Adoption Calculations."""423 if hasattr(obj, 'tm'):424 ref_tam_mask = obj.tm.ref_tam_per_region().reset_index().isna()425 verify['Unit Adoption Calculations'] = [426 ('A17:K63', obj.tm.ref_tam_per_region().reset_index(), None, None),427 ('A69:K115', obj.tm.pds_tam_per_region().reset_index(), None, None)]428 else:429 ref_tam_mask = None430 verify['Unit Adoption Calculations'] = []431 if not include_regional_data or is_custom_ad_with_no_regional_data(obj):432 regional_mask = obj.ua.soln_pds_cumulative_funits().reset_index()433 regional_mask.loc[:, :] = True434 regional_mask.loc[:, ['Year', 'World']] = False435 else:436 regional_mask = None437 if ref_tam_mask is not None and regional_mask is not None:438 regional_mask |= ref_tam_mask439 # BikeInfra-RRSv1.1c-7Oct2019.xlsm catastrophic subtraction in one scenario,440 # Carpool-RRSv1.1b-Jan2020.xlsm also catastrophic cancellation in multiple scenarios.441 baseline = obj.ua.soln_net_annual_funits_adopted().mean() * 1e-6442 s = obj.ua.soln_net_annual_funits_adopted().reset_index()443 m = s.mask(s < baseline, other=True).where(s < baseline, other=False)444 soln_net_annual_funits_adopted_mask = (m | regional_mask) if regional_mask is not None else m445 verify['Unit Adoption Calculations'].extend([446 ('P17:Z63', obj.ua.ref_population().reset_index(), None, None),447 ('AB17:AL63', obj.ua.ref_gdp().reset_index(), None, None),448 ('AN17:AX63', obj.ua.ref_gdp_per_capita().reset_index(), None, None),449 ('P69:Z115', obj.ua.pds_population().reset_index(), None, None),450 ('AB69:AL115', obj.ua.pds_gdp().reset_index(), None, None),451 ('AN69:AX115', obj.ua.pds_gdp_per_capita().reset_index(), None, None),452 ('AG199:AQ244', obj.ua.soln_ref_new_iunits_reqd().reset_index(), None, None),453 ('B252:L298', obj.ua.soln_net_annual_funits_adopted().reset_index(), soln_net_annual_funits_adopted_mask, None),454 ('Q252:AA298', obj.ua.conv_ref_tot_iunits().reset_index(), ref_tam_mask, None),455 ])456 if soln_type == 'RRS':457 # Some solutions, notably HighSpeedRail, have regional results which drop to near zero458 # for a year and then bounce back. The ~0 results are the result of catastrophic459 # subtraction with only a few bits of precision, not close enough for pytest.approx.460 # Just mask those cells off.461 s = obj.ua.soln_ref_cumulative_funits().reset_index()462 soln_ref_cumulative_funits_mask = s.mask(s < 1e-11, other=True).where(s < 1e-11, other=False)463 baseline = obj.ua.soln_pds_cumulative_funits().mean() * 1e-6464 s = obj.ua.soln_pds_cumulative_funits().reset_index()465 m = s.mask(s < baseline, other=True).where(s < baseline, other=False)466 soln_pds_cumulative_funits_mask = m | regional_mask if regional_mask is not None else m467 # Carpool-RRSv1.1b-Jan2020.xlsm catastrophic cancellation in multiple scenarios.468 baseline = obj.ua.conv_ref_annual_tot_iunits().mean() * 1e-6469 s = obj.ua.conv_ref_annual_tot_iunits().reset_index()470 m = s.mask(s < baseline, other=True).where(s < baseline, other=False)471 conv_ref_annual_tot_iunits_mask = (m | regional_mask) if regional_mask is not None else m472 baseline = obj.ua.soln_pds_fuel_units_avoided().mean() * 1e-6473 s = obj.ua.soln_pds_fuel_units_avoided().reset_index()474 m = s.mask(s < baseline, other=True).where(s < baseline, other=False)475 soln_pds_fuel_units_avoided_mask = (m | regional_mask) if regional_mask is not None else m476 # Alternative Cement477 baseline = obj.ua.conv_ref_new_iunits().mean() * 1e-6478 s = obj.ua.conv_ref_new_iunits().reset_index()479 m = s.mask(s < baseline, other=True).where(s < baseline, other=False)480 conv_ref_new_iunits_mask = (m | regional_mask) if regional_mask is not None else m481 baseline = obj.ua.soln_pds_direct_co2_emissions_saved().mean() * 1e-6482 s = obj.ua.soln_pds_direct_co2_emissions_saved().reset_index()483 m = s.mask(s < baseline, other=True).where(s < baseline, other=False)484 soln_pds_direct_co2_emissions_saved_mask = (m | regional_mask) if regional_mask is not None else m485 verify['Unit Adoption Calculations'].extend([486 ('BA17:BK63', obj.ua.ref_tam_per_capita().reset_index(), None, None),487 ('BM17:BW63', obj.ua.ref_tam_per_gdp_per_capita().reset_index(), None, None),488 ('BY17:CI63', obj.ua.ref_tam_growth().reset_index(), None, None),489 ('BA69:BK115', obj.ua.pds_tam_per_capita().reset_index(), None, None),490 ('BM69:BW115', obj.ua.pds_tam_per_gdp_per_capita().reset_index(), None, None),491 ('BY69:CI115', obj.ua.pds_tam_growth().reset_index(), None, None),492 # ('B135:L181' tested in 'Helper Tables'!C91)493 ('Q135:AA181', obj.ua.soln_pds_cumulative_funits().reset_index(), soln_pds_cumulative_funits_mask, "Excel_NaN"),494 ('AX136:BH182', obj.ua.soln_pds_tot_iunits_reqd().reset_index(), regional_mask, None),495 ('AG137:AQ182', obj.ua.soln_pds_new_iunits_reqd().reset_index(), regional_mask, None),496 # ('BN136:BS182', obj.ua.soln_pds_big4_iunits_reqd().reset_index(), None, None),497 # ('B198:L244' tested in 'Helper Tables'!C27)498 ('Q198:AA244', obj.ua.soln_ref_cumulative_funits().reset_index(), soln_ref_cumulative_funits_mask, None),499 ('AX198:BH244', obj.ua.soln_ref_tot_iunits_reqd().reset_index(), None, None),500 ('AG253:AQ298', obj.ua.conv_ref_new_iunits().reset_index(), conv_ref_new_iunits_mask, None),501 ('AX252:BH298', obj.ua.conv_ref_annual_tot_iunits().reset_index(), conv_ref_annual_tot_iunits_mask, None),502 ('B308:L354', obj.ua.soln_pds_net_grid_electricity_units_saved().reset_index(), regional_mask, None),503 ('Q308:AA354', obj.ua.soln_pds_net_grid_electricity_units_used().reset_index(), regional_mask, None),504 ('AD308:AN354', obj.ua.soln_pds_fuel_units_avoided().reset_index(), soln_pds_fuel_units_avoided_mask, None),505 ('AT308:BD354', obj.ua.soln_pds_direct_co2_emissions_saved().reset_index(), soln_pds_direct_co2_emissions_saved_mask, None),506 ('BF308:BP354', obj.ua.soln_pds_direct_ch4_co2_emissions_saved().reset_index(), regional_mask, None),507 ('BR308:CB354', obj.ua.soln_pds_direct_n2o_co2_emissions_saved().reset_index(), regional_mask, None),508 ])509 elif soln_type == 'LAND_PROTECT':510 verify['Unit Adoption Calculations'].extend([511 ('CG136:CH182', obj.ua.pds_cumulative_degraded_land_unprotected().loc[:, 'World'].reset_index(), None, None),512 # ('CZ136:DA182', Not implemented513 ('DR136:DS182', obj.ua.pds_total_undegraded_land().loc[:, 'World'].reset_index(), None, None),514 ('EI136:EJ182', obj.ua.pds_cumulative_degraded_land_protected().loc[:, 'World'].reset_index(), None, None),515 ('CG198:CH244', obj.ua.ref_cumulative_degraded_land_unprotected().loc[:, 'World'].reset_index(), None, None),516 # ('CZ198:DA244', Not implemented517 ('DR198:DS244', obj.ua.ref_total_undegraded_land().loc[:, 'World'].reset_index(), None, None),518 ('EI198:EJ244', obj.ua.ref_cumulative_degraded_land_protected().loc[:, 'World'].reset_index(), None, None),519 ('B252:C298', obj.ua.net_annual_land_units_adopted().loc[:, 'World'].reset_index(), None, None),520 ('Q252:R298', obj.ua.conv_ref_tot_iunits().loc[:, 'World'].reset_index(), None, None),521 ('AG253:AH298', obj.ua.conv_ref_new_iunits().loc[:, 'World'].reset_index(), None, None),522 # ('BO252:BP298', Not implemented523 ('CG252:CH298', obj.ua.annual_reduction_in_total_degraded_land().loc[:, 'World'].reset_index(), None, None),524 # ('CZ252:DA298', Not implemented525 ('DR252:DS298', obj.ua.cumulative_reduction_in_total_degraded_land().loc[:, 'World'].reset_index(), None, None),526 ('EI252:EJ298', obj.ua.net_land_units_after_emissions_lifetime().loc[:, 'World'].reset_index(), None, None),527 ('B308:C354', obj.ua.soln_pds_net_grid_electricity_units_saved().loc[:, 'World'].reset_index(), regional_mask, None),528 ('Q308:R354', obj.ua.soln_pds_net_grid_electricity_units_used().loc[:, 'World'].reset_index(), regional_mask, None),529 ('AD308:AE354', obj.ua.soln_pds_fuel_units_avoided().loc[:, 'World'].reset_index(), regional_mask, None),530 ('AT308:AU354', obj.ua.direct_co2eq_emissions_saved_land().loc[:, 'World'].reset_index(), None, None),531 ('BF308:BG354', obj.ua.direct_co2_emissions_saved_land().loc[:, 'World'].reset_index(), None, None),532 ('BR308:BS354', obj.ua.direct_n2o_co2_emissions_saved_land().loc[:, 'World'].reset_index(), None, None),533 ('CD308:CE354', obj.ua.direct_ch4_co2_emissions_saved_land().loc[:, 'World'].reset_index(), None, None),534 ])535 elif soln_type == 'LAND_BIOSEQ':536 verify['Unit Adoption Calculations'].extend([537 ('EH137:EI182', obj.ua.soln_pds_annual_land_area_harvested().loc[:, 'World'].reset_index(), None, None),538 ('EI253:EJ298', obj.ua.net_land_units_after_emissions_lifetime().loc[2015:, 'World'].reset_index(), None, None),539 ('B308:C354', obj.ua.soln_pds_net_grid_electricity_units_saved().loc[:, 'World'].reset_index(), regional_mask, None),540 ('Q308:R354', obj.ua.soln_pds_net_grid_electricity_units_used().loc[:, 'World'].reset_index(), regional_mask, None),541 ('AD308:AE354', obj.ua.soln_pds_fuel_units_avoided().loc[:, 'World'].reset_index(), regional_mask, None),542 ('AT308:AU354', obj.ua.direct_co2eq_emissions_saved_land().loc[:, 'World'].reset_index(), None, None),543 ('BF308:BG354', obj.ua.direct_co2_emissions_saved_land().loc[:, 'World'].reset_index(), None, None),544 ('BR308:BS354', obj.ua.direct_n2o_co2_emissions_saved_land().loc[:, 'World'].reset_index(), None, None),545 ('CD308:CE354', obj.ua.direct_ch4_co2_emissions_saved_land().loc[:, 'World'].reset_index(), None, None),546 ])547 return verify548def verify_helper_tables(obj, verify, include_regional_data=True):549 """Verified tables in Helper Tables."""550 verify['Helper Tables'] = []551 if include_regional_data:552 verify['Helper Tables'].append(553 ('B91:L137', obj.ht.soln_pds_funits_adopted().reset_index(), None, None))554 else:555 verify['Helper Tables'].append(556 ('B91:C137', obj.ht.soln_pds_funits_adopted().loc[:, 'World'].reset_index(), None, None))557 verify['Helper Tables'].append(558 ('B27:L73', obj.ht.soln_ref_funits_adopted().reset_index(), None, None))559 return verify560def verify_emissions_factors(obj, verify):561 """Verified tables in Emissions Factors."""562 verify['Emissions Factors'] = [563 ('A12:K57', obj.ef.conv_ref_grid_CO2eq_per_KWh().reset_index(), None, None),564 ('A67:K112', obj.ef.conv_ref_grid_CO2_per_KWh().reset_index(), None, None),565 ]566 return verify567def verify_first_cost(obj, verify):568 """Verified tables in First Cost."""569 verify['First Cost'] = [570 ('C37:C82', obj.fc.soln_pds_install_cost_per_iunit().loc[2015:].to_frame().reset_index(drop=True), None, "Excel_one_cent"),571 # ('D37:D82', checked by 'Unit Adoption Calculations'!AH137572 ('E37:E82', obj.fc.soln_pds_annual_world_first_cost().loc[2015:].to_frame().reset_index(drop=True), None, "Excel_one_cent"),573 ('F37:F82', obj.fc.soln_pds_cumulative_install().loc[2015:].to_frame().reset_index(drop=True), None, "Excel_one_cent"),574 ('L37:L82', obj.fc.soln_ref_install_cost_per_iunit().loc[2015:].to_frame().reset_index(drop=True), None, "Excel_one_cent"),575 # ('M37:M82', checked by 'Unit Adoption Calculations'!AH199576 ('N37:N82', obj.fc.soln_ref_annual_world_first_cost().loc[2015:].to_frame().reset_index(drop=True), None, "Excel_one_cent"),577 ('O37:O82', obj.fc.conv_ref_install_cost_per_iunit().loc[2015:].to_frame().reset_index(drop=True), None, "Excel_one_cent"),578 # ('P37:P82', checked by 'Unit Adoption Calculations'!AH253579 ('Q37:Q82', obj.fc.conv_ref_annual_world_first_cost().loc[2015:].to_frame().reset_index(drop=True), None, "Excel_one_cent"),580 ('R37:R82', obj.fc.ref_cumulative_install().loc[2015:].to_frame().reset_index(drop=True), None, "Excel_one_cent")581 ]582 return verify583def verify_operating_cost(obj, verify):584 """Verified tables in Operating Cost."""585 # This has been a pain point: the last year of each column in the annual_breakout has a tiny586 # remaining_lifetime which is the result of catastrophic substraction between the previous587 # values and therefore has only a few bits of precision. pytest.approx() checks for 6 digits,588 # and there aren't enough bits to even meet that requirement.589 #590 # We mask off all cells where the value is less than one cent. We assert that being off by591 # a penny at the end of the equipment lifetime is acceptable.592 s = obj.oc.soln_pds_annual_breakout().reset_index().abs()593 soln_breakout_mask = s.mask(s < 0.01, other=True).where(s < 0.01, other=False)594 s = obj.oc.conv_ref_annual_breakout().reset_index().abs()595 conv_breakout_mask = s.mask(s < 0.01, other=True).where(s < 0.01, other=False)596 baseline = obj.oc.soln_pds_new_funits_per_year().loc[2015:, 'World'].mean() * 1e-6597 s = obj.oc.soln_pds_new_funits_per_year().loc[2015:, ['World']].reset_index(drop=True)598 soln_pds_new_funits_per_year_mask = s.mask(s < baseline, other=True).where(s < baseline, other=False)599 verify['Operating Cost'] = [600 ('B262:AV386', obj.oc.soln_pds_annual_breakout().reset_index(), soln_breakout_mask, None),601 ('B399:AV523', obj.oc.conv_ref_annual_breakout().reset_index(), conv_breakout_mask, None),602 # ('B19:B64', Not implemented, model never uses it.603 # ('C19:C64', checked by 'Unit Adoption Calculations'!C253604 ('D19:D64', obj.oc.soln_pds_annual_operating_cost().loc[2015:2060].to_frame().reset_index(drop=True), None, "Excel_one_cent"),605 ('E19:E64', obj.oc.soln_pds_cumulative_operating_cost().loc[2015:2060].to_frame().reset_index(drop=True), None, "Excel_one_cent"),606 ('F19:F64', obj.oc.soln_pds_new_funits_per_year().loc[2015:, ['World']].reset_index(drop=True), soln_pds_new_funits_per_year_mask, None),607 # ('I19:I64', Not implemented, model never uses it.608 # ('J19:J64', checked by 'Unit Adoption Calculations'!C253609 ('K19:K64', obj.oc.conv_ref_annual_operating_cost().to_frame().reset_index(drop=True), None, "Excel_one_cent"),610 ('L19:L64', obj.oc.conv_ref_cumulative_operating_cost().to_frame().reset_index(drop=True), None, "Excel_one_cent"),611 # ('B69:B114', equal to D19:D64,612 # ('C69:C114', equal to K19:K64,613 ('D69:D114', obj.oc.marginal_annual_operating_cost().to_frame().reset_index(drop=True), None, "Excel_one_cent"),614 ('B126:B250', obj.oc.soln_marginal_first_cost().to_frame().reset_index(drop=True), None, "Excel_one_cent"),615 ('C126:C250', obj.oc.soln_marginal_operating_cost_savings().to_frame().reset_index(drop=True), None, "Excel_one_cent"),616 ('D126:D250', obj.oc.soln_net_cash_flow().to_frame().reset_index(drop=True), None, "Excel_one_cent"),617 ('E126:E250', obj.oc.soln_net_present_value().to_frame().reset_index(drop=True), None, "Excel_one_cent"),618 ('I126:I250', obj.oc.soln_vs_conv_single_iunit_cashflow().to_frame().reset_index(drop=True), None, None),619 ('J126:J250', obj.oc.soln_vs_conv_single_iunit_npv().to_frame().reset_index(drop=True), None, None),620 #('K126:K250', obj.oc.soln_vs_conv_single_iunit_payback().to_frame().reset_index(drop=True), None, None),621 #('L126:L250', obj.oc.soln_vs_conv_single_iunit_payback_discounted().to_frame().reset_index(drop=True), None, None),622 ('M126:M250', obj.oc.soln_only_single_iunit_cashflow().to_frame().reset_index(drop=True), None, None),623 ('N126:N250', obj.oc.soln_only_single_iunit_npv().to_frame().reset_index(drop=True), None, None),624 #('O126:O250', obj.oc.soln_only_single_iunit_payback().to_frame().reset_index(drop=True), None, None),625 #('P126:P250', obj.oc.soln_only_single_iunit_payback_discounted().to_frame().reset_index(drop=True), None, None),626 ]627 return verify628def verify_co2_calcs(obj, verify, shifted=False, include_regional_data=True,629 is_rrs=True, cohort=2018):630 """Verified tables in CO2 Calcs."""631 if include_regional_data == False:632 regional_mask = obj.c2.co2_mmt_reduced().loc[2015:].reset_index()633 regional_mask.loc[:, :] = True634 regional_mask.loc[:, ['Year', 'World']] = False635 else:636 regional_mask = None637 # similar to operating cost, some co2 calcs values are very slightly offset from zero due to638 # floating point errors. We mask the problematic tables when they are close to 0.639 s = obj.c2.co2eq_mmt_reduced().reset_index().abs()640 near_zero_mask = s.mask(s < 0.01, other=True).where(s < 0.01, other=False)641 if regional_mask is not None:642 near_zero_mask = near_zero_mask | regional_mask643 # Alternative Cement644 baseline = obj.c2.co2eq_direct_reduced_emissions().loc[2015:].mean() * 1e-6645 s = obj.c2.co2eq_direct_reduced_emissions().loc[2015:].reset_index()646 mask = s.mask(s < baseline, other=True).where(s < baseline, other=False)647 if regional_mask is not None:648 mask = mask | regional_mask649 co2eq_direct_reduced_emissions_mask = mask650 baseline = obj.c2.co2eq_reduced_fuel_emissions().loc[2015:].mean() * 1e-6651 s = obj.c2.co2eq_reduced_fuel_emissions().loc[2015:].reset_index()652 mask = s.mask(s < baseline, other=True).where(s < baseline, other=False)653 if regional_mask is not None:654 mask = mask | regional_mask655 co2eq_reduced_fuel_emissions_mask = mask656 if is_rrs:657 verify['CO2 Calcs'] = [658 ('A235:K280', obj.c2.co2_reduced_grid_emissions().loc[2015:].reset_index(), regional_mask, None),659 ('R235:AB280', obj.c2.co2_replaced_grid_emissions().loc[2015:].reset_index(), regional_mask, None),660 ('AI235:AS280', obj.c2.co2_increased_grid_usage_emissions().loc[2015:].reset_index(), regional_mask, None),661 ('A289:K334', obj.c2.co2eq_reduced_grid_emissions().loc[2015:].reset_index(), regional_mask, None),662 ('R289:AB334', obj.c2.co2eq_replaced_grid_emissions().loc[2015:].reset_index(), regional_mask, None),663 ('AI289:AS334', obj.c2.co2eq_increased_grid_usage_emissions().loc[2015:].reset_index(), regional_mask, None),664 ('A345:K390', obj.c2.co2eq_direct_reduced_emissions().loc[2015:].reset_index(), co2eq_direct_reduced_emissions_mask, None),665 ]666 if shifted:667 # Some spreadsheets have the last two blocks shifted by several cells668 verify['CO2 Calcs'].extend([669 ('R345:AB390', obj.c2.co2eq_reduced_fuel_emissions().loc[2015:].reset_index(), co2eq_reduced_fuel_emissions_mask, None),670 ('AM345:AW390', obj.c2.co2eq_net_indirect_emissions().loc[2015:].reset_index(), regional_mask, None)671 ])672 else:673 verify['CO2 Calcs'].extend([674 ('U345:AE390', obj.c2.co2eq_reduced_fuel_emissions().loc[2015:].reset_index(), co2eq_reduced_fuel_emissions_mask, None),675 ('AP345:AZ390', obj.c2.co2eq_net_indirect_emissions().loc[2015:].reset_index(), regional_mask, None)676 ])677 verify['CO2 Calcs'].extend([678 ('A10:K55', obj.c2.co2_mmt_reduced().loc[2015:].reset_index(), regional_mask, None),679 ('A120:AW165', obj.c2.co2_ppm_calculator().loc[2015:].reset_index(), None, None),680 ('A65:K110', obj.c2.co2eq_mmt_reduced().loc[2015:].reset_index(), regional_mask, None),681 ('A172:F217', obj.c2.co2eq_ppm_calculator().loc[2015:].reset_index(), None, None),682 ])683 else:684 s = obj.c2.co2_ppm_calculator().loc[2015:].reset_index().abs()685 ppm_near_zero_mask = s.mask(s < 1e-8, other=True).where(s < 1e-8, other=False)686 s = obj.c2.co2_sequestered_global().loc[2015:].reset_index().abs()687 seq_near_zero_mask = s.mask(s < 1e-8, other=True).where(s < 1e-8, other=False)688 co2_sequestered_global = obj.c2.co2_sequestered_global().copy().reset_index()689 co2_sequestered_global.drop(columns=['Global Arctic'], inplace=True)690 if cohort >= 2020:691 # 8 Thermal-Moisture Regimes in model, but Excel CO2 Calcs did not update from 6 TMRs.692 co2_sequestered_global['Temperate/Boreal-Humid'] = (693 co2_sequestered_global['Temperate-Humid'] +694 co2_sequestered_global['Boreal-Humid'])695 co2_sequestered_global.drop(columns=['Temperate-Humid', 'Boreal-Humid'], inplace=True)696 co2_sequestered_global['Temperate/Boreal-Semi-Arid'] = (697 co2_sequestered_global['Temperate-Semi-Arid'] +698 co2_sequestered_global['Boreal-Semi-Arid'])699 co2_sequestered_global.drop(columns=['Temperate-Semi-Arid',700 'Boreal-Semi-Arid'], inplace=True)701 # Put columns in the same order as Excel.702 co2_sequestered_global = co2_sequestered_global[["Year", "All", "Tropical-Humid",703 "Temperate/Boreal-Humid", "Tropical-Semi-Arid", "Temperate/Boreal-Semi-Arid",704 "Global Arid"]]705 verify['CO2 Calcs'] = [706 ('A65:K110', obj.c2.co2eq_mmt_reduced().loc[2015:].reset_index(), near_zero_mask, None),707 ('A121:G166', co2_sequestered_global, seq_near_zero_mask, None),708 ('A173:AW218', obj.c2.co2_ppm_calculator().loc[2015:].reset_index(), ppm_near_zero_mask, None),709 # CO2 eq table has an N20 column for LAND xls sheets that doesn't appear to be used, so we ignore it710 ('A225:C270', obj.c2.co2eq_ppm_calculator().loc[2015:, ['CO2-eq PPM', 'CO2 PPM']].reset_index(), None, None),711 ('E225:G270', obj.c2.co2eq_ppm_calculator().loc[2015:, ['CH4 PPB', 'CO2 RF', 'CH4 RF']].reset_index(drop=True), near_zero_mask, None)712 # All other tables are not implemented as they appear to be all 0713 ]714def verify_ch4_calcs_rrs(obj, verify):715 """Verified tables in CH4 Calcs."""716 verify['CH4 Calcs'] = [717 ('A11:K56', obj.c4.ch4_tons_reduced().loc[2015:, :].reset_index(), None, None),718 ('A65:AW110', obj.c4.ch4_ppb_calculator().loc[2015:, :].reset_index(), None, None),719 ]720 return verify721def verify_ch4_calcs_land(obj, verify):722 """Verified tables in CH4 Calcs."""723 verify['CH4 Calcs'] = [724 ('A13:B58', obj.c4.avoided_direct_emissions_ch4_land().loc[2015:, 'World'].reset_index(), None, None),725 ('A67:AW112', obj.c4.ch4_ppb_calculator().loc[2015:, :].reset_index(), None, None),726 ]727 return verify728def is_custom_ad_with_no_regional_data(obj):729 """Check for Custom PDS adoption with no regional adoption data.730 This situation is not handled well in Excel:731 https://docs.google.com/document/d/19sq88J_PXY-y_EnqbSJDl0v9CdJArOdFLatNNUFhjEA/edit#heading=h.9rp1qn24t2vi732 and results in unrealistically large regional adoption equal to the733 Total Addressable Market of that region, which will generally exceed734 the World adoption. This is impossible, the World is supposed to be735 strictly greater than the sum of all regions.736 We do not implement this handling in Python, instead the regional result737 will be NaN. For the test, if there is Custom PDS Adoption and it738 contains no regional data, we skip checking the regional results.739 """...
test_reset_index.py
Source:test_reset_index.py
...34 },35 columns=["idx", "a", "b"],36 )37 expected["idx"] = expected["idx"].apply(lambda d: Timestamp(d, tz=tz))38 tm.assert_frame_equal(df.reset_index(), expected)39 def test_reset_index_with_intervals(self):40 idx = IntervalIndex.from_breaks(np.arange(11), name="x")41 original = DataFrame({"x": idx, "y": np.arange(10)})[["x", "y"]]42 result = original.set_index("x")43 expected = DataFrame({"y": np.arange(10)}, index=idx)44 tm.assert_frame_equal(result, expected)45 result2 = result.reset_index()46 tm.assert_frame_equal(result2, original)47 def test_reset_index(self, float_frame):48 stacked = float_frame.stack()[::2]49 stacked = DataFrame({"foo": stacked, "bar": stacked})50 names = ["first", "second"]51 stacked.index.names = names52 deleveled = stacked.reset_index()53 for i, (lev, level_codes) in enumerate(54 zip(stacked.index.levels, stacked.index.codes)55 ):56 values = lev.take(level_codes)57 name = names[i]58 tm.assert_index_equal(values, Index(deleveled[name]))59 stacked.index.names = [None, None]60 deleveled2 = stacked.reset_index()61 tm.assert_series_equal(62 deleveled["first"], deleveled2["level_0"], check_names=False63 )64 tm.assert_series_equal(65 deleveled["second"], deleveled2["level_1"], check_names=False66 )67 # default name assigned68 rdf = float_frame.reset_index()69 exp = Series(float_frame.index.values, name="index")70 tm.assert_series_equal(rdf["index"], exp)71 # default name assigned, corner case72 df = float_frame.copy()73 df["index"] = "foo"74 rdf = df.reset_index()75 exp = Series(float_frame.index.values, name="level_0")76 tm.assert_series_equal(rdf["level_0"], exp)77 # but this is ok78 float_frame.index.name = "index"79 deleveled = float_frame.reset_index()80 tm.assert_series_equal(deleveled["index"], Series(float_frame.index))81 tm.assert_index_equal(deleveled.index, Index(np.arange(len(deleveled))))82 # preserve column names83 float_frame.columns.name = "columns"84 resetted = float_frame.reset_index()85 assert resetted.columns.name == "columns"86 # only remove certain columns87 df = float_frame.reset_index().set_index(["index", "A", "B"])88 rs = df.reset_index(["A", "B"])89 # TODO should reset_index check_names ?90 tm.assert_frame_equal(rs, float_frame, check_names=False)91 rs = df.reset_index(["index", "A", "B"])92 tm.assert_frame_equal(rs, float_frame.reset_index(), check_names=False)93 rs = df.reset_index(["index", "A", "B"])94 tm.assert_frame_equal(rs, float_frame.reset_index(), check_names=False)95 rs = df.reset_index("A")96 xp = float_frame.reset_index().set_index(["index", "B"])97 tm.assert_frame_equal(rs, xp, check_names=False)98 # test resetting in place99 df = float_frame.copy()100 resetted = float_frame.reset_index()101 return_value = df.reset_index(inplace=True)102 assert return_value is None103 tm.assert_frame_equal(df, resetted, check_names=False)104 df = float_frame.reset_index().set_index(["index", "A", "B"])105 rs = df.reset_index("A", drop=True)106 xp = float_frame.copy()107 del xp["A"]108 xp = xp.set_index(["B"], append=True)109 tm.assert_frame_equal(rs, xp, check_names=False)110 def test_reset_index_name(self):111 df = DataFrame(112 [[1, 2, 3, 4], [5, 6, 7, 8]],113 columns=["A", "B", "C", "D"],114 index=Index(range(2), name="x"),115 )116 assert df.reset_index().index.name is None117 assert df.reset_index(drop=True).index.name is None118 return_value = df.reset_index(inplace=True)119 assert return_value is None120 assert df.index.name is None121 def test_reset_index_level(self):122 df = DataFrame([[1, 2, 3, 4], [5, 6, 7, 8]], columns=["A", "B", "C", "D"])123 for levels in ["A", "B"], [0, 1]:124 # With MultiIndex125 result = df.set_index(["A", "B"]).reset_index(level=levels[0])126 tm.assert_frame_equal(result, df.set_index("B"))127 result = df.set_index(["A", "B"]).reset_index(level=levels[:1])128 tm.assert_frame_equal(result, df.set_index("B"))129 result = df.set_index(["A", "B"]).reset_index(level=levels)130 tm.assert_frame_equal(result, df)131 result = df.set_index(["A", "B"]).reset_index(level=levels, drop=True)132 tm.assert_frame_equal(result, df[["C", "D"]])133 # With single-level Index (GH 16263)134 result = df.set_index("A").reset_index(level=levels[0])135 tm.assert_frame_equal(result, df)136 result = df.set_index("A").reset_index(level=levels[:1])137 tm.assert_frame_equal(result, df)138 result = df.set_index(["A"]).reset_index(level=levels[0], drop=True)139 tm.assert_frame_equal(result, df[["B", "C", "D"]])140 # Missing levels - for both MultiIndex and single-level Index:141 for idx_lev in ["A", "B"], ["A"]:142 with pytest.raises(KeyError, match=r"(L|l)evel \(?E\)?"):143 df.set_index(idx_lev).reset_index(level=["A", "E"])144 with pytest.raises(IndexError, match="Too many levels"):145 df.set_index(idx_lev).reset_index(level=[0, 1, 2])146 def test_reset_index_right_dtype(self):147 time = np.arange(0.0, 10, np.sqrt(2) / 2)148 s1 = Series(149 (9.81 * time ** 2) / 2, index=Index(time, name="time"), name="speed"150 )151 df = DataFrame(s1)152 resetted = s1.reset_index()153 assert resetted["time"].dtype == np.float64154 resetted = df.reset_index()155 assert resetted["time"].dtype == np.float64156 def test_reset_index_multiindex_col(self):157 vals = np.random.randn(3, 3).astype(object)158 idx = ["x", "y", "z"]159 full = np.hstack(([[x] for x in idx], vals))160 df = DataFrame(161 vals,162 Index(idx, name="a"),163 columns=[["b", "b", "c"], ["mean", "median", "mean"]],164 )165 rs = df.reset_index()166 xp = DataFrame(167 full, columns=[["a", "b", "b", "c"], ["", "mean", "median", "mean"]]168 )169 tm.assert_frame_equal(rs, xp)170 rs = df.reset_index(col_fill=None)171 xp = DataFrame(172 full, columns=[["a", "b", "b", "c"], ["a", "mean", "median", "mean"]]173 )174 tm.assert_frame_equal(rs, xp)175 rs = df.reset_index(col_level=1, col_fill="blah")176 xp = DataFrame(177 full, columns=[["blah", "b", "b", "c"], ["a", "mean", "median", "mean"]]178 )179 tm.assert_frame_equal(rs, xp)180 df = DataFrame(181 vals,182 MultiIndex.from_arrays([[0, 1, 2], ["x", "y", "z"]], names=["d", "a"]),183 columns=[["b", "b", "c"], ["mean", "median", "mean"]],184 )185 rs = df.reset_index("a")186 xp = DataFrame(187 full,188 Index([0, 1, 2], name="d"),189 columns=[["a", "b", "b", "c"], ["", "mean", "median", "mean"]],190 )191 tm.assert_frame_equal(rs, xp)192 rs = df.reset_index("a", col_fill=None)193 xp = DataFrame(194 full,195 Index(range(3), name="d"),196 columns=[["a", "b", "b", "c"], ["a", "mean", "median", "mean"]],197 )198 tm.assert_frame_equal(rs, xp)199 rs = df.reset_index("a", col_fill="blah", col_level=1)200 xp = DataFrame(201 full,202 Index(range(3), name="d"),203 columns=[["blah", "b", "b", "c"], ["a", "mean", "median", "mean"]],204 )205 tm.assert_frame_equal(rs, xp)206 def test_reset_index_multiindex_nan(self):207 # GH#6322, testing reset_index on MultiIndexes208 # when we have a nan or all nan209 df = DataFrame(210 {"A": ["a", "b", "c"], "B": [0, 1, np.nan], "C": np.random.rand(3)}211 )212 rs = df.set_index(["A", "B"]).reset_index()213 tm.assert_frame_equal(rs, df)214 df = DataFrame(215 {"A": [np.nan, "b", "c"], "B": [0, 1, 2], "C": np.random.rand(3)}216 )217 rs = df.set_index(["A", "B"]).reset_index()218 tm.assert_frame_equal(rs, df)219 df = DataFrame({"A": ["a", "b", "c"], "B": [0, 1, 2], "C": [np.nan, 1.1, 2.2]})220 rs = df.set_index(["A", "B"]).reset_index()221 tm.assert_frame_equal(rs, df)222 df = DataFrame(223 {224 "A": ["a", "b", "c"],225 "B": [np.nan, np.nan, np.nan],226 "C": np.random.rand(3),227 }228 )229 rs = df.set_index(["A", "B"]).reset_index()230 tm.assert_frame_equal(rs, df)231 def test_reset_index_with_datetimeindex_cols(self):232 # GH#5818233 df = DataFrame(234 [[1, 2], [3, 4]],235 columns=date_range("1/1/2013", "1/2/2013"),236 index=["A", "B"],237 )238 result = df.reset_index()239 expected = DataFrame(240 [["A", 1, 2], ["B", 3, 4]],241 columns=["index", datetime(2013, 1, 1), datetime(2013, 1, 2)],242 )243 tm.assert_frame_equal(result, expected)244 def test_reset_index_range(self):245 # GH#12071246 df = DataFrame([[0, 0], [1, 1]], columns=["A", "B"], index=RangeIndex(stop=2))247 result = df.reset_index()248 assert isinstance(result.index, RangeIndex)249 expected = DataFrame(250 [[0, 0, 0], [1, 1, 1]],251 columns=["index", "A", "B"],252 index=RangeIndex(stop=2),253 )254 tm.assert_frame_equal(result, expected)255@pytest.mark.parametrize(256 "array, dtype",257 [258 (["a", "b"], object),259 (260 pd.period_range("12-1-2000", periods=2, freq="Q-DEC"),261 pd.PeriodDtype(freq="Q-DEC"),262 ),263 ],264)265def test_reset_index_dtypes_on_empty_frame_with_multiindex(array, dtype):266 # GH 19602 - Preserve dtype on empty DataFrame with MultiIndex267 idx = MultiIndex.from_product([[0, 1], [0.5, 1.0], array])268 result = DataFrame(index=idx)[:0].reset_index().dtypes269 expected = Series({"level_0": np.int64, "level_1": np.float64, "level_2": dtype})...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!