I have a dataframe where I want the value to be NaN if the index and column name match. I have tried using np.fill_diagonal() since that is essentially what I want, but it changes the format of the values. I want to keep the scientific notation format of the values if possible. What is the best way to do so?
granger_df = None
data_diff = df.diff()
data_diff.dropna(inplace=True)
var_model = VAR(data_diff)
res = var_model.select_order(maxlags=p)
results = var_model.fit(maxlags=p, ic='aic')
results_df = pd.DataFrame(results, index = df.columns, columns = df.columns)
my_list = []
for col1 in results_df:
for col2 in results_df:
element = results.test_causality(col1, col2, kind='f')
my_list.append(element.pvalue)
my_array = np.array(my_list)
my_array = my_array.reshape(5,5)
granger_df = pd.DataFrame(my_array, index=df.columns, columns=df.columns)
The output I currently have:
The output when I use
np.fill_diagonal(granger_df.values, np.nan)
It achieves my goal, but changes the values. I need them to stay the same: