Skip to content

Commit a7eeca3

Browse files
authored
DOC: Fixing EX01 - Added examples (#53468)
More PeriodIndex examples
1 parent 193427b commit a7eeca3

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

ci/code_checks.sh

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -284,16 +284,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
284284
pandas.PeriodIndex.end_time \
285285
pandas.PeriodIndex.freqstr \
286286
pandas.PeriodIndex.hour \
287-
pandas.PeriodIndex.is_leap_year \
288-
pandas.PeriodIndex.minute \
289-
pandas.PeriodIndex.month \
290-
pandas.PeriodIndex.quarter \
291-
pandas.PeriodIndex.second \
292-
pandas.PeriodIndex.week \
293-
pandas.PeriodIndex.weekday \
294-
pandas.PeriodIndex.weekofyear \
295-
pandas.PeriodIndex.year \
296-
pandas.PeriodIndex.to_timestamp \
297287
pandas.core.window.rolling.Rolling.max \
298288
pandas.core.window.rolling.Rolling.cov \
299289
pandas.core.window.rolling.Rolling.skew \

pandas/core/arrays/period.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,12 +419,24 @@ def __arrow_array__(self, type=None):
419419
"year",
420420
"""
421421
The year of the period.
422+
423+
Examples
424+
--------
425+
>>> idx = pd.PeriodIndex(["2023", "2024", "2025"], freq="Y")
426+
>>> idx.year
427+
Index([2023, 2024, 2025], dtype='int64')
422428
""",
423429
)
424430
month = _field_accessor(
425431
"month",
426432
"""
427433
The month as January=1, December=12.
434+
435+
Examples
436+
--------
437+
>>> idx = pd.PeriodIndex(["2023-01", "2023-02", "2023-03"], freq="M")
438+
>>> idx.month
439+
Index([1, 2, 3], dtype='int64')
428440
""",
429441
)
430442
day = _field_accessor(
@@ -449,25 +461,51 @@ def __arrow_array__(self, type=None):
449461
"minute",
450462
"""
451463
The minute of the period.
464+
465+
Examples
466+
--------
467+
>>> idx = pd.PeriodIndex(["2023-01-01 10:30:00",
468+
... "2023-01-01 11:50:00"], freq='min')
469+
>>> idx.minute
470+
Index([30, 50], dtype='int64')
452471
""",
453472
)
454473
second = _field_accessor(
455474
"second",
456475
"""
457476
The second of the period.
477+
478+
Examples
479+
--------
480+
>>> idx = pd.PeriodIndex(["2023-01-01 10:00:30",
481+
... "2023-01-01 10:00:31"], freq='s')
482+
>>> idx.second
483+
Index([30, 31], dtype='int64')
458484
""",
459485
)
460486
weekofyear = _field_accessor(
461487
"week",
462488
"""
463489
The week ordinal of the year.
490+
491+
Examples
492+
--------
493+
>>> idx = pd.PeriodIndex(["2023-01", "2023-02", "2023-03"], freq="M")
494+
>>> idx.week # It can be written `weekofyear`
495+
Index([5, 9, 13], dtype='int64')
464496
""",
465497
)
466498
week = weekofyear
467499
day_of_week = _field_accessor(
468500
"day_of_week",
469501
"""
470502
The day of the week with Monday=0, Sunday=6.
503+
504+
Examples
505+
--------
506+
>>> idx = pd.PeriodIndex(["2023-01-01", "2023-01-02", "2023-01-03"], freq="D")
507+
>>> idx.weekday
508+
Index([6, 0, 1], dtype='int64')
471509
""",
472510
)
473511
dayofweek = day_of_week
@@ -482,6 +520,12 @@ def __arrow_array__(self, type=None):
482520
"quarter",
483521
"""
484522
The quarter of the date.
523+
524+
Examples
525+
--------
526+
>>> idx = pd.PeriodIndex(["2023-01", "2023-02", "2023-03"], freq="M")
527+
>>> idx.quarter
528+
Index([1, 1, 1], dtype='int64')
485529
""",
486530
)
487531
qyear = _field_accessor("qyear")
@@ -512,6 +556,12 @@ def __arrow_array__(self, type=None):
512556
def is_leap_year(self) -> npt.NDArray[np.bool_]:
513557
"""
514558
Logical indicating if the date belongs to a leap year.
559+
560+
Examples
561+
--------
562+
>>> idx = pd.PeriodIndex(["2023", "2024", "2025"], freq="Y")
563+
>>> idx.is_leap_year
564+
array([False, True, False])
515565
"""
516566
return isleapyear_arr(np.asarray(self.year))
517567

@@ -530,6 +580,13 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray:
530580
Returns
531581
-------
532582
DatetimeArray/Index
583+
584+
Examples
585+
--------
586+
>>> idx = pd.PeriodIndex(["2023-01", "2023-02", "2023-03"], freq="M")
587+
>>> idx.to_timestamp()
588+
DatetimeIndex(['2023-01-01', '2023-02-01', '2023-03-01'],
589+
dtype='datetime64[ns]', freq='MS')
533590
"""
534591
from pandas.core.arrays import DatetimeArray
535592

0 commit comments

Comments
 (0)