Skip to content

Commit 787577e

Browse files
committed
Fix for if weights is supplied by an array instead of the column name in the DataFrame
1 parent 5bbba11 commit 787577e

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

pandas/tools/plotting.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2949,16 +2949,15 @@ def grouped_hist(data, column=None, weights=None, by=None, ax=None, bins=50,
29492949
def plot_group(group, ax, weights=None):
29502950
if isinstance(group, np.ndarray) == False:
29512951
group = group.values
2952+
inx_na = np.isnan(group)
29522953
if weights is not None:
29532954
# remove fields where we have nan in weights OR in group
29542955
# for both data sets
29552956
if isinstance(weights, np.ndarray) == False:
29562957
weights = weights.values
2957-
inx_na = (np.isnan(weights)) | (np.isnan(group))
2958+
inx_na |= (np.isnan(weights))
29582959
weights = weights[~inx_na]
2959-
group = group[~inx_na]
2960-
else:
2961-
group = group.dropna()
2960+
group = group[~inx_na]
29622961
if len(group) > 0:
29632962
# if length is less than 0, we had only NaN's for this group
29642963
# nothing to print!
@@ -3064,6 +3063,11 @@ def _grouped_plot(plotf, data, column=None, weights=None, by=None,
30643063
"size by tuple instead", FutureWarning, stacklevel=4)
30653064
figsize = None
30663065

3066+
if isinstance(weights, np.ndarray):
3067+
# weights supplied as an array instead of a part of the dataframe
3068+
data['weights'] = weights
3069+
weights = 'weights'
3070+
30673071
grouped = data.groupby(by)
30683072
if column is not None:
30693073
if weights is not None:

0 commit comments

Comments
 (0)