idp_pool_generator()   F
last analyzed

Complexity

Conditions 13

Size

Total Lines 213
Code Lines 126

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 126
dl 0
loc 213
rs 2.94
c 0
b 0
f 0
cc 13
nop 0

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

Complex classes like data.datasets.heat_demand_timeseries.idp_pool.idp_pool_generator() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
from datetime import datetime
2
import os
3
4
from sqlalchemy import ARRAY, Column, Float, Integer, String
5
from sqlalchemy.ext.declarative import declarative_base
6
7
import numpy as np
8
import pandas as pd
9
10
from egon.data import db
11
12
13
from math import ceil
14
15
import egon
16
17
18
Base = declarative_base()
19
20
21
class EgonHeatTimeseries(Base):
22
    __tablename__ = "egon_heat_timeseries_selected_profiles"
23
    __table_args__ = {"schema": "demand"}
24
    zensus_population_id = Column(Integer, primary_key=True)
25
    building_id = Column(Integer, primary_key=True)
26
    selected_idp_profiles = Column(ARRAY(Integer))
27
28
29 View Code Duplication
def temperature_classes():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
30
    return {
31
        -20: 1,
32
        -19: 1,
33
        -18: 1,
34
        -17: 1,
35
        -16: 1,
36
        -15: 1,
37
        -14: 2,
38
        -13: 2,
39
        -12: 2,
40
        -11: 2,
41
        -10: 2,
42
        -9: 3,
43
        -8: 3,
44
        -7: 3,
45
        -6: 3,
46
        -5: 3,
47
        -4: 4,
48
        -3: 4,
49
        -2: 4,
50
        -1: 4,
51
        0: 4,
52
        1: 5,
53
        2: 5,
54
        3: 5,
55
        4: 5,
56
        5: 5,
57
        6: 6,
58
        7: 6,
59
        8: 6,
60
        9: 6,
61
        10: 6,
62
        11: 7,
63
        12: 7,
64
        13: 7,
65
        14: 7,
66
        15: 7,
67
        16: 8,
68
        17: 8,
69
        18: 8,
70
        19: 8,
71
        20: 8,
72
        21: 9,
73
        22: 9,
74
        23: 9,
75
        24: 9,
76
        25: 9,
77
        26: 10,
78
        27: 10,
79
        28: 10,
80
        29: 10,
81
        30: 10,
82
        31: 10,
83
        32: 10,
84
        33: 10,
85
        34: 10,
86
        35: 10,
87
        36: 10,
88
        37: 10,
89
        38: 10,
90
        39: 10,
91
        40: 10,
92
    }
93
94
95
def idp_pool_generator():
96
    """
97
    Create List of Dataframes for each temperature class for each household stock
98
99
    Returns
100
    -------
101
    list
102
        List of dataframes with each element representing a dataframe
103
        for every combination of household stock and temperature class
104
105
    """
106
    path = os.path.join(
107
        os.getcwd(),
108
        "data_bundle_egon_data",
109
        "household_heat_demand_profiles",
110
        "household_heat_demand_profiles.hdf5",
111
    )
112
    index = pd.date_range(datetime(2011, 1, 1, 0), periods=8760, freq="H")
113
114
    sfh = pd.read_hdf(path, key="SFH")
115
    mfh = pd.read_hdf(path, key="MFH")
116
    temp = pd.read_hdf(path, key="temperature")
117
118
    globals()["luebeck_sfh"] = sfh[sfh.filter(like="Luebeck").columns]
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable globals does not seem to be defined.
Loading history...
119
    globals()["luebeck_mfh"] = mfh[mfh.filter(like="Luebeck").columns]
120
121
    globals()["kassel_sfh"] = sfh[sfh.filter(like="Kassel").columns]
122
    globals()["kassel_mfh"] = mfh[mfh.filter(like="Kassel").columns]
123
124
    globals()["wuerzburg_sfh"] = sfh[sfh.filter(like="Wuerzburg").columns]
125
    globals()["wuerzburg_mfh"] = mfh[mfh.filter(like="Wuerzburg").columns]
