How to select DateTime within a range in Python?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to select DateTime within a range in Python?

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

Relevant Projects

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

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.

House Price Prediction Project using Machine Learning
Use the Zillow dataset to follow a test-driven approach and build a regression machine learning model to predict the price of the house based on other variables.

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

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

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.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

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.