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

0

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

Relevant Projects

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

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.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

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 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.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

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.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.