Skip to content

Commit d91549a

Browse files
authored
Merge pull request #4 from AnonToky/AnonToky-patch-doc
Refactor datetimelike.py by cleaning up decorators
2 parents 04d3293 + 5c9d1be commit d91549a

File tree

1 file changed

+133
-15
lines changed

1 file changed

+133
-15
lines changed

pandas/core/indexes/datetimelike.py

Lines changed: 133 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@
3636
NullFrequencyError,
3737
)
3838
from pandas.util._decorators import (
39-
Appender,
4039
cache_readonly,
41-
doc,
4240
)
4341

4442
from pandas.core.dtypes.common import (
@@ -57,12 +55,10 @@
5755
PeriodArray,
5856
TimedeltaArray,
5957
)
60-
from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin
6158
import pandas.core.common as com
6259
import pandas.core.indexes.base as ibase
6360
from pandas.core.indexes.base import (
6461
Index,
65-
_index_shared_docs,
6662
)
6763
from pandas.core.indexes.extension import NDArrayBackedExtensionIndex
6864
from pandas.core.indexes.range import RangeIndex
@@ -92,8 +88,22 @@ class DatetimeIndexOpsMixin(NDArrayBackedExtensionIndex, ABC):
9288
_can_hold_strings = False
9389
_data: DatetimeArray | TimedeltaArray | PeriodArray
9490

95-
@doc(DatetimeLikeArrayMixin.mean)
9691
def mean(self, *, skipna: bool = True, axis: int | None = 0):
92+
"""
93+
Return the mean value of the Array.
94+
95+
Parameters
96+
----------
97+
skipna : bool, default True
98+
Whether to ignore any NaT elements.
99+
axis : int, optional, default 0
100+
Axis along which to compute the mean.
101+
102+
Returns
103+
-------
104+
scalar
105+
Timestamp or Timedelta.
106+
"""
97107
return self._data.mean(skipna=skipna, axis=axis)
98108

99109
@property
@@ -136,8 +146,10 @@ def asi8(self) -> npt.NDArray[np.int64]:
136146
return self._data.asi8
137147

138148
@property
139-
@doc(DatetimeLikeArrayMixin.freqstr)
140149
def freqstr(self) -> str:
150+
"""
151+
Return the frequency string if it is set, otherwise None.
152+
"""
141153
from pandas import PeriodIndex
142154

