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/past_performance.py

34 lines
1.1 KiB
Python
Raw Permalink Normal View History

2023-05-29 13:56:15 +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 backend.api.eps import ticker_quarter_list
from dateutil.parser import isoparse
2023-05-29 13:56:15 +00:00
DF_EPS_PATH: str = os.path.join(os.path.dirname(__file__), '..', '..', 'Elaborated_Data', 'eps_comparison.csv')
def get_eps_comp(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
2023-05-29 13:56:15 +00:00
df = df.loc[df['Ticker'].isin(ticker_series), :] \
.rename(columns={"epsDifferential": "quarterlyDifferential", "Ticker": "ticker"}) \
.reset_index(drop=True)
2023-05-29 15:50:09 +00:00
qmap = dict([ \
[ticker, ticker_quarter_list(df.loc[df.ticker == ticker, 'quarter'])] \
for ticker in tickers \
])
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 13:56:15 +00:00
df = df.pivot(index='quarter', columns='ticker', values='quarterlyDifferential').reset_index(drop=False)
return df.replace({ np.nan: None }).to_dict('records')