Skip to content

Commit

Permalink
Fix errors in docstring for pandas.PeriodIndex (#57247)
Browse files Browse the repository at this point in the history
* Fix docstring errors

* Update
  • Loading branch information
yuanx749 authored Feb 4, 2024
1 parent ba76f73 commit a28de76
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 30 deletions.
1 change: 0 additions & 1 deletion ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.TimedeltaIndex.round\
pandas.TimedeltaIndex.floor\
pandas.TimedeltaIndex.ceil\
pandas.PeriodIndex\
pandas.PeriodIndex.strftime\
pandas.Series.rename_axis\
pandas.Series.dt.to_period\
Expand Down
90 changes: 61 additions & 29 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,39 +94,24 @@ class PeriodIndex(DatetimeIndexOpsMixin):
----------
data : array-like (1d int np.ndarray or PeriodArray), optional
Optional period-like data to construct index with.
copy : bool
Make a copy of input ndarray.
freq : str or period object, optional
One of pandas period strings or corresponding objects.
year : int, array, or Series, default None
.. deprecated:: 2.2.0
Use PeriodIndex.from_fields instead.
month : int, array, or Series, default None
.. deprecated:: 2.2.0
Use PeriodIndex.from_fields instead.
quarter : int, array, or Series, default None
ordinal : array-like of int, optional
The period offsets from the proleptic Gregorian epoch.
.. deprecated:: 2.2.0
Use PeriodIndex.from_fields instead.
day : int, array, or Series, default None
.. deprecated:: 2.2.0
Use PeriodIndex.from_fields instead.
hour : int, array, or Series, default None
.. deprecated:: 2.2.0
Use PeriodIndex.from_fields instead.
minute : int, array, or Series, default None
.. deprecated:: 2.2.0
Use PeriodIndex.from_fields instead.
second : int, array, or Series, default None
Use PeriodIndex.from_ordinals instead.
freq : str or period object, optional
One of pandas period strings or corresponding objects.
dtype : str or PeriodDtype, default None
A dtype from which to extract a freq.
copy : bool
Make a copy of input ndarray.
name : str, default None
Name of the resulting PeriodIndex.
**fields : optional
Date fields such as year, month, etc.
.. deprecated:: 2.2.0
Use PeriodIndex.from_fields instead.
dtype : str or PeriodDtype, default None
Attributes
----------
Expand Down Expand Up @@ -171,7 +156,7 @@ class PeriodIndex(DatetimeIndexOpsMixin):
Examples
--------
>>> idx = pd.PeriodIndex.from_fields(year=[2000, 2002], quarter=[1, 3])
>>> idx = pd.PeriodIndex(data=['2000Q1', '2002Q3'], freq='Q')
>>> idx
PeriodIndex(['2000Q1', '2002Q3'], dtype='period[Q-DEC]')
"""
Expand Down Expand Up @@ -331,6 +316,31 @@ def from_fields(
second=None,
freq=None,
) -> Self:
"""
Construct a PeriodIndex from fields (year, month, day, etc.).
Parameters
----------
year : int, array, or Series, default None
quarter : int, array, or Series, default None
month : int, array, or Series, default None
day : int, array, or Series, default None
hour : int, array, or Series, default None
minute : int, array, or Series, default None
second : int, array, or Series, default None
freq : str or period object, optional
One of pandas period strings or corresponding objects.
Returns
-------
PeriodIndex
Examples
--------
>>> idx = pd.PeriodIndex.from_fields(year=[2000, 2002], quarter=[1, 3])
>>> idx
PeriodIndex(['2000Q1', '2002Q3'], dtype='period[Q-DEC]')
"""
fields = {
"year": year,
"quarter": quarter,
Expand All @@ -346,6 +356,28 @@ def from_fields(

@classmethod
def from_ordinals(cls, ordinals, *, freq, name=None) -> Self:
"""
Construct a PeriodIndex from ordinals.
Parameters
----------
ordinals : array-like of int
The period offsets from the proleptic Gregorian epoch.
freq : str or period object
One of pandas period strings or corresponding objects.
name : str, default None
Name of the resulting PeriodIndex.
Returns
-------
PeriodIndex
Examples
--------
>>> idx = pd.PeriodIndex.from_ordinals([-1, 0, 1], freq='Q')
>>> idx
PeriodIndex(['1969Q4', '1970Q1', '1970Q2'], dtype='period[Q-DEC]')
"""
ordinals = np.asarray(ordinals, dtype=np.int64)
dtype = PeriodDtype(freq)
data = PeriodArray._simple_new(ordinals, dtype=dtype)
Expand Down

0 comments on commit a28de76

Please sign in to comment.