Skip to content

Commit 78ded25

Browse files
DenniskamauTomAugspurger
authored andcommitted
DOC: update pandas.DatetimeIndex.to_period docstring(Nairobi) (#20131)
* Improvement to pandas.DatetimeIndex.period * Improvement to pandas.DatetimeIndex.period * Improvement to pandas.DatetimeIndex.period * Improvement to pandas.DatetimeIndex.period * Improvement to pandas.DatetimeIndex.period * correct PEP8 issues * Fixed Return type. Added reference to offsets. Added Raises. Added inferred example.
1 parent f599215 commit 78ded25

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

pandas/core/indexes/datetimes.py

+46-6
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,47 @@ def to_pydatetime(self):
10591059

10601060
def to_period(self, freq=None):
10611061
"""
1062-
Cast to PeriodIndex at a particular frequency
1062+
Cast to PeriodIndex at a particular frequency.
1063+
1064+
Converts DatetimeIndex to PeriodIndex.
1065+
1066+
Parameters
1067+
----------
1068+
freq : string or Offset, optional
1069+
One of pandas' :ref:`offset strings <timeseries.offset_aliases>`
1070+
or an Offset object. Will be inferred by default.
1071+
1072+
Returns
1073+
-------
1074+
PeriodIndex
1075+
1076+
Raises
1077+
------
1078+
ValueError
1079+
When converting a DatetimeIndex with non-regular values, so that a
1080+
frequency cannot be inferred.
1081+
1082+
Examples
1083+
--------
1084+
>>> df = pd.DataFrame({"y": [1,2,3]},
1085+
... index=pd.to_datetime(["2000-03-31 00:00:00",
1086+
... "2000-05-31 00:00:00",
1087+
... "2000-08-31 00:00:00"]))
1088+
>>> df.index.to_period("M")
1089+
PeriodIndex(['2000-03', '2000-05', '2000-08'],
1090+
dtype='period[M]', freq='M')
1091+
1092+
Infer the daily frequency
1093+
1094+
>>> idx = pd.date_range("2017-01-01", periods=2)
1095+
>>> idx.to_period()
1096+
PeriodIndex(['2017-01-01', '2017-01-02'],
1097+
dtype='period[D]', freq='D')
1098+
1099+
See also
1100+
--------
1101+
pandas.PeriodIndex: Immutable ndarray holding ordinal values
1102+
pandas.DatetimeIndex.to_pydatetime: Return DatetimeIndex as object
10631103
"""
10641104
from pandas.core.indexes.period import PeriodIndex
10651105

@@ -1148,17 +1188,17 @@ def union(self, other):
11481188

11491189
def to_perioddelta(self, freq):
11501190
"""
1151-
Calculates TimedeltaIndex of difference between index
1152-
values and index converted to PeriodIndex at specified
1153-
freq. Used for vectorized offsets
1191+
Calculate TimedeltaIndex of difference between index
1192+
values and index converted to periodIndex at specified
1193+
freq. Used for vectorized offsets
11541194
11551195
Parameters
11561196
----------
1157-
freq : Period frequency
1197+
freq: Period frequency
11581198
11591199
Returns
11601200
-------
1161-
y : TimedeltaIndex
1201+
y: TimedeltaIndex
11621202
"""
11631203
return to_timedelta(self.asi8 - self.to_period(freq)
11641204
.to_timestamp().asi8)

0 commit comments

Comments
 (0)