From 914b0e8896367e19838f57d960617eeb494b3f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Tue, 30 May 2023 19:25:35 +0200 Subject: [PATCH 1/2] Added some PeriodIndex examples --- pandas/core/arrays/period.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 2b805fb5f6c4b..fac3ca7aa6b93 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -468,6 +468,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.day_of_week + Index([6, 0, 1], dtype='int64') """, ) dayofweek = day_of_week @@ -476,6 +482,18 @@ def __arrow_array__(self, type=None): "day_of_year", """ The ordinal day of the year. + + Examples + -------- + >>> idx = pd.PeriodIndex(["2023-01-10", "2023-02-01", "2023-03-01"], freq="D") + >>> idx.dayofyear + Index([10, 32, 60], dtype='int64') + + >>> idx = pd.PeriodIndex(["2023", "2024", "2025"], freq="Y") + >>> idx + PeriodIndex(['2023', '2024', '2025'], dtype='period[A-DEC]') + >>> idx.dayofyear + Index([365, 366, 365], dtype='int64') """, ) quarter = _field_accessor( From a1eaa521f5bcb16a5bb8fd04b7638369204bbb93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Wed, 31 May 2023 10:10:26 +0200 Subject: [PATCH 2/2] Added PeriodIndex examples --- ci/code_checks.sh | 10 ---------- pandas/_libs/tslibs/period.pyx | 9 +++++++++ pandas/core/arrays/datetimelike.py | 8 ++++++++ pandas/core/arrays/period.py | 14 ++++++++++++++ 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index f44b2d15b719a..5602cbe2a11ed 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -274,16 +274,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then pandas.TimedeltaIndex.as_unit \ pandas.TimedeltaIndex.to_pytimedelta \ pandas.TimedeltaIndex.mean \ - pandas.PeriodIndex.day \ - pandas.PeriodIndex.dayofweek \ - pandas.PeriodIndex.day_of_week \ - pandas.PeriodIndex.dayofyear \ - pandas.PeriodIndex.day_of_year \ - pandas.PeriodIndex.days_in_month \ - pandas.PeriodIndex.daysinmonth \ - pandas.PeriodIndex.end_time \ - pandas.PeriodIndex.freqstr \ - pandas.PeriodIndex.hour \ pandas.PeriodIndex.is_leap_year \ pandas.PeriodIndex.minute \ pandas.PeriodIndex.month \ diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 2047bc7970558..54bf041d59264 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -1684,6 +1684,15 @@ cdef class PeriodMixin: 1 2020-02-29 23:59:59.999999999 2 2020-03-31 23:59:59.999999999 dtype: datetime64[ns] + + For PeriodIndex: + + >>> idx = pd.PeriodIndex(["2023-01", "2023-02", "2023-03"], freq="M") + >>> idx.end_time + DatetimeIndex(['2023-01-31 23:59:59.999999999', + '2023-02-28 23:59:59.999999999', + '2023-03-31 23:59:59.999999999'], + dtype='datetime64[ns]', freq=None) """ return self.to_timestamp(how="end") diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index ee5dff24c8bed..89e91ecd1a0ff 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -829,6 +829,8 @@ def freqstr(self) -> str | None: Examples -------- + For DatetimeIndex: + >>> idx = pd.DatetimeIndex(["1/1/2020 10:00:00+00:00"], freq="D") >>> idx.freqstr 'D' @@ -839,6 +841,12 @@ def freqstr(self) -> str | None: ... freq="infer") >>> idx.freqstr '2D' + + For PeriodIndex: + + >>> idx = pd.PeriodIndex(["2023-1", "2023-2", "2023-3"], freq="M") + >>> idx.freqstr + 'M' """ if self.freq is None: return None diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index fac3ca7aa6b93..28869f9615999 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -443,6 +443,12 @@ def __arrow_array__(self, type=None): "hour", """ The hour of the period. + + Examples + -------- + >>> idx = pd.PeriodIndex(["2023-01-01 10:00", "2023-01-01 11:00"], freq='H') + >>> idx.hour + Index([10, 11], dtype='int64') """, ) minute = _field_accessor( @@ -510,6 +516,8 @@ def __arrow_array__(self, type=None): Examples -------- + For Series: + >>> period = pd.period_range('2020-1-1 00:00', '2020-3-1 00:00', freq='M') >>> s = pd.Series(period) >>> s @@ -522,6 +530,12 @@ def __arrow_array__(self, type=None): 1 29 2 31 dtype: int64 + + For PeriodIndex: + + >>> idx = pd.PeriodIndex(["2023-01", "2023-02", "2023-03"], freq="M") + >>> idx.days_in_month # It can be also entered as `daysinmonth` + Index([31, 28, 31], dtype='int64') """, ) daysinmonth = days_in_month