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

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

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.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

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.

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

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.

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.

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