-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Allow to plot weighted KDEs. #59087
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
Comments
Hello, I am working on it. |
I updated the following https://github.com/fbourgey/pandas/blob/feature-plot-weighted-kde/pandas/plotting/_core.py#L1449 The code works. Should we add one example in the function |
The following code gives s = pd.Series([1, 2, 2.5, 3, 3.5, 4, 5])
ax = s.plot.kde() Replacing with some weights = pd.Series([0.1, 0.0, 0.0, 0.2, 0.3, 0.4, 0.9])
ax = s.plot.kde(weights=weights) Using a Numpy Array works as well weights = np.array([0.1, 0.4, 0.0, 0.2, 0.3, 0.4, 0.2]) However, passing a weights = [0.1, 0.4, 0.0, 0.2, 0.3, 0.4, 0.2] raises the following error File "/Users/florianbourgey/projects/misc/pandas_gaussian_kde.py", line 7, in <module>
ax = s.plot.kde(weights=weights)
File "/Users/florianbourgey/projects/pandas/pandas/plotting/_core.py", line 1567, in kde
return self(kind="kde", bw_method=bw_method, weights=weights, ind=ind, **kwargs)
File "/Users/florianbourgey/projects/pandas/pandas/plotting/_core.py", line 1049, in __call__
return plot_backend.plot(data, kind=kind, **kwargs)
File "/Users/florianbourgey/projects/pandas/pandas/plotting/_matplotlib/__init__.py", line 71, in plot
plot_obj.generate()
File "/Users/florianbourgey/projects/pandas/pandas/plotting/_matplotlib/core.py", line 500, in generate
self._make_plot(fig)
File "/Users/florianbourgey/projects/pandas/pandas/plotting/_matplotlib/hist.py", line 168, in _make_plot
kwds["weights"] = type(self)._get_column_weights(self.weights, i, y)
File "/Users/florianbourgey/projects/pandas/pandas/plotting/_matplotlib/hist.py", line 202, in _get_column_weights
weights = weights[~isna(y)] |
Feature Type
Adding new functionality to pandas
Changing existing functionality in pandas
Removing existing functionality in pandas
Problem Description
The current implementation does not currently allow to plot weighted KDEs.
Feature Description
Estimation of the PDF is currently done via scipy.stats.gaussian_kde which allows for a parameter
weights
.pandas.DataFrame.plot.kde should accept this parameter as well.
Alternative Solutions
Here allow to pass a parameter
weights
toscipy.stats.gaussian_kde
.Additional Context
No response
The text was updated successfully, but these errors were encountered: