How to select DateTime within a range in Python?

This recipe helps you select DateTime within a range in Python

Recipe Objective

Have you tried to select a range of DateTime in dataset with many DateTime entry?

So this is the recipe on we can select DateTime within a range in Python.

Step 1 - Import the library

import pandas as pd

We have imported pandas which is needed.

Step 2 - Setting up the Data

We have created a DataFrame and created a column of datetime with 100000 entry starting from a fixed date and differ on hourly basis. We have set that feature as index. df = pd.DataFrame() df["date"] = pd.date_range("13/12/2020", periods=100000, freq="H") df = df.set_index(df["date"])

Step 3 - Making Time Series

We have selected all the entry between "2021-1-1 01:00:00" and "2021-1-1 07:00:00" by loc function. print(df.loc["2021-1-1 01:00:00":"2021-1-1 07:00:00"]) So the output comes as

                                   date
date                                   
2021-01-01 01:00:00 2021-01-01 01:00:00
2021-01-01 02:00:00 2021-01-01 02:00:00
2021-01-01 03:00:00 2021-01-01 03:00:00
2021-01-01 04:00:00 2021-01-01 04:00:00
2021-01-01 05:00:00 2021-01-01 05:00:00
2021-01-01 06:00:00 2021-01-01 06:00:00
2021-01-01 07:00:00 2021-01-01 07:00:00
2021-01-01 08:00:00 2021-01-01 08:00:00
2021-01-01 09:00:00 2021-01-01 09:00:00
2021-01-01 10:00:00 2021-01-01 10:00:00
2021-01-01 11:00:00 2021-01-01 11:00:00
2021-01-01 12:00:00 2021-01-01 12:00:00
2021-01-01 13:00:00 2021-01-01 13:00:00
2021-01-01 14:00:00 2021-01-01 14:00:00
2021-01-01 15:00:00 2021-01-01 15:00:00
2021-01-01 16:00:00 2021-01-01 16:00:00
2021-01-01 17:00:00 2021-01-01 17:00:00
2021-01-01 18:00:00 2021-01-01 18:00:00
2021-01-01 19:00:00 2021-01-01 19:00:00
2021-01-01 20:00:00 2021-01-01 20:00:00
2021-01-01 21:00:00 2021-01-01 21:00:00
2021-01-01 22:00:00 2021-01-01 22:00:00
2021-01-01 23:00:00 2021-01-01 23:00:00
2021-01-02 00:00:00 2021-01-02 00:00:00
2021-01-02 01:00:00 2021-01-02 01:00:00
2021-01-02 02:00:00 2021-01-02 02:00:00
2021-01-02 03:00:00 2021-01-02 03:00:00
2021-01-02 04:00:00 2021-01-02 04:00:00
2021-01-02 05:00:00 2021-01-02 05:00:00
2021-01-02 06:00:00 2021-01-02 06:00:00
2021-01-02 07:00:00 2021-01-02 07:00:00

Download Materials

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.

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