Skip to content

Commit

Permalink
Fix docstrings for pandas.Period.month (#59517)
Browse files Browse the repository at this point in the history
* Fix docstrings for pandas.Period.month

* Fix docstrings for pandas.Period.month indentation

* Fix docstrings for pandas.Period.month

* Fix docstrings for pandas.Period.asfreq SA01

* Fix docstrings for pandas.Period.asfreq

* Fix docstrings ci error cython-lint

* Fix docstrings pre-commit.cierror

* Fix docstring validation error

* Fix docstring validation errors
  • Loading branch information
uditbaliyan authored Aug 15, 2024
1 parent 2bd6954 commit 60e36d9
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 7 deletions.
2 changes: 0 additions & 2 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
-i "pandas.MultiIndex.to_frame RT03" \
-i "pandas.NA SA01" \
-i "pandas.NaT SA01" \
-i "pandas.Period.asfreq SA01" \
-i "pandas.Period.freq GL08" \
-i "pandas.Period.freqstr SA01" \
-i "pandas.Period.month SA01" \
-i "pandas.Period.ordinal GL08" \
-i "pandas.Period.strftime PR01,SA01" \
-i "pandas.Period.to_timestamp SA01" \
Expand Down
82 changes: 77 additions & 5 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1913,20 +1913,58 @@ cdef class _Period(PeriodMixin):
Parameters
----------
freq : str, BaseOffset
The desired frequency. If passing a `str`, it needs to be a
valid :ref:`period alias <timeseries.period_aliases>`.
The target frequency to convert the Period object to.
If a string is provided,
it must be a valid :ref:`period alias <timeseries.period_aliases>`.

how : {'E', 'S', 'end', 'start'}, default 'end'
Start or end of the timespan.
Specifies whether to align the period to the start or end of the interval:
- 'E' or 'end': Align to the end of the interval.
- 'S' or 'start': Align to the start of the interval.

Returns
-------
resampled : Period
Period : Period object with the specified frequency, aligned to the parameter.

See Also
--------
Period.end_time : Return the end Timestamp.
Period.start_time : Return the start Timestamp.
Period.dayofyear : Return the day of the year.
Period.dayofweek : Return the day of the week.

Examples
--------
>>> period = pd.Period('2023-1-1', freq='D')
Convert a daily period to an hourly period, aligning to the end of the day:

>>> period = pd.Period('2023-01-01', freq='D')
>>> period.asfreq('h')
Period('2023-01-01 23:00', 'h')

Convert a monthly period to a daily period, aligning to the start of the month:

>>> period = pd.Period('2023-01', freq='M')
>>> period.asfreq('D', how='start')
Period('2023-01-01', 'D')

Convert a yearly period to a monthly period, aligning to the last month:

>>> period = pd.Period('2023', freq='Y')
>>> period.asfreq('M', how='end')
Period('2023-12', 'M')

Convert a monthly period to an hourly period,
aligning to the first day of the month:

>>> period = pd.Period('2023-01', freq='M')
>>> period.asfreq('h', how='start')
Period('2023-01-01 00:00', 'H')

Convert a weekly period to a daily period, aligning to the last day of the week:

>>> period = pd.Period('2023-08-01', freq='W')
>>> period.asfreq('D', how='end')
Period('2023-08-04', 'D')
"""
freq = self._maybe_convert_freq(freq)
how = validate_end_alias(how)
Expand Down Expand Up @@ -2014,11 +2052,45 @@ cdef class _Period(PeriodMixin):
"""
Return the month this Period falls on.

Returns
-------
int

See Also
--------
period.week : Get the week of the year on the given Period.
Period.year : Return the year this Period falls on.
Period.day : Return the day of the month this Period falls on.

Notes
-----
The month is based on the `ordinal` and `base` attributes of the Period.

Examples
--------
Create a Period object for January 2022 and get the month:

>>> period = pd.Period('2022-01', 'M')
>>> period.month
1

Period object with no specified frequency, resulting in a default frequency:

>>> period = pd.Period('2022', 'Y')
>>> period.month
12

Create a Period object with a specified frequency but an incomplete date string:

>>> period = pd.Period('2022', 'M')
>>> period.month
1

Handle a case where the Period object is empty, which results in `NaN`:

>>> period = pd.Period('nan', 'M')
>>> period.month
nan
"""
base = self._dtype._dtype_code
return pmonth(self.ordinal, base)
Expand Down

0 comments on commit 60e36d9

Please sign in to comment.