Preparation
Before any data manipulation can occur, one (1) new library will require installation.
- The Pandas library enables access to/from a DataFrame.
To install this library, navigate to an IDE terminal. At the command prompt ($
), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($
). Your terminal prompt may be different.
π‘ Note: The pytz
comes packaged with pandas and does not require installation. However, this library is needed for the tz_ localize()
and tz_convert()
methods to work.
$ pip install pandas
Hit the <Enter>
key on the keyboard to start the installation process.
If the installation was successful, a message displays in the terminal indicating the same.
Feel free to view the PyCharm installation guide for the required library.
Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.
import pandas as pd import pytz
DataFrame to_period()
The to_period()
method converts a DataFrame/Series from a DatetimeIndex
format to a PeriodIndex
format based on the selected frequency.
The syntax for this method is as follows:
DataFrame.to_period(freq=None, axis=0, copy=True)
Parameter | Description |
---|---|
freq | This parameter is an available frequency of the PeriodIndex method. |
axis | If zero (0) or index is selected, apply to each column. Default 0. If one (1) apply to each row. |
copy | If True , the data copies. By default, True . |
For these examples, we have a list containing datetimes
. These datetimes
convert to Monthly & Yearly formats.
Code βMonthly Format
idx = pd.to_datetime(['2022-01-15 08:17:00', '2022-01-15 08:23:00', '2022-01-15 08:47:00', '2022-01-15 09:01:00', '2022-01-15 09:28:00']) print(idx) result = idx.to_period('M') print(result)
- Line [1] converts a list of strings to a datetime format and saves it to
idx
. - Line [2] outputs the contents of
idx
to the terminal. - Line [3] converts the contents of
idx
to aPeriodIndex
Monthly format. The output saves toresult
. - Line [4] outputs the result to the terminal.
Output
idx DatetimeIndex(['2022-01-15 08:17:00', '2022-01-15 08:23:00', '2022-01-15 08:47:00', '2022-01-15 09:01:00', '2022-01-15 09:28:00'], dtype='datetime64[ns]', freq=None) result PeriodIndex(['2022-01', '2022-01', '2022-01', '2022-01', '2022-01'], dtype='period[M]')
Code βYearly Example
idx = pd.to_datetime(['2018-01-15 08:17:00', '2019-01-15 08:23:00', '2020-01-15 08:47:00', '2021-01-15 09:01:00', '2022-01-15 09:28:00']) print(idx) result = idx.to_period('Y') print(result)
- Line [1] converts a list of strings to a datetime format and saves it to
idx
. - Line [2] outputs the contents of
idx
to the terminal. - Line [3] converts the contents of
idx
to aPeriodIndex
Yearly format. The output saves toresult
. - Line [4] outputs the result to the terminal.
Output
idx DatetimeIndex(['2018-01-15 08:17:00', '2019-01-15 08:23:00', '2020-01-15 08:47:00', '2021-01-15 09:01:00', '2022-01-15 09:28:00'], dtype='datetime64[ns]', freq=None) result PeriodIndex(['2018', '2019', '2020', '2021', '2022'], dtype='period[A-DEC]')
Note: Definition of frequency period [A-DEC]:
- A: year-end
- DEC: year ends in December
More Pandas DataFrame Methods
Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:
Also, check out the full cheat sheet overview of all Pandas DataFrame methods.