126
127
    globals()["chemnitz_sfh"] = sfh[sfh.filter(like="Chemnitz").columns]
128
    globals()["chemnitz_mfh"] = mfh[mfh.filter(like="Chemnitz").columns]
129
130
    temp_daily = pd.DataFrame()
131
    for column in temp.columns:
132
        temp_current = (
133
            temp[column]
134
            .resample("D")
135
            .mean()
136
            .reindex(temp.index)
137
            .fillna(method="ffill")
138
            .fillna(method="bfill")
139
        )
140
        temp_current_geom = temp_current
141
        temp_daily = pd.concat([temp_daily, temp_current_geom], axis=1)
142
143
    def round_temperature(station):
144
        """
145
        Description: Create dataframe to assign temperature class to each day of TRY climate zones
146
147
        Parameters
148
        ----------
149
        station : str
150
            Name of the location
151
152
        Returns
153
        -------
154
        temp_class : pandas.DataFrame
155
            Each day assignd to their respective temperature class
156
157
        """
158
        intervals = temperature_classes()
159
        temperature_rounded = []
160
        for i in temp_daily.loc[:, station]:
161
            temperature_rounded.append(ceil(i))
162
        temperature_interval = []
163
        for i in temperature_rounded:
164
            temperature_interval.append(intervals[i])
165
        temp_class_dic = {f"Class_{station}": temperature_interval}
166
        temp_class = pd.DataFrame.from_dict(temp_class_dic)
167
        return temp_class
168
169
    temp_class_luebeck = round_temperature("Luebeck")
170
    temp_class_kassel = round_temperature("Kassel")
171
    temp_class_wuerzburg = round_temperature("Wuerzburg")
172
    temp_class_chemnitz = round_temperature("Chemnitz")
173
    temp_class = pd.concat(
174
        [
175
            temp_class_luebeck,
176
            temp_class_kassel,
177
            temp_class_wuerzburg,
178
            temp_class_chemnitz,
179
        ],
180
        axis=1,
181
    )
182
    temp_class.set_index(index, inplace=True)
183
184
    def unique_classes(station):
185
        """
186
187
        Returns
188
        -------
189
        classes : list
190
            Collection of temperature classes for each location
191
192
        """
193
        classes = []
194
        for x in temp_class[f"Class_{station}"]:
195
            if x not in classes:
196
                classes.append(x)
197
        classes.sort()
198
        return classes
199
200
    globals()["luebeck_classes"] = unique_classes("Luebeck")
201
    globals()["kassel_classes"] = unique_classes("Kassel")
202
    globals()["wuerzburg_classes"] = unique_classes("Wuerzburg")
203
    globals()["chemnitz_classes"] = unique_classes("Chemnitz")
204
205
    stock = ["MFH", "SFH"]
206
    class_list = [2, 3, 4, 5, 6, 7, 8, 9, 10]
207
    for s in stock:
208
        for m in class_list:
209
            globals()[f"idp_collection_class_{m}_{s}"] = pd.DataFrame(
210
                index=range(24)
211
            )
212
213
    def splitter(station, household_stock):
214
        """
215
216
217
        Parameters
218
        ----------
219
        station : str
220
            Name of the location
221
        household_stock : str
222
            SFH or MFH
223
224
        Returns
225
        -------
226
        None.
227
228
        """
229
        this_classes = globals()["{}_classes".format(station.lower())]
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable globals does not seem to be defined.
Loading history...
230
        for classes in this_classes:
231
            this_itteration = globals()[
232
                "{}_{}".format(station.lower(), household_stock.lower())
233
            ].loc[temp_class["Class_{}".format(station)] == classes, :]
234
            days = list(range(int(len(this_itteration) / 24)))
235
            for day in days:
236
                this_day = this_itteration[day * 24 : (day + 1) * 24]
237
                this_day = this_day.reset_index(drop=True)
238
                globals()[
239
                    f"idp_collection_class_{classes}_{household_stock}"
240
                ] = pd.concat(
241
                    [
242
                        globals()[
243
                            f"idp_collection_class_{classes}_{household_stock}"
244
                        ],
245
                        this_day,
246
                    ],
247
                    axis=1,
248
                    ignore_index=True,
249
                )
250
251
    splitter("Luebeck", "SFH")
252
    splitter("Kassel", "SFH")
253
    splitter("Wuerzburg", "SFH")
254
    splitter("Chemnitz", "SFH")
255
    splitter("Luebeck", "MFH")
256
    splitter("Kassel", "MFH")
257
    splitter("Chemnitz", "MFH")
258
259
    def pool_normalize(x):
260
        """
261
262
263
        Parameters
264
        ----------
265
        x : pandas.Series
266
            24-hour profiles of IDP pool
267
268
        Returns
269
        -------
270
        TYPE : pandas.Series
271
            Normalized to their daily total
272
273
        """
274
        if x.sum() != 0:
275
            c = x.sum()
276
            return x / c
277
        else:
278
            return x
279
280
    stock = ["MFH", "SFH"]
281
    class_list = [2, 3, 4, 5, 6, 7, 8, 9, 10]
282
    for s in stock:
283
        for m in class_list:
284
            df_name = globals()[f"idp_collection_class_{m}_{s}"]
285
            globals()[f"idp_collection_class_{m}_{s}_norm"] = df_name.apply(
286
                pool_normalize
287
            )
288
289
    return [
290
        idp_collection_class_2_SFH_norm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable idp_collection_class_2_SFH_norm does not seem to be defined.
Loading history...
291
        idp_collection_class_3_SFH_norm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable idp_collection_class_3_SFH_norm does not seem to be defined.
Loading history...
292
        idp_collection_class_4_SFH_norm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable idp_collection_class_4_SFH_norm does not seem to be defined.
Loading history...
293
        idp_collection_class_5_SFH_norm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable idp_collection_class_5_SFH_norm does not seem to be defined.
Loading history...
294
        idp_collection_class_6_SFH_norm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable idp_collection_class_6_SFH_norm does not seem to be defined.
Loading history...
295
        idp_collection_class_7_SFH_norm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable idp_collection_class_7_SFH_norm does not seem to be defined.
Loading history...
296
        idp_collection_class_8_SFH_norm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable idp_collection_class_8_SFH_norm does not seem to be defined.
Loading history...
297
        idp_collection_class_9_SFH_norm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable idp_collection_class_9_SFH_norm does not seem to be defined.
Loading history...
298
        idp_collection_class_10_SFH_norm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable idp_collection_class_10_SFH_norm does not seem to be defined.
Loading history...
299
        idp_collection_class_2_MFH_norm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable idp_collection_class_2_MFH_norm does not seem to be defined.
Loading history...
300
        idp_collection_class_3_MFH_norm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable idp_collection_class_3_MFH_norm does not seem to be defined.
Loading history...
301
        idp_collection_class_4_MFH_norm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable idp_collection_class_4_MFH_norm does not seem to be defined.
Loading history...
302
        idp_collection_class_5_MFH_norm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable idp_collection_class_5_MFH_norm does not seem to be defined.
Loading history...
303
        idp_collection_class_6_MFH_norm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable idp_collection_class_6_MFH_norm does not seem to be defined.
Loading history...
304
        idp_collection_class_7_MFH_norm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable idp_collection_class_7_MFH_norm does not seem to be defined.
Loading history...
305
        idp_collection_class_8_MFH_norm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable idp_collection_class_8_MFH_norm does not seem to be defined.
Loading history...
306
        idp_collection_class_9_MFH_norm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable idp_collection_class_9_MFH_norm does not seem to be defined.
Loading history...
307
        idp_collection_class_10_MFH_norm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable idp_collection_class_10_MFH_norm does not seem to be defined.
Loading history...
308
    ]
309
310
311
def create():
312
    """
313
    Description: Create dataframe with all temprature classes, 24hr. profiles and household stock
314
315
    Returns
316
    -------
317
    idp_df : pandas.DataFrame
318
        All IDP pool as classified as per household stock and temperature class
319
320
    """
321
    idp_list = idp_pool_generator()
322
    stock = ["MFH", "SFH"]
323
    class_list = [2, 3, 4, 5, 6, 7, 8, 9, 10]
324
    idp_df = pd.DataFrame(columns=["idp", "house", "temperature_class"])
325
    for s in stock:
326
        for m in class_list:
327
            if s == "SFH":
328
                i = class_list.index(m)
329
            if s == "MFH":
330
                i = class_list.index(m) + 9
331
            current_pool = idp_list[i]
0 ignored issues
show
introduced by
The variable i does not seem to be defined for all execution paths.
Loading history...
332
            idp_df = pd.concat(
333
                [
334
                    idp_df,
335
                    pd.DataFrame(
336
                        data={
337
                            "idp": current_pool.transpose().values.tolist(),
338
                            "house": s,
339
                            "temperature_class": m,
340
                        }
341
                    ),
342
                ],
343
                ignore_index=True,
344
            )
345
    idp_df = idp_df.reset_index(drop=True)
346
347
    idp_df.to_sql(
348
        "egon_heat_idp_pool",
349
        con=db.engine(),
350
        schema="demand",
351
        if_exists="replace",
352
        index=True,
353
        dtype={
354
            "index": Integer(),
355
            "idp": ARRAY(Float()),
356
            "house": String(),
357
            "temperature_class": Integer(),
358
        },
359
    )
360
361
    idp_df["idp"] = idp_df.idp.apply(lambda x: np.array(x))
362
363
    idp_df.idp = idp_df.idp.apply(lambda x: x.astype(np.float32))
364
365
    return idp_df
366
367
368
def annual_demand_generator(scenario):
369
    """
370
    Description: Create dataframe with annual demand and household count for each zensus cell
371
372
    Returns
373
    -------
374
    demand_count: pandas.DataFrame
375
        Annual demand of all zensus cell with MFH and SFH count and
376
        respective associated Station
377
378
    """
379
380
    demand_zone = db.select_dataframe(
381
        f"""
382
        SELECT a.demand, a.zensus_population_id, a.scenario, c.climate_zone
383
        FROM demand.egon_peta_heat a
384
        JOIN boundaries.egon_map_zensus_climate_zones c
385
        ON a.zensus_population_id = c.zensus_population_id
386
        WHERE a.sector = 'residential'
387
        AND a.scenario = '{scenario}'
388
        """,
389
        index_col="zensus_population_id",
390
    )
391
392
    house_count_MFH = db.select_dataframe(
393
        """
394
395
        SELECT cell_id as zensus_population_id, COUNT(*) as number FROM
396
        (
397
        SELECT cell_id, COUNT(*), building_id
398
        FROM demand.egon_household_electricity_profile_of_buildings
399
        GROUP BY (cell_id, building_id)
400
        ) a
401
402
        WHERE a.count >1
403
        GROUP BY cell_id
404
        """,
405
        index_col="zensus_population_id",
406
    )
407
408
    house_count_SFH = db.select_dataframe(
409
        """
410
411
        SELECT cell_id as zensus_population_id, COUNT(*) as number FROM
412
        (
413
        SELECT cell_id, COUNT(*), building_id
414
        FROM demand.egon_household_electricity_profile_of_buildings
415
        GROUP BY (cell_id, building_id)
416
        ) a
417
        WHERE a.count = 1
418
        GROUP BY cell_id
419
        """,
420
        index_col="zensus_population_id",
421
    )
422
423
    demand_zone["SFH"] = house_count_SFH.number
424
    demand_zone["MFH"] = house_count_MFH.number
425
426
    demand_zone["SFH"].fillna(0, inplace=True)
427
    demand_zone["MFH"].fillna(0, inplace=True)
428
429
    return demand_zone
430
431
432
def select():
433
    """
434
435
    Random assignment of intray-day profiles to each day based on their temeprature class
436
    and household stock count
437
438
    Returns
439
    -------
440
    None.
441
442
    """
443
    start_profile_selector = datetime.now()
