@@ -1059,7 +1059,47 @@ def to_pydatetime(self):
1059
1059
1060
1060
def to_period (self , freq = None ):
1061
1061
"""
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
1063
1103
"""
1064
1104
from pandas .core .indexes .period import PeriodIndex
1065
1105
@@ -1148,17 +1188,17 @@ def union(self, other):
1148
1188
1149
1189
def to_perioddelta (self , freq ):
1150
1190
"""
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
1154
1194
1155
1195
Parameters
1156
1196
----------
1157
- freq : Period frequency
1197
+ freq: Period frequency
1158
1198
1159
1199
Returns
1160
1200
-------
1161
- y : TimedeltaIndex
1201
+ y: TimedeltaIndex
1162
1202
"""
1163
1203
return to_timedelta (self .asi8 - self .to_period (freq )
1164
1204
.to_timestamp ().asi8 )
0 commit comments