2

I do not understand this apply behavior .. pandas apply updates inplace but returns None

>>>import pandas as pd
>>>df_test = pd.Series({0: {'A', 'E'},
                     1: {'A', 'C', 'E', 'S'},
                     2: {'A', 'C', 'E'},
                     3: {'A', 'C', 'E', 'S'},
                     4: {'A', 'E'}})
>>>df_test
0          {A, E}
1    {A, S, E, C}
2       {A, C, E}
3    {A, S, E, C}
4          {A, E}
dtype: object

>>>df_test.apply(lambda x: x.add("X"))
0    None
1    None
2    None
3    None
4    None
dtype: object

but,

>>>df_test
0          {A, X, E}
1    {A, X, S, E, C}
2       {A, X, C, E}
3    {A, X, S, E, C}
4          {A, X, E}
dtype: object

so df_test is updated. but then if I do this:

>>> df_test = df_test.apply(lambda x: x.add("X"))
>>> df_test
0    None
1    None
2    None
3    None
4    None
dtype: object

what is the explanation for thisbehavior - the apply is updating the dataframe in place but returned type is None.

muon
  • 12,821
  • 11
  • 69
  • 88

2 Answers2

3

Adding to a set is an in-place operation that returns None, so while the Series will be updated, the copy returned from apply will have None set in every row.

If you really want to view the results when you call apply, instead of adding to the set, you could use the union of two sets to get your desired results:

df_test.apply(lambda x: x | {'X'})

0          {A, X, E}
1    {A, X, E, C, S}
2       {A, X, E, C}
3    {A, X, E, C, S}
4          {A, X, E}

Which does not modify the original Series:

df_test   

0          {A, E}  
1    {A, E, C, S}  
2       {A, E, C}  
3    {A, E, C, S}  
4          {A, E}  
user3483203
  • 50,081
  • 9
  • 65
  • 94
  • nice. may be this is my ignorance, but I have never come across this usage of pipe in python for sets – muon Jul 12 '18 at 16:20
1

You operate on python sets inside you apply function. Using add is an in place operation on that set, and it (the add function) returns None. To do what you want, you can do something like:

df_test = df_test.apply(lambda x: x |{"X"})

This will return a new set, which is the original set plus X, för each row.

ALollz
  • 57,915
  • 7
  • 66
  • 89
ilmarinen
  • 4,557
  • 3
  • 16
  • 12