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:
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.
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.