|
15 | 15 | from pandas.tseries.index import DatetimeIndex
|
16 | 16 | from pandas.tseries.period import PeriodIndex
|
17 | 17 | from pandas.core.internals import BlockManager
|
| 18 | +import pandas.core.algorithms as algos |
18 | 19 | import pandas.core.common as com
|
19 | 20 | import pandas.core.missing as mis
|
20 | 21 | import pandas.core.datetools as datetools
|
@@ -3751,6 +3752,66 @@ def last(self, offset):
|
3751 | 3752 | start = self.index.searchsorted(start_date, side='right')
|
3752 | 3753 | return self.ix[start:]
|
3753 | 3754 |
|
| 3755 | + def rank(self, axis=0, method='average', numeric_only=None, |
| 3756 | + na_option='keep', ascending=True, pct=False): |
| 3757 | + """ |
| 3758 | + Compute numerical data ranks (1 through n) along axis. Equal values are |
| 3759 | + assigned a rank that is the average of the ranks of those values |
| 3760 | +
|
| 3761 | + Parameters |
| 3762 | + ---------- |
| 3763 | + axis: {0 or 'index', 1 or 'columns'}, default 0 |
| 3764 | + index to direct ranking |
| 3765 | + method : {'average', 'min', 'max', 'first', 'dense'} |
| 3766 | + * average: average rank of group |
| 3767 | + * min: lowest rank in group |
| 3768 | + * max: highest rank in group |
| 3769 | + * first: ranks assigned in order they appear in the array |
| 3770 | + * dense: like 'min', but rank always increases by 1 between groups |
| 3771 | + numeric_only : boolean, default None |
| 3772 | + Include only float, int, boolean data. Valid only for DataFrame or |
| 3773 | + Panel objects |
| 3774 | + na_option : {'keep', 'top', 'bottom'} |
| 3775 | + * keep: leave NA values where they are |
| 3776 | + * top: smallest rank if ascending |
| 3777 | + * bottom: smallest rank if descending |
| 3778 | + ascending : boolean, default True |
| 3779 | + False for ranks by high (1) to low (N) |
| 3780 | + pct : boolean, default False |
| 3781 | + Computes percentage rank of data |
| 3782 | +
|
| 3783 | + Returns |
| 3784 | + ------- |
| 3785 | + ranks : same type as caller |
| 3786 | + """ |
| 3787 | + axis = self._get_axis_number(axis) |
| 3788 | + |
| 3789 | + if self.ndim > 2: |
| 3790 | + msg = "rank does not make sense when ndim > 2" |
| 3791 | + raise NotImplementedError(msg) |
| 3792 | + |
| 3793 | + def ranker(data): |
| 3794 | + ranks = algos.rank(data.values, axis=axis, method=method, |
| 3795 | + ascending=ascending, na_option=na_option, |
| 3796 | + pct=pct) |
| 3797 | + ranks = self._constructor(ranks, **data._construct_axes_dict()) |
| 3798 | + return ranks.__finalize__(self) |
| 3799 | + |
| 3800 | + # if numeric_only is None, and we can't get anything, we try with |
| 3801 | + # numeric_only=True |
| 3802 | + if numeric_only is None: |
| 3803 | + try: |
| 3804 | + return ranker(self) |
| 3805 | + except TypeError: |
| 3806 | + numeric_only = True |
| 3807 | + |
| 3808 | + if numeric_only: |
| 3809 | + data = self._get_numeric_data() |
| 3810 | + else: |
| 3811 | + data = self |
| 3812 | + |
| 3813 | + return ranker(data) |
| 3814 | + |
3754 | 3815 | _shared_docs['align'] = ("""
|
3755 | 3816 | Align two object on their axes with the
|
3756 | 3817 | specified join method for each axis Index
|
|
0 commit comments