verystrongjoe
verystrongjoe

Reputation: 3921

TypeError: must be real number, not NoneType

I run this code as follows. I don't know why I got this error in the end.

Code

df_price_population.info()

Output

<class 'pandas.core.frame.DataFrame'>
Int64Index: 59177 entries, 0 to 28894
Data columns (total 4 columns):
 #   Column      Non-Null Count  Dtype  
---  ------      --------------  -----  
 0   danji_id    59177 non-null  object 
 1   area1       59177 non-null  float64
 2   yyyymmdd    59177 non-null  object 
 3   candidates  48228 non-null  float64
dtypes: float64(2), object(2)
memory usage: 2.3+ MB

Code

df_price_population.isna().sum()

Output

danji_id,0
area1,0
yyyymmdd,0
candidates,10949

Code

df_price_population.isna().sum()
df_price_population.groupby(by=['danji_id', 'area1'])['candidates'].rolling(window=7).apply(lambda r : print(r))

Output

File ~/anaconda3/envs/richgo_eda_template/lib/python3.8/site-packages/pandas/_libs/window/aggregations.pyx:1315, in pandas._libs.window.aggregations.roll_apply()

TypeError: must be real number, not NoneType

Upvotes: 0

Views: 1665

Answers (1)

sitting_duck
sitting_duck

Reputation: 3720

Starting in 1.1.0 the function of the apply is now expected to return a single value to affect the corresponding column with.

To fix it you should be able to:

def print_with_return(z):
    print(z) 
    return 0

Then apply it:

df_price_population.groupby(by=['danji_id', 'area1'])['candidates'].rolling(window=7).apply(print_with_return)

Upvotes: 1

Related Questions