Title: Break Up Dates And Times Into Multiple Features
Slug: break_up_dates_and_times_into_multiple_features
Summary: How to break up dates and times into multiple features for machine learning in Python.
Date: 2017-09-11 12:00
Category: Machine Learning
Tags: Preprocessing Dates And Times
Authors: Chris Albon
In [1]:
# Load library
import pandas as pd
In [2]:
# Create data frame
df = pd.DataFrame()
# Create five dates
df['date'] = pd.date_range('1/1/2001', periods=150, freq='W')
In [3]:
# Create features for year, month, day, hour, and minute
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month
df['day'] = df['date'].dt.day
df['hour'] = df['date'].dt.hour
df['minute'] = df['date'].dt.minute
# Show three rows
df.head(3)
Out[3]: