![]() |
VOOZH | about |
The Pandas dt.to_period() method converts the underlying data of the given Series object to PeriodArray/Index at a particular frequency.
It is used to convert a DateTime series to a period series with a specific frequency, such as daily, monthly, quarterly, or yearly periods.
Output:
👁 DateTime converted to period format
Syntax: Series.dt.to_period(freq)
Parameter
- freq : string or Offset, optional
Returns: The original Series cast to PeriodArray/Index at the specified frequency
To convert a Pandas DateTime Series to a Period series we use the dt.to_period() method of the Pandas library in Python.
Let us understand it better with an example:
Use the dt.to_period() function to cast the underlying data of the given series object to Index at two-year frequency.
Output:
Now we can use dt.to_period() method to convert it to period format
Output:
👁 converted datetime series in period format
As we can see in the output, the Series.dt.to_period() function has successfully cast the data to the target frequency.