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

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.