| Conditions | 3 | 
| Total Lines | 180 | 
| Code Lines | 86 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import os  | 
            ||
| 185 | def CTS_demand_scale(aggregation_level):  | 
            ||
| 186 | """  | 
            ||
| 187 | |||
| 188 | Description: caling the demand curves to the annual demand of the respective aggregation level  | 
            ||
| 189 | |||
| 190 | |||
| 191 | Parameters  | 
            ||
| 192 | ----------  | 
            ||
| 193 | aggregation_level : str  | 
            ||
| 194 | aggregation_level : str  | 
            ||
| 195 | if further processing is to be done in zensus cell level 'other'  | 
            ||
| 196 | else 'dsitrict'  | 
            ||
| 197 | |||
| 198 | Returns  | 
            ||
| 199 | -------  | 
            ||
| 200 | CTS_per_district : pandas.DataFrame  | 
            ||
| 201 | if aggregation ='district'  | 
            ||
| 202 | Profiles scaled up to annual demand  | 
            ||
| 203 | else  | 
            ||
| 204 | 0  | 
            ||
| 205 | CTS_per_grid : pandas.DataFrame  | 
            ||
| 206 | if aggregation ='district'  | 
            ||
| 207 | Profiles scaled up to annual demandd  | 
            ||
| 208 | else  | 
            ||
| 209 | 0  | 
            ||
| 210 | CTS_per_zensus : pandas.DataFrame  | 
            ||
| 211 | if aggregation ='district'  | 
            ||
| 212 | 0  | 
            ||
| 213 | else  | 
            ||
| 214 | Profiles scaled up to annual demand  | 
            ||
| 215 | |||
| 216 | """  | 
            ||
| 217 | scenarios = ["eGon2035", "eGon100RE"]  | 
            ||
| 218 | |||
| 219 | CTS_district = pd.DataFrame()  | 
            ||
| 220 | CTS_grid = pd.DataFrame()  | 
            ||
| 221 | CTS_zensus = pd.DataFrame()  | 
            ||
| 222 | |||
| 223 | for scenario in scenarios:  | 
            ||
| 224 | (  | 
            ||
| 225 | CTS_per_district,  | 
            ||
| 226 | CTS_per_grid,  | 
            ||
| 227 | CTS_per_zensus,  | 
            ||
| 228 | ) = cts_demand_per_aggregation_level(aggregation_level, scenario)  | 
            ||
| 229 | CTS_per_district = CTS_per_district.transpose()  | 
            ||
| 230 | CTS_per_grid = CTS_per_grid.transpose()  | 
            ||
| 231 | CTS_per_zensus = CTS_per_zensus.transpose()  | 
            ||
| 232 | |||
| 233 | demand = db.select_dataframe(  | 
            ||
| 234 | f"""  | 
            ||
| 235 | SELECT demand, zensus_population_id  | 
            ||
| 236 | FROM demand.egon_peta_heat  | 
            ||
| 237 | WHERE sector = 'service'  | 
            ||
| 238 |                 AND scenario = '{scenario}' | 
            ||
| 239 | ORDER BY zensus_population_id  | 
            ||
| 240 | """  | 
            ||
| 241 | )  | 
            ||
| 242 | |||
| 243 | if aggregation_level == "district":  | 
            ||
| 244 | |||
| 245 | district_heating = db.select_dataframe(  | 
            ||
| 246 | f"""  | 
            ||
| 247 | SELECT area_id, zensus_population_id  | 
            ||
| 248 | FROM demand.egon_map_zensus_district_heating_areas  | 
            ||
| 249 |                 WHERE scenario = '{scenario}' | 
            ||
| 250 | """  | 
            ||
| 251 | )  | 
            ||
| 252 | |||
| 253 | CTS_demands_district = pd.merge(  | 
            ||
| 254 | demand,  | 
            ||
| 255 | district_heating,  | 
            ||
| 256 | on="zensus_population_id",  | 
            ||
| 257 | how="inner",  | 
            ||
| 258 | )  | 
            ||
| 259 | CTS_demands_district.drop(  | 
            ||
| 260 | "zensus_population_id", axis=1, inplace=True  | 
            ||
| 261 | )  | 
            ||
| 262 | CTS_demands_district = CTS_demands_district.groupby(  | 
            ||
| 263 | "area_id"  | 
            ||
| 264 | ).sum()  | 
            ||
| 265 | |||
| 266 | CTS_per_district = pd.merge(  | 
            ||
| 267 | CTS_per_district,  | 
            ||
| 268 | CTS_demands_district[["demand"]],  | 
            ||
| 269 | how="inner",  | 
            ||
| 270 | right_on=CTS_per_district.index,  | 
            ||
| 271 | left_on=CTS_demands_district.index,  | 
            ||
| 272 | )  | 
            ||
| 273 | |||
| 274 | CTS_per_district = CTS_per_district.rename(  | 
            ||
| 275 |                 columns={"key_0": "area_id"} | 
            ||
| 276 | )  | 
            ||
| 277 |             CTS_per_district.set_index("area_id", inplace=True) | 
            ||
| 278 | |||
| 279 | CTS_per_district = CTS_per_district[  | 
            ||
| 280 | CTS_per_district.columns[:-1]  | 
            ||
| 281 | ].multiply(CTS_per_district.demand, axis=0)  | 
            ||
| 282 | |||
| 283 | CTS_per_district.insert(0, "scenario", scenario)  | 
            ||
| 284 | |||
| 285 | CTS_district = CTS_district.append(CTS_per_district)  | 
            ||
| 286 | CTS_district = CTS_district.sort_index()  | 
            ||
| 287 | |||
| 288 | |||
| 289 | |||
| 290 | mv_grid_ind = db.select_dataframe(  | 
            ||
| 291 | f"""  | 
            ||
| 292 | SELECT bus_id, a.zensus_population_id  | 
            ||
| 293 | FROM boundaries.egon_map_zensus_grid_districts a  | 
            ||
| 294 | |||
| 295 | LEFT JOIN demand.egon_map_zensus_district_heating_areas b  | 
            ||
| 296 | ON a.zensus_population_id = b.zensus_population_id  | 
            ||
| 297 | |||
| 298 | JOIN demand.egon_peta_heat c  | 
            ||
| 299 | ON a.zensus_population_id = c.zensus_population_id  | 
            ||
| 300 | |||
| 301 | 				WHERE b.scenario = '{scenario}' | 
            ||
| 302 | 				AND c.scenario = '{scenario}' | 
            ||
| 303 | AND c.sector = 'service'  | 
            ||
| 304 | """  | 
            ||
| 305 | )  | 
            ||
| 306 | |||
| 307 | CTS_demands_grid = pd.merge(  | 
            ||
| 308 | demand,  | 
            ||
| 309 | mv_grid_ind[["bus_id", "zensus_population_id"]],  | 
            ||
| 310 | on="zensus_population_id",  | 
            ||
| 311 | how="inner",  | 
            ||
| 312 | )  | 
            ||
| 313 | |||
| 314 |             CTS_demands_grid.drop("zensus_population_id", axis=1, inplace=True) | 
            ||
| 315 |             CTS_demands_grid = CTS_demands_grid.groupby("bus_id").sum() | 
            ||
| 316 | |||
| 317 | CTS_per_grid = pd.merge(  | 
            ||
| 318 | CTS_per_grid,  | 
            ||
| 319 | CTS_demands_grid[["demand"]],  | 
            ||
| 320 | how="inner",  | 
            ||
| 321 | right_on=CTS_per_grid.index,  | 
            ||
| 322 | left_on=CTS_demands_grid.index,  | 
            ||
| 323 | )  | 
            ||
| 324 | |||
| 325 |             CTS_per_grid = CTS_per_grid.rename(columns={"key_0": "bus_id"}) | 
            ||
| 326 |             CTS_per_grid.set_index("bus_id", inplace=True) | 
            ||
| 327 | |||
| 328 | CTS_per_grid = CTS_per_grid[CTS_per_grid.columns[:-1]].multiply(  | 
            ||
| 329 | CTS_per_grid.demand, axis=0  | 
            ||
| 330 | )  | 
            ||
| 331 | |||
| 332 | CTS_per_grid.insert(0, "scenario", scenario)  | 
            ||
| 333 | |||
| 334 | CTS_grid = CTS_grid.append(CTS_per_grid)  | 
            ||
| 335 | CTS_grid = CTS_grid.sort_index()  | 
            ||
| 336 | |||
| 337 | CTS_per_zensus = 0  | 
            ||
| 338 | |||
| 339 | else:  | 
            ||
| 340 | CTS_per_district = 0  | 
            ||
| 341 | CTS_per_grid = 0  | 
            ||
| 342 | |||
| 343 | CTS_per_zensus = pd.merge(  | 
            ||
| 344 | CTS_per_zensus,  | 
            ||
| 345 | demand,  | 
            ||
| 346 | how="inner",  | 
            ||
| 347 | right_on=CTS_per_zensus.index,  | 
            ||
| 348 | left_on=demand.zensus_population_id,  | 
            ||
| 349 | )  | 
            ||
| 350 |             CTS_per_zensus = CTS_per_zensus.drop("key_0", axis=1) | 
            ||
| 351 |             CTS_per_zensus.set_index("zensus_population_id", inplace=True) | 
            ||
| 352 | |||
| 353 | CTS_per_zensus = CTS_per_zensus[  | 
            ||
| 354 | CTS_per_zensus.columns[:-1]  | 
            ||
| 355 | ].multiply(CTS_per_zensus.demand, axis=0)  | 
            ||
| 356 | CTS_per_zensus.insert(0, "scenario", scenario)  | 
            ||
| 357 | |||
| 358 | CTS_per_zensus.reset_index(inplace=True)  | 
            ||
| 359 | |||
| 360 | CTS_zensus = CTS_zensus.append(CTS_per_grid)  | 
            ||
| 361 |             CTS_zensus = CTS_zensus.set_index("bus_id") | 
            ||
| 362 | CTS_zensus = CTS_zensus.sort_index()  | 
            ||
| 363 | |||
| 364 | return CTS_district, CTS_grid, CTS_zensus  |