|
47 | 47 | )
|
48 | 48 |
|
49 | 49 | import pandas as pd
|
50 |
| -from pandas.core import algorithms, base, generic, nanops, ops |
| 50 | +from pandas.core import algorithms, base, generic, groupby, nanops, ops |
51 | 51 | from pandas.core.accessor import CachedAccessor
|
52 | 52 | from pandas.core.arrays import ExtensionArray, try_cast_to_ea
|
53 | 53 | from pandas.core.arrays.categorical import Categorical, CategoricalAccessor
|
@@ -1565,6 +1565,89 @@ def _set_name(self, name, inplace=False):
|
1565 | 1565 | ser.name = name
|
1566 | 1566 | return ser
|
1567 | 1567 |
|
| 1568 | + @Appender( |
| 1569 | + """ |
| 1570 | +Examples |
| 1571 | +-------- |
| 1572 | +>>> ser = pd.Series([390., 350., 30., 20.], |
| 1573 | +... index=['Falcon', 'Falcon', 'Parrot', 'Parrot'], name="Max Speed") |
| 1574 | +>>> ser |
| 1575 | +Falcon 390.0 |
| 1576 | +Falcon 350.0 |
| 1577 | +Parrot 30.0 |
| 1578 | +Parrot 20.0 |
| 1579 | +Name: Max Speed, dtype: float64 |
| 1580 | +>>> ser.groupby(["a", "b", "a", "b"]).mean() |
| 1581 | +a 210.0 |
| 1582 | +b 185.0 |
| 1583 | +Name: Max Speed, dtype: float64 |
| 1584 | +>>> ser.groupby(level=0).mean() |
| 1585 | +Falcon 370.0 |
| 1586 | +Parrot 25.0 |
| 1587 | +Name: Max Speed, dtype: float64 |
| 1588 | +>>> ser.groupby(ser > 100).mean() |
| 1589 | +Max Speed |
| 1590 | +False 25.0 |
| 1591 | +True 370.0 |
| 1592 | +Name: Max Speed, dtype: float64 |
| 1593 | +
|
| 1594 | +**Grouping by Indexes** |
| 1595 | +
|
| 1596 | +We can groupby different levels of a hierarchical index |
| 1597 | +using the `level` parameter: |
| 1598 | +
|
| 1599 | +>>> arrays = [['Falcon', 'Falcon', 'Parrot', 'Parrot'], |
| 1600 | +... ['Captive', 'Wild', 'Captive', 'Wild']] |
| 1601 | +>>> index = pd.MultiIndex.from_arrays(arrays, names=('Animal', 'Type')) |
| 1602 | +>>> ser = pd.Series([390., 350., 30., 20.], index=index, name="Max Speed") |
| 1603 | +>>> ser |
| 1604 | +Animal Type |
| 1605 | +Falcon Captive 390.0 |
| 1606 | + Wild 350.0 |
| 1607 | +Parrot Captive 30.0 |
| 1608 | + Wild 20.0 |
| 1609 | +Name: Max Speed, dtype: float64 |
| 1610 | +>>> ser.groupby(level=0).mean() |
| 1611 | +Animal |
| 1612 | +Falcon 370.0 |
| 1613 | +Parrot 25.0 |
| 1614 | +Name: Max Speed, dtype: float64 |
| 1615 | +>>> ser.groupby(level="Type").mean() |
| 1616 | +Type |
| 1617 | +Captive 210.0 |
| 1618 | +Wild 185.0 |
| 1619 | +Name: Max Speed, dtype: float64 |
| 1620 | +""" |
| 1621 | + ) |
| 1622 | + @Appender(generic._shared_docs["groupby"] % _shared_doc_kwargs) |
| 1623 | + def groupby( |
| 1624 | + self, |
| 1625 | + by=None, |
| 1626 | + axis=0, |
| 1627 | + level=None, |
| 1628 | + as_index: bool = True, |
| 1629 | + sort: bool = True, |
| 1630 | + group_keys: bool = True, |
| 1631 | + squeeze: bool = False, |
| 1632 | + observed: bool = False, |
| 1633 | + ) -> "groupby.SeriesGroupBy": |
| 1634 | + |
| 1635 | + if level is None and by is None: |
| 1636 | + raise TypeError("You have to supply one of 'by' and 'level'") |
| 1637 | + axis = self._get_axis_number(axis) |
| 1638 | + |
| 1639 | + return groupby.SeriesGroupBy( |
| 1640 | + obj=self, |
| 1641 | + keys=by, |
| 1642 | + axis=axis, |
| 1643 | + level=level, |
| 1644 | + as_index=as_index, |
| 1645 | + sort=sort, |
| 1646 | + group_keys=group_keys, |
| 1647 | + squeeze=squeeze, |
| 1648 | + observed=observed, |
| 1649 | + ) |
| 1650 | + |
1568 | 1651 | # ----------------------------------------------------------------------
|
1569 | 1652 | # Statistics, overridden ndarray methods
|
1570 | 1653 |
|
|
0 commit comments