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

30 lines
1.2 KiB
Python
Raw Permalink Normal View History

import pandas as pd
import numpy as np
import os
ROOT_PATH: str = os.path.join(os.path.dirname(__file__), '..', '..')
def compare_balance_sheets(ticker_list: list):
df_ret = pd.DataFrame(columns=['total_assets', 'current_assets', 'total_debt', 'current_debt'])
for i, ticker in enumerate(ticker_list):
assets_debt = pd.read_csv(ROOT_PATH + r'/Companies_Data/' + ticker + '_Data/' + ticker + '_balance_sheet_4Y+4Q.csv')
2023-05-29 15:50:09 +00:00
selected_data = assets_debt
2023-05-29 15:50:09 +00:00
def get_or_na(column: str):
row = selected_data.iloc[-2]
if column in row and not np.isnan(row[column]):
return row[column]
return 0
df_ret.loc[ticker, 'total_assets'] = get_or_na('TotalAssets')
df_ret.loc[ticker, 'current_assets'] = get_or_na('CurrentAssets')
df_ret.loc[ticker, 'total_debt'] = get_or_na('TotalDebt')
df_ret.loc[ticker, 'current_debt'] = get_or_na('CurrentDebt')
2023-05-29 08:51:16 +00:00
df_ret.loc[:, 'old_assets'] = df_ret['total_assets'].copy() - df_ret['current_assets'].copy()
df_ret.loc[:, 'old_debt'] = df_ret['total_debt'].copy() - df_ret['current_debt'].copy()
df_ret.index.name = 'ticker'
return df_ret.reset_index(drop=False).replace({ np.nan: None }).to_dict('records')