How to split DateTime Data to create multiple feature in Python?

This recipe helps you split DateTime Data to create multiple feature in Python

Recipe Objective

Many a times in a dataset we find Date Time Stamps which is the combination of Date and Time written in a perticular format. For analysis we have to split the Data Time Stamp such that we can get different information seperately like Year, Month, Day, Hour, Minute and Seconds. This can be easily done by using pandas.

So this is the recipe on how we can split DateTime Data to create multiple feature in Python.

Get Closer To Your Dream of Becoming a Data Scientist with 70+ Solved End-to-End ML Projects


Step 1 - Import the library

import pandas as pd

We have imported only pandas which is requied for this split.

Step 2 - Setting up the Data

We have created an empty dataframe then we have created a column 'date'. By using date_range function we have created a dataset of date time stamp by passing the parameters of starting date, periods i.e number of stamps and frequency as weekly. df = pd.DataFrame() df['date'] = pd.date_range('1/6/2020 01:00:00', periods=6, freq='W') print(df)

 

Explore More Data Science and Machine Learning Projects for Practice. Fast-Track Your Career Transition with ProjectPro

Step 3 - Creating features of Date Time Stamps

We have to split the date time stamp into few features like Year, Month, Day, Hour, Minute and Seconds. For each of the feature split there are pre defined functions.

    • Creating the year column form date time stamp.

df['year'] = df['date'].dt.year

    • Creating the month column form date time stamp.

df['month'] = df['date'].dt.month

    • Creating the day column form date time stamp.

df['day'] = df['date'].dt.day

    • Creating the hour column form date time stamp.

df['hour'] = df['date'].dt.hour

    • Creating the hour column form date time stamp.

df['hour'] = df['date'].dt.hour

Now we are printing the final dataset and the output comes as:

                 date
0 2020-01-12 01:00:00
1 2020-01-19 01:00:00
2 2020-01-26 01:00:00
3 2020-02-02 01:00:00
4 2020-02-09 01:00:00
5 2020-02-16 01:00:00

                 date  year  month  day  hour  minute
0 2020-01-12 01:00:00  2020      1   12     1       0
1 2020-01-19 01:00:00  2020      1   19     1       0
2 2020-01-26 01:00:00  2020      1   26     1       0
3 2020-02-02 01:00:00  2020      2    2     1       0
4 2020-02-09 01:00:00  2020      2    9     1       0
5 2020-02-16 01:00:00  2020      2   16     1       0

Join Millions of Satisfied Developers and Enterprises to Maximize Your Productivity and ROI with ProjectPro - Read ProjectPro Reviews Now!

Download Materials

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.