143155
if self._data.freqstr is not None and isinstance(
@@ -153,8 +165,15 @@ def freqstr(self) -> str:
153165
def _resolution_obj(self) -> Resolution: ...
154166

155167
@cache_readonly
156-
@doc(DatetimeLikeArrayMixin.resolution)
157168
def resolution(self) -> str:
169+
"""
170+
Return the resolution of the array.
171+
172+
Returns
173+
-------
174+
str
175+
The resolution of the array.
176+
"""
158177
return self._data.resolution
159178

160179
# ------------------------------------------------------------------------
@@ -199,8 +218,10 @@ def equals(self, other: Any) -> bool:
199218

200219
return np.array_equal(self.asi8, other.asi8)
201220

202-
@Appender(Index.__contains__.__doc__)
203221
def __contains__(self, key: Any) -> bool:
222+
"""
223+
Return True if the key is in the Index.
224+
"""
204225
hash(key)
205226
try:
206227
self.get_loc(key)
@@ -243,8 +264,10 @@ def _format_attrs(self):
243264
attrs.append(("freq", freq))
244265
return attrs
245266

246-
@Appender(Index._summary.__doc__)
247267
def _summary(self, name=None) -> str:
268+
"""
269+
Return a summary of the Index.
270+
"""
248271
result = super()._summary(name=name)
249272
if self.freq:
250273
result += f"\nFreq: {self.freqstr}"
@@ -405,8 +428,20 @@ def shift(self, periods: int = 1, freq=None) -> Self:
405428

406429
# --------------------------------------------------------------------
407430

408-
@doc(Index._maybe_cast_listlike_indexer)
409431
def _maybe_cast_listlike_indexer(self, keyarr):
432+
"""
433+
Ensure that the list-like indexer is of the correct type.
434+
435+
Parameters
436+
----------
437+
keyarr : list-like
438+
The list-like indexer to check.
439+
440+
Returns
441+
-------
442+
list-like
443+
The list-like indexer of the correct type.
444+
"""
410445
try:
411446
res = self._data._validate_listlike(keyarr, allow_object=True)
412447
except (ValueError, TypeError):
@@ -497,8 +532,33 @@ def values(self) -> np.ndarray:
497532
data.flags.writeable = False
498533
return data
499534

500-
@doc(DatetimeIndexOpsMixin.shift)
501535
def shift(self, periods: int = 1, freq=None) -> Self:
536+
"""
537+
Shift index by desired number of time frequency increments.
538+
539+
This method is for shifting the values of datetime-like indexes
540+
by a specified time increment a given number of times.
541+
542+
Parameters
543+
----------
544+
periods : int, default 1
545+
Number of periods (or increments) to shift by,
546+
can be positive or negative.
547+
freq : pandas.DateOffset, pandas.Timedelta or string, optional
548+
Frequency increment to shift by.
549+
If None, the index is shifted by its own `freq` attribute.
550+
Offset aliases are valid strings, e.g., 'D', 'W', 'M' etc.
551+
552+
Returns
553+
-------
554+
pandas.DatetimeIndex
555+
Shifted index.
556+
557+
See Also
558+
--------
559+
Index.shift : Shift values of Index.
560+
PeriodIndex.shift : Shift values of PeriodIndex.
561+
"""
502562
if freq is not None and freq != self.freq:
503563
if isinstance(freq, str):
504564
freq = to_offset(freq)
@@ -524,8 +584,14 @@ def shift(self, periods: int = 1, freq=None) -> Self:
524584
return type(self)._simple_new(result, name=self.name)
525585

526586
@cache_readonly
527-
@doc(DatetimeLikeArrayMixin.inferred_freq)
528587
def inferred_freq(self) -> str | None:
588+
"""
589+
Tries to return a string representing a frequency guess generated by infer_freq.
590+
591+
Returns
592+
-------
593+
str or None
594+
"""
529595
return self._data.inferred_freq
530596

531597
# --------------------------------------------------------------------
@@ -816,14 +882,41 @@ def _get_insert_freq(self, loc: int, item):
816882
freq = self.freq
817883
return freq
818884

819-
@doc(NDArrayBackedExtensionIndex.delete)
820885
def delete(self, loc) -> Self:
886+
"""
887+
Make new Index with passed location(-s) deleted.
888+
889+
Parameters
890+
----------
891+
loc : int or list of int
892+
Location of item(-s) to delete. Use a list of locations to delete
893+
multiple items. If a slice is provided, the step must be 1.
894+
895+
Returns
896+
-------
897+
Index
898+
Will be same type as self, except for RangeIndex.
899+
"""
821900
result = super().delete(loc)
822901
result._data._freq = self._get_delete_freq(loc)
823902
return result
824903

825-
@doc(NDArrayBackedExtensionIndex.insert)
826904
def insert(self, loc: int, item):
905+
"""
906+
Make new Index inserting new item at location.
907+
908+
Follows Python list.insert semantics for negative values. Only at
909+
least one of the index/item pairs must be specified.
910+
911+
Parameters
912+
----------
913+
loc : int
914+
item : object
915+
916+
Returns
917+
-------
918+
Index
919+
"""
827920
result = super().insert(loc, item)
828921
if isinstance(result, type(self)):
829922
# i.e. parent class method did not cast
@@ -833,7 +926,6 @@ def insert(self, loc: int, item):
833926
# --------------------------------------------------------------------
834927
# NDArray-Like Methods
835928

836-
@Appender(_index_shared_docs["take"] % _index_doc_kwargs)
837929
def take(
838930
self,
839931
indices,
@@ -842,6 +934,32 @@ def take(
842934
fill_value=None,
843935
**kwargs,
844936
) -> Self:
937+
"""
938+
Return a new Index of the values selected by the indices.
939+
940+
For internal compatibility with numpy arrays.
941+
942+
Parameters
943+
----------
944+
indices : array-like
945+
Indices to be taken.
946+
axis : int, optional
947+
The axis over which to select values. Always 0.
948+
allow_fill : bool, default True
949+
fill_value : scalar, default None
950+
If allow_fill=True and fill_value is not None, this value is used
951+
instead of raising if the index is out of bounds.
952+
953+
Returns
954+
-------
955+
Index
956+
An element of same type as self.
957+
958+
See Also
959+
--------
960+
numpy.ndarray.take : Return an array formed from the elements of a
961+
at the given indices.
962+
"""
845963
nv.validate_take((), kwargs)
846964
indices = np.asarray(indices, dtype=np.intp)
847965

0 commit comments

Comments
 (0)