def __init__(self, historical=None, balance_sheet=None, income_statement=None, cash_flow=None, financial_report_preparation_lag=np.timedelta64(30, 'D') ): """ Returns an object that is capable of calculating various metrics of the stock. historical, balance_sheet, income_statement, cash_flow: respective DataFrame objects, default: None financial_report_preparation_lag (np.timedelta64 object): how long does it take to release the financial report after the end of fiscal year? default: 30 days. """ self.historical = historical self.balance_sheet = balance_sheet self.income_statement = income_statement self.cash_flow = cash_flow self.financial_report_preparation_lag = financial_report_preparation_lag if historical is not None: # historical data with missing dates filled in, with values from future self.historical_fillforward = historical.resample('1D', fill_method="ffill") # historical data with missing dates filled in, with values from past self.historical_fillbackward = historical.resample('1D', fill_method="bfill") # financial reports with the lag in information release factored in if balance_sheet is not None: self.balance_sheet_lagged = translate_index(balance_sheet, self.financial_report_preparation_lag) if income_statement is not None: self.income_statement_lagged = translate_index(income_statement, self.financial_report_preparation_lag) if cash_flow is not None: self.cash_flow_lagged = translate_index(cash_flow, self.financial_report_preparation_lag)
def test_translate_index(self): result = calculator.translate_index(pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}), 3) self.assertFrameEqual(result, pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, index=[3, 4, 5]))