Skip to content

Weight aes generates ValueError: assignment destination is read-only #936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
krivard opened this issue Apr 25, 2025 · 0 comments
Closed
Labels

Comments

@krivard
Copy link

krivard commented Apr 25, 2025

Hey, I was hoping to use plotnine to make a weighted histogram, but it looks like the weight aesthetic isn't working right. This may be related to pandas' shift to returning read-only numpy arrays -- you may be missing an explicit copy.

Given:

import plotnine as p9
import pandas as pd

df = pd.DataFrame({
    "x": list(range(10)),
    "w": list(range(10)),
})

(
    p9.ggplot(df, p9.aes(x="x", weight="w"))
    +p9.geom_histogram(bins=10)
)

Expected:

A histogram with ten bins, starting low and getting higher from left to right

Actual:

[...]
File [.../python3.12/site-packages/plotnine/stats/binning.py:169](.../python3.12/site-packages/plotnine/stats/binning.py#line=168), in assign_bins(x, breaks, weight, pad, closed)
    167 else:
    168     weight = np.asarray(weight)
--> 169     weight[np.isnan(weight)] = 0
    171 bin_idx = pd.cut(
    172     x,
    173     bins=breaks,  # type: ignore
   (...)    176     include_lowest=True,
    177 )
    178 bin_widths = np.diff(breaks)

ValueError: assignment destination is read-only
@has2k1 has2k1 added the bug label Apr 25, 2025
@has2k1 has2k1 closed this as completed in 82c20a0 May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants