pandas - set attribute when attribute changes in python -
i have class has attribute pandas dataframe. define attribute in same class changes when dataframe altered. thought setter might correct approach haven't been able work. here toy example:
import pandas pd class foo(object): def __init__(self, df=none): self._df = df self.altered = false # bool, set true if self._df changes @property def df(self): return self._df @df.setter def df(self, df): self.altered = true # since _df might change here set self.altered true self._df = df df_test = pd.dataframe([[1,2,3]], columns=['a','b','c']) cl = foo(df_test) cl.df.loc[0, 'a'] = 3 # change default value in dataframe print(cl.df) print(cl.altered)
which prints altered dataframe cl.altered still set false.
any suggestions?
i don't know pandas. setter isn't called because line was:
cl.df.loc[0, 'a'] = 3
it have been called if line was:
cl.df = 3
one thing store copy of original df
when instance initialized , like:
@property def altered(self): return self._orig_df == self._current_df
Comments
Post a Comment