|
59 | 59 | is_empty_data,
|
60 | 60 | sanitize_array,
|
61 | 61 | )
|
62 |
| -from pandas.core.generic import _shared_docs |
| 62 | +from pandas.core.groupby import generic as groupby_generic |
63 | 63 | from pandas.core.indexers import maybe_convert_indices
|
64 | 64 | from pandas.core.indexes.accessors import CombinedDatetimelikeProperties
|
65 | 65 | from pandas.core.indexes.api import (
|
@@ -1431,7 +1431,7 @@ def to_string(
|
1431 | 1431 | """
|
1432 | 1432 | )
|
1433 | 1433 | @Substitution(klass="Series")
|
1434 |
| - @Appender(_shared_docs["to_markdown"]) |
| 1434 | + @Appender(generic._shared_docs["to_markdown"]) |
1435 | 1435 | def to_markdown(
|
1436 | 1436 | self, buf: Optional[IO[str]] = None, mode: Optional[str] = None, **kwargs,
|
1437 | 1437 | ) -> Optional[str]:
|
@@ -1568,6 +1568,89 @@ def _set_name(self, name, inplace=False):
|
1568 | 1568 | ser.name = name
|
1569 | 1569 | return ser
|
1570 | 1570 |
|
| 1571 | + @Appender( |
| 1572 | + """ |
| 1573 | +Examples |
| 1574 | +-------- |
| 1575 | +>>> ser = pd.Series([390., 350., 30., 20.], |
| 1576 | +... index=['Falcon', 'Falcon', 'Parrot', 'Parrot'], name="Max Speed") |
| 1577 | +>>> ser |
| 1578 | +Falcon 390.0 |
| 1579 | +Falcon 350.0 |
| 1580 | +Parrot 30.0 |
| 1581 | +Parrot 20.0 |
| 1582 | +Name: Max Speed, dtype: float64 |
| 1583 | +>>> ser.groupby(["a", "b", "a", "b"]).mean() |
| 1584 | +a 210.0 |
| 1585 | +b 185.0 |
| 1586 | +Name: Max Speed, dtype: float64 |
| 1587 | +>>> ser.groupby(level=0).mean() |
| 1588 | +Falcon 370.0 |
| 1589 | +Parrot 25.0 |
| 1590 | +Name: Max Speed, dtype: float64 |
| 1591 | +>>> ser.groupby(ser > 100).mean() |
| 1592 | +Max Speed |
| 1593 | +False 25.0 |
| 1594 | +True 370.0 |
| 1595 | +Name: Max Speed, dtype: float64 |
| 1596 | +
|
| 1597 | +**Grouping by Indexes** |
| 1598 | +
|
| 1599 | +We can groupby different levels of a hierarchical index |
| 1600 | +using the `level` parameter: |
| 1601 | +
|
| 1602 | +>>> arrays = [['Falcon', 'Falcon', 'Parrot', 'Parrot'], |
| 1603 | +... ['Captive', 'Wild', 'Captive', 'Wild']] |
| 1604 | +>>> index = pd.MultiIndex.from_arrays(arrays, names=('Animal', 'Type')) |
| 1605 | +>>> ser = pd.Series([390., 350., 30., 20.], index=index, name="Max Speed") |
| 1606 | +>>> ser |
| 1607 | +Animal Type |
| 1608 | +Falcon Captive 390.0 |
| 1609 | + Wild 350.0 |
| 1610 | +Parrot Captive 30.0 |
| 1611 | + Wild 20.0 |
| 1612 | +Name: Max Speed, dtype: float64 |
| 1613 | +>>> ser.groupby(level=0).mean() |
| 1614 | +Animal |
| 1615 | +Falcon 370.0 |
| 1616 | +Parrot 25.0 |
| 1617 | +Name: Max Speed, dtype: float64 |
| 1618 | +>>> ser.groupby(level="Type").mean() |
| 1619 | +Type |
| 1620 | +Captive 210.0 |
| 1621 | +Wild 185.0 |
| 1622 | +Name: Max Speed, dtype: float64 |
| 1623 | +""" |
| 1624 | + ) |
| 1625 | + @Appender(generic._shared_docs["groupby"] % _shared_doc_kwargs) |
| 1626 | + def groupby( |
| 1627 | + self, |
| 1628 | + by=None, |
| 1629 | + axis=0, |
| 1630 | + level=None, |
| 1631 | + as_index: bool = True, |
| 1632 | + sort: bool = True, |
| 1633 | + group_keys: bool = True, |
| 1634 | + squeeze: bool = False, |
| 1635 | + observed: bool = False, |
| 1636 | + ) -> "groupby_generic.SeriesGroupBy": |
| 1637 | + |
| 1638 | + if level is None and by is None: |
| 1639 | + raise TypeError("You have to supply one of 'by' and 'level'") |
| 1640 | + axis = self._get_axis_number(axis) |
| 1641 | + |
| 1642 | + return groupby_generic.SeriesGroupBy( |
| 1643 | + obj=self, |
| 1644 | + keys=by, |
| 1645 | + axis=axis, |
| 1646 | + level=level, |
| 1647 | + as_index=as_index, |
| 1648 | + sort=sort, |
| 1649 | + group_keys=group_keys, |
| 1650 | + squeeze=squeeze, |
| 1651 | + observed=observed, |
| 1652 | + ) |
| 1653 | + |
1571 | 1654 | # ----------------------------------------------------------------------
|
1572 | 1655 | # Statistics, overridden ndarray methods
|
1573 | 1656 |
|
|
0 commit comments