Reputation: 1063
I have a project which creates a number of pandas dataframes. These get manipulated all sorts of ways depending on user input and then get displayed using a Class function. At times it may be necessary for parts of the dataframe to be changed from the Class. So in essence I need to pass the dataframe to the Class.
Right now I'm accomplishing this as such (where TableDisp is the class):
ui = TableDisp()
self.ui.my_df = self.my_df
I realize as an alternative that I could pass through the class initialization with something like...
ui = TableDisp(self.my_df)
... if I set up the class this way, but different instances of the Class need different df's (which would need to be clearly identified). I.e. I'd need to do something like...
class TableDisp(Frame):
def __init__(self, my_df=None, another_df=None, more_df=None, still_more_df=None):
But with a dozen or so of these.
My current method is throwing ...
SettingWithCopyWarning:
... whenever I make a change to a df from the Class, which I can ignore, but it is making me wonder if I'm doing this in the most pythonic way. Any thoughts?
Upvotes: 1
Views: 388
Reputation: 425
For a simple fix I'd go with @Chris's comment using DataFrame.copy()
. However, it seems to me that since the class only needs to hold one dataframe at a time, you could simplify your class definition like so:
class TableDisp(Frame):
def __init__(self, df=None, df_type=None):
if df_type == "type1":
# Format df in a specific way
elif df_type == "type2":
# You get the idea
else:
raise ValueError("Unrecognised df_type")
Upvotes: 2