raphaelvallat / pingouin

Statistical package in Python based on Pandas
https://pingouin-stats.org/
GNU General Public License v3.0
1.57k stars 137 forks source link

Bug in ANCOVA function (ValueError: assignment destination is read-only) #421

Open plankter opened 2 months ago

plankter commented 2 months ago

Hi,

First, thank you for a very useful library!

With the following versions installed:

Python: 3.12.3 Pingouin: 0.5.4 Pandas: 2.2.2 Numpy: 1.26.4

In the file parametric.py [lines: 1739, 1743] there is a problem with ancova(...) function. This code tries to assign a value to read-only numpy array:

ss_resid = aov["SS"].iloc[-1]
all_effsize = aov["SS"].apply(lambda x: x / (x + ss_resid)).to_numpy()
all_effsize[-1] = np.nan

ValueError: assignment destination is read-only

If one prints flags of all_effsize array with print(all_effsize.flags), the output is the following:

C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : False WRITEABLE : False ALIGNED : True WRITEBACKIFCOPY : False

Making a copy of the array before assignment fixes the problem:

all_effsize = all_effsize.copy()
all_effsize[-1] = np.nan

Best regards, Anton

raphaelvallat commented 1 month ago

Hey,

Can you please provide a minimal code example to reproduce the error? I'm not able to reproduce it with the example from Pingouin's documentation.

Can you also please check whether the following works:

ss_resid = aov["SS"].iloc[-1]
all_effsize = aov["SS"].apply(lambda x: x / (x + ss_resid)).to_numpy(copy=True)
all_effsize[-1] = np.nan

If so, please feel free to submit a quick PR.

Thanks, Raphael