Skip to content

Commit a2c5f9a

Browse files
DOC: Fixing EX01 - Added examples (#53796)
* examples TimedeltaArray, Period.asfreq * Examples for categoricals * Exampl Categorical.codes and .__array__, edited code_checks * Corrected TimedeltaArray and wording to categorical * remove multiline --------- Co-authored-by: MarcoGorelli <[email protected]>
1 parent af83376 commit a2c5f9a

File tree

6 files changed

+89
-11
lines changed

6 files changed

+89
-11
lines changed

ci/code_checks.sh

-11
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
105105
pandas.errors.UnsupportedFunctionCall \
106106
pandas.test \
107107
pandas.NaT \
108-
pandas.arrays.TimedeltaArray \
109-
pandas.Period.asfreq \
110-
pandas.Period.now \
111-
pandas.arrays.PeriodArray \
112-
pandas.CategoricalDtype.categories \
113-
pandas.CategoricalDtype.ordered \
114-
pandas.Categorical.dtype \
115-
pandas.Categorical.categories \
116-
pandas.Categorical.ordered \
117-
pandas.Categorical.codes \
118-
pandas.Categorical.__array__ \
119108
pandas.SparseDtype \
120109
pandas.DatetimeTZDtype.unit \
121110
pandas.DatetimeTZDtype.tz \

pandas/_libs/tslibs/period.pyx

+11
Original file line numberDiff line numberDiff line change
@@ -1925,6 +1925,12 @@ cdef class _Period(PeriodMixin):
19251925
Returns
19261926
-------
19271927
resampled : Period
1928+
1929+
Examples
1930+
--------
1931+
>>> period = pd.Period('2023-1-1', freq='D')
1932+
>>> period.asfreq('H')
1933+
Period('2023-01-01 23:00', 'H')
19281934
"""
19291935
freq = self._maybe_convert_freq(freq)
19301936
how = validate_end_alias(how)
@@ -2460,6 +2466,11 @@ cdef class _Period(PeriodMixin):
24602466
----------
24612467
freq : str, BaseOffset
24622468
Frequency to use for the returned period.
2469+
2470+
Examples
2471+
--------
2472+
>>> pd.Period.now('H') # doctest: +SKIP
2473+
Period('2023-06-12 11:00', 'H')
24632474
"""
24642475
return Period(datetime.now(), freq=freq)
24652476

pandas/core/arrays/categorical.py

+47
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,15 @@ def __init__(
481481
def dtype(self) -> CategoricalDtype:
482482
"""
483483
The :class:`~pandas.api.types.CategoricalDtype` for this instance.
484+
485+
Examples
486+
--------
487+
>>> cat = pd.Categorical(['a', 'b'], ordered=True)
488+
>>> cat
489+
['a', 'b']
490+
Categories (2, object): ['a' < 'b']
491+
>>> cat.dtype
492+
CategoricalDtype(categories=['a', 'b'], ordered=True, categories_dtype=object)
484493
"""
485494
return self._dtype
486495

@@ -751,6 +760,9 @@ def categories(self) -> Index:
751760
752761
Examples
753762
--------
763+
764+
For Series:
765+
754766
>>> ser = pd.Series(["a", "b", "c", "a"], dtype="category")
755767
>>> ser.cat.categories
756768
Index(['a', 'b', 'c'], dtype='object')
@@ -759,6 +771,12 @@ def categories(self) -> Index:
759771
>>> ser = pd.Series(raw_cat)
760772
>>> ser.cat.categories
761773
Index(['b', 'c', 'd'], dtype='object')
774+
775+
For Categorical:
776+
777+
>>> cat = pd.Categorical(['a', 'b'], ordered=True)
778+
>>> cat.categories
779+
Index(['a', 'b'], dtype='object')
762780
"""
763781
return self.dtype.categories
764782

@@ -769,6 +787,9 @@ def ordered(self) -> Ordered:
769787
770788
Examples
771789
--------
790+
791+
For Series:
792+
772793
>>> ser = pd.Series(["a", "b", "c", "a"], dtype="category")
773794
>>> ser.cat.ordered
774795
False
@@ -777,6 +798,16 @@ def ordered(self) -> Ordered:
777798
>>> ser = pd.Series(raw_cat)
778799
>>> ser.cat.ordered
779800
True
801+
802+
For Categorical:
803+
804+
>>> cat = pd.Categorical(['a', 'b'], ordered=True)
805+
>>> cat.ordered
806+
True
807+
808+
>>> cat = pd.Categorical(['a', 'b'], ordered=False)
809+
>>> cat.ordered
810+
False
780811
"""
781812
return self.dtype.ordered
782813

@@ -795,6 +826,12 @@ def codes(self) -> np.ndarray:
795826
-------
796827
ndarray[int]
797828
A non-writable view of the `codes` array.
829+
830+
Examples
831+
--------
832+
>>> cat = pd.Categorical(['a', 'b'], ordered=True)
833+
>>> cat.codes
834+
array([0, 1], dtype=int8)
798835
"""
799836
v = self._codes.view()
800837
v.flags.writeable = False
@@ -1492,6 +1529,16 @@ def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
14921529
A numpy array of either the specified dtype or,
14931530
if dtype==None (default), the same dtype as
14941531
categorical.categories.dtype.
1532+
1533+
Examples
1534+
--------
1535+
1536+
>>> cat = pd.Categorical(['a', 'b'], ordered=True)
1537+
1538+
The following calls ``cat.__array__``
1539+
1540+
>>> np.asarray(cat)
1541+
array(['a', 'b'], dtype=object)
14951542
"""
14961543
ret = take_nd(self.categories._values, self._codes)
14971544
if dtype and np.dtype(dtype) != self.categories.dtype:

pandas/core/arrays/period.py

+8
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ class PeriodArray(dtl.DatelikeOps, libperiod.PeriodMixin): # type: ignore[misc]
163163
164164
The `freq` indicates the span covered by each element of the array.
165165
All elements in the PeriodArray have the same `freq`.
166+
167+
Examples
168+
--------
169+
>>> pd.arrays.PeriodArray(pd.PeriodIndex(['2023-01-01',
170+
... '2023-01-02'], freq='D'))
171+
<PeriodArray>
172+
['2023-01-01', '2023-01-02']
173+
Length: 2, dtype: period[D]
166174
"""
167175

168176
# array priority higher than numpy scalars

pandas/core/arrays/timedeltas.py

+7
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ class TimedeltaArray(dtl.TimelikeOps):
132132
Methods
133133
-------
134134
None
135+
136+
Examples
137+
--------
138+
>>> pd.arrays.TimedeltaArray(pd.TimedeltaIndex(['1H', '2H']))
139+
<TimedeltaArray>
140+
['0 days 01:00:00', '0 days 02:00:00']
141+
Length: 2, dtype: timedelta64[ns]
135142
"""
136143

137144
_typ = "timedeltaarray"

pandas/core/dtypes/dtypes.py

+16
Original file line numberDiff line numberDiff line change
@@ -610,13 +610,29 @@ def update_dtype(self, dtype: str_type | CategoricalDtype) -> CategoricalDtype:
610610
def categories(self) -> Index:
611611
"""
612612
An ``Index`` containing the unique categories allowed.
613+
614+
Examples
615+
--------
616+
>>> cat_type = pd.CategoricalDtype(categories=['a', 'b'], ordered=True)
617+
>>> cat_type.categories
618+
Index(['a', 'b'], dtype='object')
613619
"""
614620
return self._categories
615621

616622
@property
617623
def ordered(self) -> Ordered:
618624
"""
619625
Whether the categories have an ordered relationship.
626+
627+
Examples
628+
--------
629+
>>> cat_type = pd.CategoricalDtype(categories=['a', 'b'], ordered=True)
630+
>>> cat_type.ordered
631+
True
632+
633+
>>> cat_type = pd.CategoricalDtype(categories=['a', 'b'], ordered=False)
634+
>>> cat_type.ordered
635+
False
620636
"""
621637
return self._ordered
622638

0 commit comments

Comments
 (0)