How to generate a timeseries for 10 weekdays with random values using Python?

I want to create time-series data using Python libraries and the task I want to do involves creating a time-series from a given specific date which contains 10 weekdays, the day can be any day of the week and I want this time-series to contain random values. For example:

2000-01-01    44
2000-01-08    63
2000-01-15    99
2000-01-22    99
2000-01-29    12
2000-02-05     8
2000-02-12    14
2000-02-19    22
2000-02-26    13
2000-03-04    46

This time-series contain 10 weekdays specifically Saturdays, and starts from 2000-01-01 which is also a Saturday, the series then contains some random values between 1 and 100. I hope I have given a clear idea of what I want, please provide me with some methods of doing this in Python.

Hi @mubashir_rizvi, You should refer to this approach, in which I have used pd.DateOffset() and random.sample() functions:

  • This method uses a loop to generate the dates by iterating over 10 Saturdays, starting from the given date. Inside the loop, the date is converted to a date-time object using the pd.to_datetime().
  • The pd.DateOffset() shifts or offsets a date object by a specified amount. Here we have used pd.DateOffset(weekday=5, weeks=i) to shift our given date that corresponds to the i-th Saturday after the specified start date.
  • The weekday specifies the day of the week, and 5 corresponds to Saturday, and the weeks specifies the number of weeks after the start date.
  • A random value is generated using the random.sample() function for that date currently in the iteration. The date and value are appended to their respective lists in each iteration of the loop.
  • Lastly, a time series is created using pd.Series() in the same way it was made in the previous methods.

Note:

  • The advantage of this method is that it uses a for loop allowing more flexibility and it can be adapted to more complex scenarios.
  • It also allows for more control over the generation of dates and values, and the resulting time series can be customized in many ways.
  • This approach is particularly useful when dealing with irregular or unpredictable data, or when the frequency of the time series needs to be determined dynamically based on other criteria.

Hello @mubashir_rizvi , you can also achieve your goal by using multiple Pandas functions and random.sample(). Let me show a code below to you:

Note:

  • The advantage of this code is that it uses a combination of Pandas and Python’s built-in libraries (random) to generate the time series.

  • It uses pd.offsets.Week() to create an offset object for Saturdays, which is more concise and readable than using pd.DateOffset() used in method 3.

  • It also uses random.sample() to generate random values, which is a simple and efficient way to generate random values within a given range.