Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 112 additions & 4 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1538,22 +1538,76 @@ def idxmax(self, skipna: bool = True) -> Series:
"""
return self._idxmax_idxmin("idxmax", skipna=skipna)

@doc(Series.corr.__doc__)
def corr(
self,
other: Series,
method: CorrelationMethod = "pearson",
min_periods: int | None = None,
) -> Series:
"""
Compute correlation between each group and another Series.

Parameters
----------
other : Series
Series to compute correlation with.
method : {'pearson', 'kendall', 'spearman'}, default 'pearson'
Method of correlation to use.
min_periods : int, optional
Minimum number of observations required per pair of columns to
have a valid result.

Returns
-------
Series
Correlation value for each group.

See Also
--------
Series.corr : Equivalent method on ``Series``.

Examples
--------
>>> s = pd.Series([1, 2, 3, 4], index=[0, 0, 1, 1])
>>> g = s.groupby([0, 0, 1, 1])
>>> g.corr() # doctest: +SKIP
"""
result = self._op_via_apply(
"corr", other=other, method=method, min_periods=min_periods
)
return result

@doc(Series.cov.__doc__)
def cov(
self, other: Series, min_periods: int | None = None, ddof: int | None = 1
) -> Series:
"""
Compute covariance between each group and another Series.

Parameters
----------
other : Series
Series to compute covariance with.
min_periods : int, optional
Minimum number of observations required per pair of columns to
have a valid result.
ddof : int, optional
Delta degrees of freedom for variance calculation.

Returns
-------
Series
Covariance value for each group.

See Also
--------
Series.cov : Equivalent method on ``Series``.

Examples
--------
>>> s = pd.Series([1, 2, 3, 4], index=[0, 0, 1, 1])
>>> g = s.groupby([0, 0, 1, 1])
>>> g.cov() # doctest: +SKIP
"""
result = self._op_via_apply(
"cov", other=other, min_periods=min_periods, ddof=ddof
)
Expand Down Expand Up @@ -1607,7 +1661,6 @@ def is_monotonic_decreasing(self) -> Series:
"""
return self.apply(lambda ser: ser.is_monotonic_decreasing)

@doc(Series.hist.__doc__)
def hist(
self,
by=None,
Expand All @@ -1623,6 +1676,52 @@ def hist(
legend: bool = False,
**kwargs,
):
"""
Draw histogram for each group's values using :meth:`Series.hist` API.

Parameters
----------
by : object, optional
Grouping key.
ax : matplotlib.axes.Axes, optional
Axis to draw the histogram on.
grid : bool, default True
Show axis grid lines.
xlabelsize : int, default None
X axis label size.
xrot : float, default None
Rotation for x ticks.
ylabelsize : int, default None
Y axis label size.
yrot : float, default None
Rotation for y ticks.
figsize : tuple, optional
Figure size in inches.
bins : int or sequence, default 10
Number of histogram bins or bin edges.
backend : str or callable or None, optional
Plotting backend to use (e.g. 'matplotlib'). If None, use the default
plotting backend.
legend : bool, default False
Whether to draw the legend.
**kwargs
Additional keyword arguments passed to :meth:`Series.hist`.

Returns
-------
matplotlib.axes.Axes or ndarray of Axes
The returned matplotlib axes or array of axes depending on input.

See Also
--------
Series.hist : Equivalent histogram plotting method on Series.

Examples
--------
>>> df = pd.DataFrame({"val": [1, 2, 2, 3, 3, 3]}, index=[0, 0, 1, 1, 2, 2])
>>> g = df["val"].groupby([0, 0, 1, 1, 2, 2])
>>> g.hist() # doctest: +SKIP
"""
result = self._op_via_apply(
"hist",
by=by,
Expand All @@ -1641,8 +1740,17 @@ def hist(
return result

@property
@doc(Series.dtype.__doc__)
def dtype(self) -> Series:
"""
Return the dtype object of the underlying data for each group.

Mirrors :meth:`Series.dtype` applied group-wise.

Returns
-------
Series
Dtype of each group's values.
"""
return self.apply(lambda ser: ser.dtype)

def unique(self) -> Series:
Expand Down
Loading