How to do it...

To proceed with the recipe, let's import the libraries and create a toy dataset:

  1. Import pandas and NumPy:
import numpy as npimport pandas as pd
  1. Let's create 20 datetime values, beginning from 2019-03-05 at midnight followed by increments of 1 month. Then, let's capture the value range in a dataframe and display the top five rows:
rng_ = pd.date_range('2019-03-05', periods=20, freq='M')df = pd.DataFrame({'date': rng_}) df.head()

Note how the values increase by one month in the first five observations of the variable we created:

  1. Let's extract the year part of the date in a new column and display the top five rows of the ...

Get Python Feature Engineering Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.