Skip to content

Issue: DataFrame is modified in place instead of copied #23

@matheus0sa

Description

@matheus0sa

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions