Generate a date range
Pandas package is imported. pd.date_range() method is used to create a date range, the date range has a monthly frequency.
Python3
# importing pandas import pandas as pd # creating a date range Date_range = pd.date_range(start = '1/12/2020' , end = '20/5/2021' , freq = 'M' ) print (Date_range) print ( type (Date_range)) print ( type (Date_range[ 0 ])) |
Output:
DatetimeIndex(['2020-01-31', '2020-02-29', '2020-03-31', '2020-04-30', '2020-05-31', '2020-06-30', '2020-07-31', '2020-08-31', '2020-09-30', '2020-10-31', '2020-11-30', '2020-12-31', '2021-01-31', '2021-02-28', '2021-03-31', '2021-04-30'], dtype='datetime64[ns]', freq='M') <class 'pandas.core.indexes.datetimes.DatetimeIndex'> <class 'pandas._libs.tslibs.timestamps.Timestamp'>
Manipulating Time Series Data in Python
A collection of observations (activity) for a single subject (entity) at various time intervals is known as time-series data. In the case of metrics, time series are equally spaced and in the case of events, time series are unequally spaced. We may add the date and time for each record in this Pandas module, as well as fetch dataframe records and discover data inside a specific date and time range.