How to create a timeindex from one datetime to another in pandas?

How to create a timeindex from one datetime to another in pandas?

How to create a timeindex from one datetime to another in pandas?

This recipe helps you create a timeindex from one datetime to another in pandas

Recipe Objective

Suppose you have a datetime data in form of index and you wish to convert it to datatime format and make it as index.

So this recipe is a short example on How to create a timeindex from one datetime to another in pandas. Let's get started.

Step 1 - Import the library

import pandas as pd

Let's pause and look at these imports. Pandas is generally used for performing mathematical operation and preferably over arrays.

Step 2 - Setup the Data

df = pd.DataFrame({'name':['john','mary','peter','jeff','bill'],'date_of_birth':['2000-01-01', '1999-12-20', '2000-11-01', '1995-02-25', '1992-06-30']}) print(df)

Here we have setup a dataframe of randome values with names and date_of_birth as columns.

Step 3 - Setting date time Index

df=df.set_index(pd.to_datetime(df['date_of_birth'])) df.drop('date_of_birth',axis=1,inplace=True) print(df)

We have first coverted date_of_birth to date time index and finally set it as index. Now, we drop that particular column to have a clearity.

Step 4 - Let's look at our dataset now

Once we run the above code snippet, we will see:

Scroll down the ipython file to visualize the final output.

We can see date_of_birth used as datatime Index.

Relevant Projects

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Build a Face Recognition System in Python using FaceNet
In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

Census Income Data Set Project - Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based on census data.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

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.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.