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

0

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

Relevant Projects

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.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

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.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

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

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.