Sobre la base @ Godsmith de train of thought y abordar @ necesidad de Zitrax (creo) para hacer el copia de datos para todos los atributos dentro del constructor:
class ConfusionMatrix(pd.DataFrame):
def __init__(self, df, *args, **kwargs):
try:
# Check if `df` looks like a `ConfusionMatrix`
# Could check `isinstance(df, ConfusionMatrix)`
# But might miss some "ConfusionMatrix-elligible" `DataFrame`s
assert((df.columns == df.index).all())
assert(df.values.dtype == int)
self.construct_copy(df, *args, **kwargs)
return
except (AssertionError, AttributeError, ValueError):
pass
# df is just data, so continue with normal constructor here ...
def construct_copy(self, other, *args, **kwargs):
# construct a parent DataFrame instance
parent_type = super(ConfusionMatrix, self)
parent_type.__init__(other)
for k, v in other.__dict__.iteritems():
if hasattr(parent_type, k) and hasattr(self, k) and getattr(parent_type, k) == getattr(self, k):
continue
setattr(self, k, deepcopy(v))
Esta clase hereda una ConfusionMatrix
pandas.DataFrame
y añade un montón de otra attribut es y métodos que deben ser recalculados a menos que se puedan copiar los datos de la matriz other
. Buscando una solución es cómo encontré esta pregunta.
Usted puede estar interesado en esta cuestión [Disclaimer: fue el que lo preguntó]: http://stackoverflow.com/questions/990758/reclassing-an-instance-in-python – balpha
Ten cuidado. Las advertencias publicadas por algunos de los respondedores no deben subestimarse. – balpha
Para que no se vea muy legible, probablemente cambie mi código de usar herencia para encapsular el otro objeto. – Zitrax