How to split DateTime Data to create multiple feature in Python?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

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

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.

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)

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

Download Materials

Relevant Projects

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

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.

Machine learning for Retail Price Recommendation with Python
Use the Mercari Dataset with dynamic pricing to build a price recommendation algorithm using machine learning in Python to automatically suggest the right product prices.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Time Series Analysis Project in R on Stock Market forecasting
In this time series project, you will build a model to predict the stock prices and identify the best time series forecasting model that gives reliable and authentic results for decision making.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.