has2k1 / plotnine

A Grammar of Graphics for Python
https://plotnine.org
MIT License
3.89k stars 209 forks source link

TypeError: asarray() got an unexpected keyword argument 'copy' with geom_smooth(method='loess') #811

Open cauricht opened 2 weeks ago

cauricht commented 2 weeks ago

Description

When using geom_smooth with the loess method, I encounter a TypeError with the message asarray() got an unexpected keyword argument 'copy'. This issue does not occur with other smoothing methods like lm or lowess.

Reproduction Steps

Here are the steps to reproduce the issue:

  1. Install the required packages:

    
    !pip install pandas plotnine scikit-misc numpy matplotlib
    
    2. Run this code
    %matplotlib inline

import pandas as pd import numpy as np from plotnine import ggplot, aes, geom_smooth, labs, theme, element_text

Generate random data

np.random.seed(0) random_data = { 'Date': pd.date_range(start='2020-01-01', periods=300, freq='D'), 'S2_band': np.linspace(400, 2500, 300), 'spec_value': np.sin(np.linspace(0, 20, 300)) + np.random.normal(scale=0.1, size=300), 'Site': ['SDFS_Treat_1']150 + ['SDFS_ref']150 }

random_df = pd.DataFrame(random_data)

Filtered data for the year 2020

filtered_df = random_df[pd.to_datetime(random_df['Date']).dt.year == 2020]

Create the plot with geom_smooth

p = (ggplot(filtered_df, aes(x='S2_band', y='spec_value', color='Site'))

Display the plot

p.show()

Expected behavior

The plot should be generated with geom_smooth using the loess method without any errors.

Actual behavior

A TypeError is raised: TypeError: asarray() got an unexpected keyword argument 'copy' Environment:

pandas version: 2.2.2
plotnine version: 0.13.6
scikit-misc version: 0.4.0
numpy version: 1.26.4
matplotlib version: 3.9.0

Additional context

Other smoothing methods like lm and gam work correctly.

Traceback (most recent call last): File "/env/lib/python3.10/site-packages/plotnine/stats/stat.py", line 306, in fn return cls.compute_panel(pdata, pscales, **params) ... TypeError: asarray() got an unexpected keyword argument 'copy'

Steps Taken to Resolve

Uninstalled and reinstalled all related packages (pandas, plotnine, scikit-misc, numpy, matplotlib).
Verified the issue persists only with the loess method in geom_smooth.
Rolled back to earlier versions of the related packages but the issue persists.
cauricht commented 2 weeks ago

Issue has only arose after 22 May 2024 - and was current on 26 June However re-running code today (27 June) it works again without trace back error! Even with all packages updated to The version of pandas is 2.2.2 The version of plotnine is 0.13.6 The version of numpy is 2.0.0 The version of matplotlib is 3.9.0 The version of scikit-misc is 0.4.0 The version of statsmodels is 0.14.2

christophscheuch commented 6 days ago

I have the same error with plotnine version 0.13.6 and numpy version 1.26.0. Any suggestions which library is the culprit?

from plotnine import ggplot, aes, geom_point, geom_smooth, labs
from plotnine.data import mpg

(
    ggplot(mpg, aes(x="displ", y="hwy"))
    + geom_point()
    + geom_smooth()
    + labs(x="displacement", y="horsepower")
)

import plotnine
print(plotnine.__version__)
cauricht commented 6 days ago

Hi Christoph - Greetings from Australia! FYI, I found issue seemed to be inconsistent and specific to geom_smooth loess method.

After digging deeper conflicts may be related to scikit-misc and numpy.

Based on above, I found the following package stack and version combination seems to work and run geom-smooth loess consistently.

The version of pandas is 2.2.2 The version of plotnine is 0.13.6 The version of scikit-misc is 0.3.0 The version of numpy is 1.26.4 The version of matplotlib is 3.8.4

Might be that matplotlib ver 3.9.0 may work in above, but given things are stable and working I've left as above for the moment!

Hope this helps.

Rgds, C

christophscheuch commented 5 days ago

Ah, downgrading to scikit-misc 0.3.0 did the trick! I'll try to keep it this way for the time being as well.

cauricht commented 5 days ago

Good to hear it works for you as well. Rgds, C