No topics yet. Start the conversation.

Description

The below description is supplied in free-text by the user

Index error issues

A sample chart show how to work around duplicate index errors

import pandas as pd
import numpy as np
import random
from novem import Plot
from novem.table import Selector as S

# Define a function to generate random text related to financial services
def generate_text(field):
    if field == "Feature":
        choices = ['Liquidity Risk', 'Credit Risk', 'Operational Risk', 'Market Risk', 'Interest Rate Risk']
    elif field == "Breach":
        choices = ['High Exposure', 'Low Reserve', 'Non-compliance', 'Unusual Activity', 'Late Reporting']
    else: # field == "Reason"
        choices = ['Data Error', 'Process Failure', 'Regulatory Change', 'Market Volatility', 'Operational Issue']

    return random.choice(choices)

# Create the data
data = {
    'Value': np.random.rand(5),
    'Feature': [generate_text("Feature") for _ in range(5)],
    'Breach': [generate_text("Breach") for _ in range(5)],
    'Reason': [generate_text("Reason") for _ in range(5)]
}

# Create the DataFrame with duplicate index "ABCD"
df = pd.DataFrame(data, index=['ABCD']*5)

# reset index (so we get unique index values)
df = df.reset_index()

# Create a test plot
pnl = Plot('a_test', type='table') 

# share it with the public
pnl.shared = 'public'
pnl.name = 'Duplicate index issue'

# add our data
df.pipe(pnl)

pnl.cell.align = "0 1: -"
pnl.cell.align += "1: 1: -"

# Use column offset value of -1 to account for the fact that we will
# be updating the index later
pnl.cell.format = S(df.iloc[:,0],  ",.1f", r=df, co=-1)
pnl.cell.format += S(df.iloc[:,1], ",.1%", r=df, co=-1)
pnl.cell.format += S(df.iloc[:,2], ",.1f", r=df, co=-1)

# add index back to dataframe and send it to novem
df.set_index('index').pipe(pnl)

# set type to mailtable
pnl.type = 'mtable'

print(pnl.url)

# add our code as an example
with open(__file__,'r') as f:
  ctnt = f.read().replace('```','```') # turtles

  desc = f"""
# Index error issues
A sample chart show how to work around duplicate index errors

```
{ctnt}
```
"""

  pnl.description = desc