58 lines
2.2 KiB
Python
58 lines
2.2 KiB
Python
import os
|
|
import pandas as pd
|
|
import numpy as np
|
|
from scraper.top100_extractor import programming_crime_list
|
|
from typing import Optional
|
|
from dateutil.parser import isoparse
|
|
|
|
DF_EPS_PATH: str = os.path.join(os.path.dirname(__file__), '..', '..', 'Elaborated_Data', 'eps_quarterly_bar_chart.csv')
|
|
|
|
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))
|
|
|
|
|
|
def get_eps(tickers: list[str]) -> list[dict]:
|
|
df = pd.read_csv(DF_EPS_PATH)
|
|
ticker_series = pd.Series(tickers)
|
|
qmap = dict([ \
|
|
[ticker, ticker_quarter_list(df.loc[df.symbol == ticker, 'quarter'])] \
|
|
for ticker in tickers \
|
|
])
|
|
|
|
df = df.loc[df.symbol.isin(ticker_series), :] \
|
|
.rename(columns={"epsActual": "quarterlyEps", "symbol": "ticker"}) \
|
|
.reset_index(drop=True)
|
|
|
|
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)
|
|
df = df.pivot(index='quarter', columns='ticker', values='quarterlyEps').reset_index(drop=False)
|
|
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)
|