pandas.api.extensions.register_dataframe_accessor How to keep instance attributes on dataframe slicing

Viewed 88

I'm trying to use the dataframe extension functionality: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.api.extensions.register_dataframe_accessor.html#pandas.api.extensions.register_dataframe_accessor

However, the problem is that a lot of Pandas functionality returns slices of DataFrames which are actually new Pandas Dataframe objects. So, for example if when using the .loc() functions this returns a new Pandas Dataframe object which results in losing the previous object state. So, all previous instance attributes are lost.

For example: Below I have some instance attribute for the test_preprocessing class, min_feature_values.

enter image description here

And when calling the preprocessing function in the cell above (which contains that .loc[] code), the following happens:

enter image description here

That print statement "CREATING OBJECT" is in the __init__ function of the class with the decorator. What I think is happening is that a new Pandas Dataframe object is being returned so when I call the class alias name test_Preprocessing it's re-running the __init__ function as this class has not been run on this new object before.

What is the best way of avoiding this problem? Or to copy over the previous attributes to the new object?

0 Answers
Related