Creating an instance of a custom class crashes IPython kernel ("Kernel appears to have died. It will restart automaticaly")

Viewed 41

I was recently creating a custom class in Python that was supposed to be a child class of pandas DataFrame. When I run the code to define the class - everything appears to be working - see code below:

import os

import numpy as np
import pandas as pd
from pandas.core.frame import DataFrame
import matplotlib.pyplot as plt


class CustomFrame(DataFrame):
    '''
    Docstring here
    '''
    
    def __init__(self, frame, samples, spots, errors, measurements, spotlines=None):
        
        self.samples = frame[samples].unique().tolist()
        self.lines = frame[spotlines].unique().tolist()
        self.spots = frame[spots].unique().tolist()
        
        only_data = frame.drop(columns=[samples, spotlines, spots, distances])
        cols = only_data.columns
        res_cols = cols[~cols.str.contains(errors)]
        err_cols = cols[cols.str.contains(errors)]
        
        self.results = frame.loc[:, res_cols]
        self.errors = frame.loc[:, err_cols]

But creating an instance of the class causes the kernel to restart:

test = pd.DataFrame({
    'Samples' : ['Samp1', 'Samp1', 'Samp2', 'Samp2'],
    'Lines' : ['Line1', 'Line2', 'Line1', 'Line1'],
    'Spots' : ['Spot1', 'Spot2', 'Spot1', 'Spot2'],
    'Dist' : [1.25, 12.50, 10.20, 33.30],
    'Values' : [101.32, 243.32, 54.65, 984.43],
    'err' : [5.01, 10.20, 4.54, 50.21]
    })

myclass = CustomFrame(test, samples = 'Samples', spots = 'Spots', errors = 'err', distances="Dist", spotlines='Line')

First I encountered it in Jupyter Notebook/JupyterLab and the message The kernel for [filepath] appears to have died. It will restart automatically popped up. I tried also in Spyder and the kernel restarted as well.

Am I doing something wrong and don't see it or is there another potential cause?

0 Answers
Related