-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
82 lines (62 loc) · 2.09 KB
/
main.py
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
77
78
79
80
81
82
import os
import glob
import prefect
import numpy as np
import pandas as pd
from s3fs.core import S3FileSystem
from prefect import Flow, task
logger = prefect.context.get("logger")
@task
def extract(path: str):
"""Loads all csv found in path arg and put it in a dataframe
Assumes same columns for every csv
Returns:
[pd.Dataframe]: appended coins csvs
"""
files = glob.glob(path)
df = pd.DataFrame()
for file in files:
df_csv = pd.read_csv(file)
df = df.append(df_csv)
logger.info(f"Shape of the dataframe is: {df.shape}")
return df.reset_index(drop=True)
@task
def transform(df: pd.DataFrame):
"""Create new dataframe with historical daily best performing coin
Args:
df (pd.DataFrame): all coins historical data
Returns:
[pd.DataFrame]: dataframe with daily best performer
"""
df["Date"] = pd.to_datetime(df.loc[:, "Date"], utc=True)
df["closing_positive"] = np.where(df["Close"] > df["Open"], True, False)
df["daily_diff"] = df["Close"] - df["Open"]
df["daily_diff_percentage"] = ((df["Close"] - df["Open"]) / df["Open"]) * 100
df_daily_best = (
df.sort_values(["Date", "daily_diff_percentage"], ascending=[False, False])
.drop_duplicates(["Date"])
.reset_index(drop=True)
)
return df_daily_best
@task
def load_data(df: pd.DataFrame, file_name: str):
"""Upload data to s3
Args:
df (pd.DataFrame): Undergone the transformation
file_name (str): Outputfile name
"""
s3 = S3FileSystem(
anon=False,
client_kwargs={
"endpoint_url": os.getenv("AWS_S3_ENDPOINT_URL"),
"aws_access_key_id": os.getenv("AWS_ACCESS_KEY_ID"),
"aws_secret_access_key": os.getenv("AWS_SECRET_ACCESS_KEY"),
},
)
bytes_to_write = df.to_csv(None).encode()
with s3.open(f"s3://{os.getenv('AWS_S3_BUCKET')}/{file_name}.csv", "wb") as f:
f.write(bytes_to_write)
with Flow("etl-example") as flow:
df = extract("data/*.csv")
df_daily_best = transform(df)
load_data(df_daily_best, "best_daily")