def to_period(self, freq=None):
from pandas.core.arrays import PeriodArray<EOL>if self.tz is not None:<EOL><INDENT>warnings.warn("<STR_LIT>"<EOL>"<STR_LIT>", UserWarning)<EOL><DEDENT>if freq is None:<EOL><INDENT>freq = self.freqstr or self.inferred_freq<EOL>if freq is None:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>freq = get_period_alias(freq)<EOL><DEDENT>return PeriodArray._from_datetime64(self._data, freq, tz=self.tz)<EOL>
Cast to PeriodArray/Index at a particular frequency.

Converts DatetimeArray/Index to PeriodArray/Index.

Parameters
----------
freq : str or Offset, optional
    One of pandas' :ref:`offset strings <timeseries.offset_aliases>`
    or an Offset object. Will be inferred by default.

Returns
-------
PeriodArray/Index

Raises
------
ValueError
    When converting a DatetimeArray/Index with non-regular values,
    so that a frequency cannot be inferred.

See Also
--------
PeriodIndex: Immutable ndarray holding ordinal values.
DatetimeIndex.to_pydatetime: Return DatetimeIndex as object.

Examples
--------
>>> df = pd.DataFrame({"y": [1, 2, 3]},
...                   index=pd.to_datetime(["2000-03-31 00:00:00",
...                                         "2000-05-31 00:00:00",
...                                         "2000-08-31 00:00:00"]))
>>> df.index.to_period("M")
PeriodIndex(['2000-03', '2000-05', '2000-08'],
            dtype='period[M]', freq='M')

Infer the daily frequency

>>> idx = pd.date_range("2017-01-01", periods=2)
>>> idx.to_period()
PeriodIndex(['2017-01-01', '2017-01-02'],
            dtype='period[D]', freq='D')