This repository has been archived on 2023-06-18. You can view files and clone it, but cannot push or open issues or pull requests.
va-project/backend/api/eps.py

59 lines
2.2 KiB
Python
Raw Permalink Normal View History

2023-05-29 11:38:02 +00:00
import os
import pandas as pd
import numpy as np
from scraper.top100_extractor import programming_crime_list
from typing import Optional
2023-05-29 15:50:09 +00:00
from dateutil.parser import isoparse
2023-05-29 11:38:02 +00:00
DF_EPS_PATH: str = os.path.join(os.path.dirname(__file__), '..', '..', 'Elaborated_Data', 'eps_quarterly_bar_chart.csv')
2023-05-29 15:50:09 +00:00
def ticker_quarter_list(df_ticker: pd.Series) -> list[int]:
dfu = df_ticker.unique()
months = set([isoparse(date).month for date in dfu])
return list(sorted(months))
2023-05-29 11:38:02 +00:00
def get_eps(tickers: list[str]) -> list[dict]:
df = pd.read_csv(DF_EPS_PATH)
ticker_series = pd.Series(tickers)
2023-05-29 15:50:09 +00:00
qmap = dict([ \
[ticker, ticker_quarter_list(df.loc[df.symbol == ticker, 'quarter'])] \
for ticker in tickers \
])
2023-05-29 11:38:02 +00:00
df = df.loc[df.symbol.isin(ticker_series), :] \
.rename(columns={"epsActual": "quarterlyEps", "symbol": "ticker"}) \
.reset_index(drop=True)
2023-05-29 15:50:09 +00:00
def get_quart(x):
date = isoparse(x.quarter)
quarter = qmap[x.ticker].index(date.month) + 1
return f"{date.year}-Q{quarter}"
df['quarter'] = df.apply(get_quart, axis=1)
2023-05-29 11:38:02 +00:00
df = df.pivot(index='quarter', columns='ticker', values='quarterlyEps').reset_index(drop=False)
2023-05-29 15:50:09 +00:00
return df.replace({ np.nan: None }).to_dict('records')
def get_eps_old(tickers: list[str]) -> list[dict]:
eps_df = pd.read_csv('Elaborated_Data/eps_quarterly_bar_chart.csv', index_col=[0])
s_tickers = pd.Series(tickers)
eps_df = eps_df.loc[eps_df['symbol'].isin(s_tickers), :]
dates = eps_df['quarter'].to_list()
for i, date in enumerate(dates):
eps_df[date] = eps_df['epsActual'].copy().apply(lambda x: int(eval(str(x))[i]))
eps_df = pd.melt(eps_df, id_vars=['symbol'], value_vars=['quarter'], var_name='quarter').reset_index(drop=True)
print(eps_df)
eps_df = eps_df.pivot(index='value', columns='symbol', values='value').reset_index(drop=False)
exit(1)
for ticker in tickers:
filepath = f'Companies_Data/{ticker}_Data/{ticker}earnings.csv'
eps_quarterly = pd.read_csv(filepath)
ticker_df = eps_quarterly.loc[eps_quarterly['symbol'] == ticker]
ticker_df['ticker'] = ticker
combined_df = combined_df._append(ticker_df)