function - Pandas: whole column used as input modified -


i have little function performing cumsum of nan values on column in pandas. function little bit tricky since want reset of values when switching nan non-nan cells.

ex: [1., 1., nan, nan, 2., nan, nan, 3.] gives [0., 0., 1., 2., 0., 1., 2., 0.]

anyway function working , here is:

def count_nan_reset(v): vm = v.copy() vm = v.as_matrix() vm[~np.isnan(vm)] = 2 # arbitraire vm[np.isnan(vm)] = 1 vm[vm==2] = np.nan n = np.isnan(vm) = ~n c = np.cumsum(a) d = np.diff(np.concatenate(([0.], c[n]))) vm[n] = -d fin = np.cumsum(vm) return fin 

the issue have when try apply function column input, changes columns (as inplace = true option do)!!

for instance:

d = {'values_for_trial' : pd.series([1., 1., np.nan, np.nan, 2., np.nan, np.nan, 3.])} df = pd.dataframe(d) df["results"] = count_nan_reset(df["values_for_trial"]) 

well changes values inside df["values_for_trial"]

i not know why if can thank much!!

because vm not copy,

you need change

def count_nan_reset(v):     vm = v.copy()     vm = v.as_matrix() 

to

def count_nan_reset(v):     vm = v.copy().as_matrix()     ... 

Comments

Popular posts from this blog

'hasOwnProperty' in javascript -

python - ValueError: No axis named 1 for object type <class 'pandas.core.series.Series'> -

java - How to provide dependency injections in Eclipse RCP 3.x? -