functools reduce In-Place modifies original dataframe

2177 views python
-2

I currently facing the issue that "functools.reduce(operator.iadd,...)" alters the original input. E.g.

I have a simple dataframe df = pd.DataFrame([[['A', 'B']], [['C', 'D']]])

        0
0  [A, B]
1  [C, D]

Applying the iadd operator leads to following result:

functools.reduce(operator.iadd, df[0])
['A', 'B', 'C', 'D']

Now, the original df changed to

              0
0  [A, B, C, D]
1        [C, D]

Also copying the df using df.copy(deep=True) beforehand does not help.

Has anyone an idea to overcome this issue? THX, Lazloo

answered question

From Unutbu's answer, i realized it ...

2 Answers

2

Use operator.add instead of operator.iadd:

In [8]: functools.reduce(operator.add, df[0])
Out[8]: ['A', 'B', 'C', 'D']

In [9]: df
Out[9]: 
        0
0  [A, B]
1  [C, D]

After all, operator.iadd(a, b) is the same as a += b. So it modifies df[0]. In contrast, operator.add(a, b) returns a + b, so there is no modification of df[0].

posted this
8

In addition to @unutbu's good answer, you can also use the int.__add__ method:

df = pd.DataFrame([[['A', 'B']], [['C', 'D']]])
functools.reduce(lambda x,y: (x).__add__(y), df[0])
print(df)

And You can see that it is:

        0
0  [A, B]
1  [C, D]

For output!!!

posted this

Have an answer?

JD

Please login first before posting an answer.