444
445
    # Drop old table and re-create it
446
    engine = db.engine()
447
    EgonHeatTimeseries.__table__.drop(bind=engine, checkfirst=True)
448
    EgonHeatTimeseries.__table__.create(bind=engine, checkfirst=True)
449
450
    # Select all intra-day-profiles
451
    idp_df = db.select_dataframe(
452
        """
453
        SELECT index, house, temperature_class
454
        FROM demand.egon_heat_idp_pool
455
        """,
456
        index_col="index",
457
    )
458
459
    # Select daily heat demand shares per climate zone from table
460
    temperature_classes = db.select_dataframe(
461
        """
462
        SELECT climate_zone, day_of_year, temperature_class
463
        FROM demand.egon_daily_heat_demand_per_climate_zone
464
        """
465
    )
466
467
    # Calculate annual heat demand per census cell
468
    annual_demand = annual_demand_generator(
469
        scenario=egon.data.config.settings()["egon-data"]["--scenarios"][0]
470
    )
471
472
    # Count number of SFH and MFH per climate zone
473
    houses_per_climate_zone = (
474
        annual_demand.groupby("climate_zone")[["SFH", "MFH"]].sum().astype(int)
475
    )
476
477
    # Set random seed to make code reproducable
478
    np.random.seed(
479
        seed=egon.data.config.settings()["egon-data"]["--random-seed"]
480
    )
481
482
    for station in houses_per_climate_zone.index:
483
        result_SFH = pd.DataFrame(columns=range(1, 366))
484
        result_MFH = pd.DataFrame(columns=range(1, 366))
485
486
        # Randomly select individual daily demand profile for selected climate zone
487
        for day in range(1, 366):
488
            t_class = temperature_classes.loc[
489
                (temperature_classes.climate_zone == station)
490
                & (temperature_classes.day_of_year == day),
491
                "temperature_class",
492
            ].values[0]
493
494
            result_SFH[day] = np.random.choice(
495
                np.array(
496
                    idp_df[
497
                        (idp_df.temperature_class == t_class)
498
                        & (idp_df.house == "SFH")
499
                    ].index.values
500
                ),
501
                houses_per_climate_zone.loc[station, "SFH"],
502
            )
503
504
            result_MFH[day] = np.random.choice(
505
                np.array(
506
                    idp_df[
507
                        (idp_df.temperature_class == t_class)
508
                        & (idp_df.house == "MFH")
509
                    ].index.values
510
                ),
511
                houses_per_climate_zone.loc[station, "MFH"],
512
            )
513
514
        result_SFH["zensus_population_id"] = (
515
            annual_demand[annual_demand.climate_zone == station]
516
            .loc[
517
                annual_demand[
518
                    annual_demand.climate_zone == station
519
                ].index.repeat(
520
                    annual_demand[
521
                        annual_demand.climate_zone == station
522
                    ].SFH.astype(int)
523
                )
524
            ]
525
            .index.values
526
        )
527
528
        result_SFH["building_id"] = (
529
            db.select_dataframe(
530
                """
531
532
            SELECT cell_id as zensus_population_id, building_id FROM
533
            (
534
            SELECT cell_id, COUNT(*), building_id
535
            FROM demand.egon_household_electricity_profile_of_buildings
536
            GROUP BY (cell_id, building_id)
537
            ) a
538
            WHERE a.count = 1
539
            """,
540
                index_col="zensus_population_id",
541
            )
542
            .loc[result_SFH["zensus_population_id"].unique(), "building_id"]
543
            .values
544
        )
545
546
        result_MFH["zensus_population_id"] = (
547
            annual_demand[annual_demand.climate_zone == station]
548
            .loc[
549
                annual_demand[
550
                    annual_demand.climate_zone == station
551
                ].index.repeat(
552
                    annual_demand[
553
                        annual_demand.climate_zone == station
554
                    ].MFH.astype(int)
555
                )
556
            ]
557
            .index.values
558
        )
