Python - Create hourly/minutely time range using pandas

Learn, how can we create hourly/minutely time range using Python pandas?
Submitted by Pranit Sharma, on August 31, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Creating hourly/minutely time range using

In pandas, we have a feature to create a range of date which is pandas.date_range() method. Similar to date range, we have a method time_range() which is used to create a range of time.

The syntax of pandas.date_range() method is:

pandas.date_range(
    start=None, 
    end=None, 
    periods=None, 
    freq=None, 
    tz=None, 
    normalize=False, 
    name=None, 
    closed=NoDefault.no_default, 
    inclusive=None, 
    **kwargs
    )

The parameters of pandas.date_range() method are:

  • start: starting date
  • end: ending date
  • periods: Number of dates.
  • freq: frequency between two times.
  • normalize: modification to start/end.

The return value of pandas.date_range() method is: DatetimeIndex.

Let us understand with the help of an example,

Python program to create hourly/minutely time range using pandas

# Importing pandas package
import pandas as pd

# Creating two dictionaries
d1 = {'Time':pd.date_range(start ='12-07-2001',end ='12-12-2001', freq ='5H')}

# Creating DataFrames
df = pd.DataFrame(d1)

# Display the DataFrames
print("Original DataFrame 1:\n",df,"\n\n")

Output

The output of the above program is:

Example: Create hourly/minutely time range

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.