raceplotly
raceplotly copied to clipboard
Issue: DataFrame is modified in place instead of copied
Currently, in the constructor the DataFrame is stored directly:
def __init__(self, df: pd.DataFrame = None, ...):
self.df = df
...
Later, when colors are assigned in __get_colors:
def __get_colors(self):
if self.item_color is None:
colors = {
item: 'rgb({}, {}, {})'.format(*sample(range(256), 3))
for item in self.df[self.item_column].unique()
}
self.df['color'] = self.df[self.item_column].map(colors)
this mutates the original DataFrame that was passed to the class.
I believe it would be safer to avoid side effects by making a copy in the constructor, e.g.:
self.df = df.copy()
This way, the internal logic won’t unintentionally change the caller’s DataFrame.