Time series data are the dataset that has been collected in a regular or constant time intervals. Time series data are used to follow a long term forecast, observe a time-dependent trend or a seasonality trend. This is very useful and commonly used in financial institutes, retail businesses, real estate, and many other types of businesses. But what about you have the data but the dates are not recorded. What if you need to use last year’s data this year to generate an experimental report? Or, you may need to use last quarter’s data in this quarter. Sometimes it is required for research, analysis, or forecasting purpose. But this year’s or this quarter’s holidays and weekends will be different.

In this article, I will explain,

  1. How to generate a series of dates/times
  2. Using the holiday calendar
  3. Making custom holiday calendar
  4. Incorporating the time series in a dataset

Generating Time Series

Luckily Pandas has a function named date-range to generate a series of dates or times. We will see how we can use it to solve some problems that we may encounter at work. Here, we will solve a few questions.

  1. Let’s start with the most simple one. Generate a series of dates with the frequency of a day.
import pandas as pd
pd.date_range(start = '1/1/2020', end='1/15/2020')

#Output:
DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05', '2020-01-06', '2020-01-07', '2020-01-08',                '2020-01-09', '2020-01-10', '2020-01-11', '2020-01-12',                '2020-01-13', '2020-01-14', '2020-01-15'],               dtype='datetime64[ns]', freq='D')

2. Generate a series of dates with an interval of two days

pd.date_range('1/1/2020', '1/15/2020', freq='2D')

#Output:
DatetimeIndex(['2020-01-01', '2020-01-03', '2020-01-05', '2020-01-07', '2020-01-09', '2020-01-11', '2020-01-13', '2020-01-15'],               dtype='datetime64[ns]', freq='2D')

If you notice, we do not need to mention start and end all the time. By default, the first date is taken as the start date and the second one is taken as the end date.

#python #pandas #towards-data-science #time-series-analysis #timeseries

Generate Time Series And Custom Holiday Calendars in Pandas
13.10 GEEK