pandas.Series.dt.to_period#
- Series.dt.to_period(freq=None)[source]#
Cast the datetimes to periods at a particular frequency.
Each datetime is mapped to the period of the given frequency that contains it.
- Parameters:
- freqstr, optional
One of pandas’ period aliases. Will be inferred by default.
- Returns:
- Series or PeriodArray
The values as periods of the given frequency.
- Raises:
- ValueError
If
freqis None and a frequency cannot be inferred from the values.
See also
DatetimeIndex.to_periodCast a DatetimeIndex to a PeriodIndex.
Series.dt.start_timeGet the start of each period as a datetime.
Notes
A multiplied
freqdoes not bin timestamps: with"2h", each timestamp maps to the 2-hour period starting at its own hour. For aligned 2-hour bins, useser.dt.floor("2h").dt.to_period("2h").Examples
>>> ser = pd.Series(pd.to_datetime(["2000-03-31", "2000-05-31", "2000-08-31"])) >>> ser.dt.to_period("M") 0 2000-03 1 2000-05 2 2000-08 dtype: period[M]
Infer the daily frequency
>>> ser = pd.Series(pd.date_range("2017-01-01", periods=3)) >>> ser.dt.to_period() 0 2017-01-01 1 2017-01-02 2 2017-01-03 dtype: period[D]