-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
76 lines (59 loc) · 3.18 KB
/
Copy pathmain.py
File metadata and controls
76 lines (59 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import pandas as pd
import json
from sqlalchemy import create_engine
from dotenv import load_dotenv
def extract_tabular_data(file_path: str):
"""Extract data from a tabular file_format, with pandas"""
if file_path.endswith(".csv"):
return pd.read_csv(file_path)
elif file_path.endswith(".parquet"):
return pd.read_parquet(file_path)
else:
print("Warning: Invalid file extension. Please try with .csv or .parquet!")
def extract_json_data(file_path):
"""Extract and flatten data from a JSON file."""
if file_path[-5:len(file_path)] == ".json":
df = pd.read_json(file_path)
df_energy_source = pd.json_normalize(df["energySource"])
df["energySource."+df_energy_source.columns] = df_energy_source
df.drop(columns="energySource", inplace=True)
return df
def transform_electricity_sales_data(raw_data: pd.DataFrame):
"""
Transform electricity sales to find the total amount of electricity sold
in the residential and transportation sectors.
To transform the electricity sales data, you'll need to do the following:
- Drop any records with NA values in the `price` column. Do this inplace.
- Only keep records with a `sectorName` of "residential" or "transportation".
- Create a `month` column using the first 4 characters of the values in `period`.
- Create a `year` column using the last 2 characters of the values in `period`.
- Return the transformed `DataFrame`, keeping only the columns `year`, `month`, `stateid`, `price` and `price-units`.
"""
raw_data = raw_data[(raw_data["sectorName"]=="residential") | (raw_data["sectorName"]=="transportation")]
period = raw_data["period"].str.split("-", n=1, expand=True)
raw_data = raw_data.copy()
raw_data.loc[:, "month"] = period[1]
raw_data.loc[:, "year"] = period[0]
raw_data.drop(columns=["period", "stateDescription", "sectorid", "sectorName"], inplace=True)
return raw_data
def transform_electricity_cap_data(raw_data: pd.DataFrame):
"""
"""
raw_data.columns = ["period", "stateId", "stateDescription", "energySourceid", "energySourceDescription", "energySourceCapability", "energySourceCapabilityUnits"]
raw_data["energySourceCapability"] = raw_data["energySourceCapability"].astype(float)
return raw_data
def load(dataframe: pd.DataFrame, table_name: str):
"""Load a DataFrame to MySQL Database"""
# Create MySQL engine
dbuser = os.getenv("USER")
dbpassword = os.getenv("PASSWORD")
dbname = "electricity_db"
engine = create_engine(f"mysql+mysqlconnector://{dbuser}:{dbpassword}@localhost/{dbname}")
dataframe.to_sql(name=table_name, con=engine, index=False, if_exists="replace")
raw_electricity_capability_df = extract_json_data("datasets/electricity_capability_nested.json")
raw_electricity_sales_df = extract_tabular_data("datasets/electricity_sales.csv")
cleaned_electricity_sales_df = transform_electricity_sales_data(raw_electricity_sales_df)
cleaned_electricity_cap_df = transform_electricity_cap_data(raw_electricity_capability_df)
load(cleaned_electricity_cap_df, "electricity_capability")
load(cleaned_electricity_sales_df, "electricity_sales")