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

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

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.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

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.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

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.

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.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

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.