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

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

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

End-to-End Snowflake Healthcare Analytics Project on AWS-1
In this Snowflake Healthcare Analytics Project, you will leverage Snowflake on AWS to predict patient length of stay (LOS) in hospitals. The prediction of LOS can help in efficient resource allocation, lower the risk of staff/visitor infections, and improve overall hospital functioning.

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.