559
560
        result_MFH["building_id"] = (
561
            db.select_dataframe(
562
                """
563
564
            SELECT cell_id as zensus_population_id, building_id FROM
565
            (
566
            SELECT cell_id, COUNT(*), building_id
567
            FROM demand.egon_household_electricity_profile_of_buildings
568
            GROUP BY (cell_id, building_id)
569
            ) a
570
            WHERE a.count > 1
571
            """,
572
                index_col="zensus_population_id",
573
            )
574
            .loc[result_MFH["zensus_population_id"].unique(), "building_id"]
575
            .values
576
        )
577
578
        df_sfh = pd.DataFrame(
579
            data={
580
                "selected_idp_profiles": result_SFH[
581
                    range(1, 366)
582
                ].values.tolist(),
583
                "zensus_population_id": (
584
                    annual_demand[annual_demand.climate_zone == station]
585
                    .loc[
586
                        annual_demand[
587
                            annual_demand.climate_zone == station
588
                        ].index.repeat(
589
                            annual_demand[
590
                                annual_demand.climate_zone == station
591
                            ].SFH.astype(int)
592
                        )
593
                    ]
594
                    .index.values
595
                ),
596
                "building_id": (
597
                    db.select_dataframe(
598
                        """
599
600
                SELECT cell_id as zensus_population_id, building_id FROM
601
                (
602
                SELECT cell_id, COUNT(*), building_id
603
                FROM demand.egon_household_electricity_profile_of_buildings
604
                GROUP BY (cell_id, building_id)
605
                ) a
606
                WHERE a.count = 1
607
                """,
608
                        index_col="zensus_population_id",
609
                    )
610
                    .loc[
611
                        result_SFH["zensus_population_id"].unique(),
612
                        "building_id",
613
                    ]
614
                    .values
615
                ),
616
            }
617
        )
618
        start_sfh = datetime.now()
619
        df_sfh.set_index(["zensus_population_id", "building_id"]).to_sql(
620
            EgonHeatTimeseries.__table__.name,
621
            schema=EgonHeatTimeseries.__table__.schema,
622
            con=db.engine(),
623
            if_exists="append",
624
            chunksize=5000,
625
            method="multi",
626
        )
627
        print(f"SFH insertation for zone {station}:")
628
        print(datetime.now() - start_sfh)
629
630
        df_mfh = pd.DataFrame(
631
            data={
632
                "selected_idp_profiles": result_MFH[
633
                    range(1, 366)
634
                ].values.tolist(),
635
                "zensus_population_id": (
636
                    annual_demand[annual_demand.climate_zone == station]
637
                    .loc[
638
                        annual_demand[
639
                            annual_demand.climate_zone == station
640
                        ].index.repeat(
641
                            annual_demand[
642
                                annual_demand.climate_zone == station
643
                            ].MFH.astype(int)
644
                        )
645
                    ]
646
                    .index.values
647
                ),
648
                "building_id": (
649
                    db.select_dataframe(
650
                        """
651
652
                SELECT cell_id as zensus_population_id, building_id FROM
653
                (
654
                SELECT cell_id, COUNT(*), building_id
655
                FROM demand.egon_household_electricity_profile_of_buildings
656
                GROUP BY (cell_id, building_id)
657
                ) a
658
                WHERE a.count > 1
659
                """,
660
                        index_col="zensus_population_id",
661
                    )
662
                    .loc[
663
                        result_MFH["zensus_population_id"].unique(),
664
                        "building_id",
665
                    ]
666
                    .values
667
                ),
668
            }
669
        )
670
671
        start_mfh = datetime.now()
672
        df_mfh.set_index(["zensus_population_id", "building_id"]).to_sql(
673
            EgonHeatTimeseries.__table__.name,
674
            schema=EgonHeatTimeseries.__table__.schema,
675
            con=db.engine(),
676
            if_exists="append",
677
            chunksize=5000,
678
            method="multi",
679
        )
680
        print(f"MFH insertation for zone {station}:")
681
        print(datetime.now() - start_mfh)
682
683
    print("Time for overall profile selection:")
684
    print(datetime.now() - start_profile_selector)
685