From 4ae3897a893d85c4f17621368bd8fec283676a59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Wed, 31 May 2023 16:13:50 +0200 Subject: [PATCH] More PeriodIndex examples --- ci/code_checks.sh | 10 ------- pandas/core/arrays/period.py | 57 ++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index 6fcb8222f0359..409cca4b69bfa 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -296,16 +296,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then pandas.PeriodIndex.end_time \ pandas.PeriodIndex.freqstr \ pandas.PeriodIndex.hour \ - pandas.PeriodIndex.is_leap_year \ - pandas.PeriodIndex.minute \ - pandas.PeriodIndex.month \ - pandas.PeriodIndex.quarter \ - pandas.PeriodIndex.second \ - pandas.PeriodIndex.week \ - pandas.PeriodIndex.weekday \ - pandas.PeriodIndex.weekofyear \ - pandas.PeriodIndex.year \ - pandas.PeriodIndex.to_timestamp \ pandas.core.window.rolling.Rolling.max \ pandas.core.window.rolling.Rolling.cov \ pandas.core.window.rolling.Rolling.skew \ diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 237ac99bcf45f..d50912090486a 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -419,12 +419,24 @@ def __arrow_array__(self, type=None): "year", """ The year of the period. + + Examples + -------- + >>> idx = pd.PeriodIndex(["2023", "2024", "2025"], freq="Y") + >>> idx.year + Index([2023, 2024, 2025], dtype='int64') """, ) month = _field_accessor( "month", """ The month as January=1, December=12. + + Examples + -------- + >>> idx = pd.PeriodIndex(["2023-01", "2023-02", "2023-03"], freq="M") + >>> idx.month + Index([1, 2, 3], dtype='int64') """, ) day = _field_accessor( @@ -443,18 +455,38 @@ def __arrow_array__(self, type=None): "minute", """ The minute of the period. + + Examples + -------- + >>> idx = pd.PeriodIndex(["2023-01-01 10:30:00", + ... "2023-01-01 11:50:00"], freq='min') + >>> idx.minute + Index([30, 50], dtype='int64') """, ) second = _field_accessor( "second", """ The second of the period. + + Examples + -------- + >>> idx = pd.PeriodIndex(["2023-01-01 10:00:30", + ... "2023-01-01 10:00:31"], freq='s') + >>> idx.second + Index([30, 31], dtype='int64') """, ) weekofyear = _field_accessor( "week", """ The week ordinal of the year. + + Examples + -------- + >>> idx = pd.PeriodIndex(["2023-01", "2023-02", "2023-03"], freq="M") + >>> idx.week # It can be written `weekofyear` + Index([5, 9, 13], dtype='int64') """, ) week = weekofyear @@ -462,6 +494,12 @@ def __arrow_array__(self, type=None): "day_of_week", """ The day of the week with Monday=0, Sunday=6. + + Examples + -------- + >>> idx = pd.PeriodIndex(["2023-01-01", "2023-01-02", "2023-01-03"], freq="D") + >>> idx.weekday + Index([6, 0, 1], dtype='int64') """, ) dayofweek = day_of_week @@ -476,6 +514,12 @@ def __arrow_array__(self, type=None): "quarter", """ The quarter of the date. + + Examples + -------- + >>> idx = pd.PeriodIndex(["2023-01", "2023-02", "2023-03"], freq="M") + >>> idx.quarter + Index([1, 1, 1], dtype='int64') """, ) qyear = _field_accessor("qyear") @@ -506,6 +550,12 @@ def __arrow_array__(self, type=None): def is_leap_year(self) -> npt.NDArray[np.bool_]: """ Logical indicating if the date belongs to a leap year. + + Examples + -------- + >>> idx = pd.PeriodIndex(["2023", "2024", "2025"], freq="Y") + >>> idx.is_leap_year + array([False, True, False]) """ return isleapyear_arr(np.asarray(self.year)) @@ -524,6 +574,13 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray: Returns ------- DatetimeArray/Index + + Examples + -------- + >>> idx = pd.PeriodIndex(["2023-01", "2023-02", "2023-03"], freq="M") + >>> idx.to_timestamp() + DatetimeIndex(['2023-01-01', '2023-02-01', '2023-03-01'], + dtype='datetime64[ns]', freq='MS') """ from pandas.core.arrays import DatetimeArray