Manipulating Instruments to Beautify Expiry Lists

instrumentList = pd.read_csv("https://api.kite.trade/instruments")    
instrumentList.to_csv(instrumentcsv)

This is the basic.

import json
import pandas as pd
import os

def create_expiry_list(df):
    expiry_list = {"fut": {}, "options": {}}
    
    # Filtering for Futures
    df_fut = df[df['instrument_type'] == 'FUT']
    unique_names_fut = df_fut['name'].unique()
    for name in unique_names_fut:
        expiry_dates = df_fut[df_fut['name'] == name]['expiry'].dropna().unique()
        expiry_dates = pd.to_datetime(expiry_dates)
        expiry_list["fut"][name] = sorted(expiry_dates)
    
    # Filtering for Options (CE and PE)
    df_options = df[df['instrument_type'].isin(['CE', 'PE'])]
    unique_names_options = df_options['name'].unique()
    for name in unique_names_options:
        expiry_dates = df_options[df_options['name'] == name]['expiry'].dropna().unique()
        expiry_dates = pd.to_datetime(expiry_dates)
        expiry_list["options"][name] = sorted(expiry_dates)
    
    # Converting numpy datetime64 to string in 'YYYY-MM-DD' format
    for instrument_type in expiry_list:
        for name in expiry_list[instrument_type]:
            expiry_list[instrument_type][name] = [date.strftime('%Y-%m-%d') for date in expiry_list[instrument_type][name]]
    
    return json.dumps(expiry_list, indent=4)

def create_expiry_list2(df):
    expiry_list = {}
    
    # Converting expiry column to datetime and dropping NaT
    df = df.dropna(subset=['expiry'])
    df['expiry'] = pd.to_datetime(df['expiry'])
    
    # Getting unique expiry dates and sorting them
    unique_expiry_dates = sorted(df['expiry'].unique())
    
    for date in unique_expiry_dates:
        # Converting numpy datetime64 to pandas Timestamp
        date = pd.Timestamp(date)
        date_str = date.strftime('%Y-%m-%d')
        expiry_list[date_str] = {"fut": [], "options": []}
        
        # Filtering DataFrame for each unique expiry date
        df_date = df[df['expiry'] == date]
        
        # Adding futures symbols
        fut_symbols = df_date[df_date['instrument_type'] == 'FUT']['name'].unique()
        expiry_list[date_str]['fut'] = sorted(fut_symbols)
        
        # Adding options symbols
        option_symbols = df_date[df_date['instrument_type'].isin(['CE', 'PE'])]['name'].unique()
        expiry_list[date_str]['options'] = sorted(option_symbols)
    
    return json.dumps(expiry_list, indent=4)

def create_combined_expiry_list(df):
    # Getting the JSON data from both functions
    stockwise_json = json.loads(create_expiry_list(df))
    datewise_json = json.loads(create_expiry_list2(df))
    
    # Combining the JSON data
    combined_json = {
        "stockwise": stockwise_json,
        "datewise": datewise_json
    }
    
    # File path
    file_path = 'expiry.json'
    
    # Reading existing JSON data if file exists
    if os.path.exists(file_path):
        with open(file_path, 'r') as file:
            existing_json = json.load(file)
        
        # Merging the JSON data
        for leg in combined_json:
            if leg in existing_json:
                existing_json[leg].update(combined_json[leg])
            else:
                existing_json[leg] = combined_json[leg]
    else:
        existing_json = combined_json
    
    # Saving the merged JSON data
    with open(file_path, 'w') as file:
        json.dump(existing_json, file, indent=4)

    return json.dumps(existing_json, indent=4)