-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
146 lines (103 loc) · 4.8 KB
/
utils.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Utils for other files
import pandas as pd
from datetime import datetime, timezone
from typing import Dict, Any
from datetime import datetime, timedelta
def historical_data_to_dataframe(historical_data):
"""
Convert historical data from JSON to a pandas DataFrame.
:param historical_data: List of dictionaries containing historical data
:return: pandas DataFrame with date as index and other fields as columns
"""
# Convert list of dictionaries to DataFrame
df = pd.DataFrame(historical_data)
# Convert Unix timestamp to datetime
df['date'] = pd.to_datetime(df['date'], unit='s', utc=True)
# Set 'date' as the index
df.set_index('date', inplace=True)
# Sort index in descending order (most recent date first)
df.sort_index(ascending=False, inplace=True)
# Convert UTC time to local time
df.index = df.index.tz_convert('America/Sao_Paulo')
return df
def financial_statement_to_dataframe(financial_data):
"""
Convert financial statement data from JSON to a pandas DataFrame.
:param financial_data: List of dictionaries containing financial statement data
:return: pandas DataFrame with end dates as column headers and financial statement lines as index
"""
# Convert list of dictionaries to DataFrame
df = pd.DataFrame(financial_data)
# Convert endDate to datetime and format it
df['endDate'] = pd.to_datetime(df['endDate'])
df['endDate'] = df['endDate'].dt.strftime('%Y-%m-%d')
# Set endDate as index temporarily
df.set_index('endDate', inplace=True)
# Transpose the DataFrame
df_transposed = df.transpose()
# Sort columns in descending order (most recent date first)
df_transposed = df_transposed.sort_index(axis=1, ascending=False)
# Format numbers (optional)
df_transposed = df_transposed.applymap(lambda x: f"{x:,.0f}" if isinstance(x, (int, float)) else x)
return df_transposed
def prime_rate_to_dataframe(prime_rate_data):
"""
Convert prime rate data to a pandas DataFrame with date as index and rate as value.
:param prime_rate_data: Dictionary containing prime rate data
:return: pandas DataFrame with date as index and prime rate as value
"""
# Extract the list of prime rate entries
prime_rates = prime_rate_data['prime-rate']
# Convert list of dictionaries to DataFrame
df = pd.DataFrame(prime_rates)
# Convert 'date' to datetime
df['date'] = pd.to_datetime(df['date'], format='%d/%m/%Y')
# Set 'date' as index
df.set_index('date', inplace=True)
# Sort index in descending order (most recent date first)
df.sort_index(ascending=False, inplace=True)
# Keep only the 'value' column and rename it
df = df[['value']].rename(columns={'value': 'prime_rate'})
# Convert prime_rate to float
df['prime_rate'] = df['prime_rate'].astype(float)
return df
def inflation_to_dataframe(inflation_rate_data):
"""
Convert prime rate data to a pandas DataFrame with date as index and rate as value.
:param prime_rate_data: Dictionary containing prime rate data
:return: pandas DataFrame with date as index and prime rate as value
"""
# Extract the list of prime rate entries
inflation = inflation_rate_data['inflation']
# Convert list of dictionaries to DataFrame
df = pd.DataFrame(inflation)
# Convert 'date' to datetime
df['date'] = pd.to_datetime(df['date'], format='%d/%m/%Y')
# Set 'date' as index
df.set_index('date', inplace=True)
# Sort index in descending order (most recent date first)
df.sort_index(ascending=False, inplace=True)
# Keep only the 'value' column and rename it
df = df[['value']].rename(columns={'value': 'prime_rate'})
# Convert prime_rate to float
df['prime_rate'] = df['prime_rate'].astype(float)
return df
def get_price(quote_data: Dict[str, Any]) -> pd.DataFrame:
"""
Extract historical price data from quote data and create a pandas DataFrame.
:param quote_data: Dictionary containing quote data returned by get_quote
:return: pandas DataFrame with historical price data
"""
# Extract historical price data
historical_data = quote_data['results'][0]['historicalDataPrice']
# Convert to DataFrame
df = pd.DataFrame(historical_data)
# Convert Unix timestamp to UTC-3 datetime
df['date'] = pd.to_datetime(df['date'], unit='s') - timedelta(hours=3)
# Set date as index
df.set_index('date', inplace=True)
# Sort index in descending order (most recent date first)
df.sort_index(ascending=False, inplace=True)
# Select and reorder columns
df = df[['open', 'high', 'low', 'close', 'volume']]
return df