0

I have a timeseries data where X1 column is my frequency column. I have seasonality column which is repetitive in nature (values repeating in every 5 days). I have third column which is sum of above two mentioned columns.

Below is the sample data

I want to create mechanism in such a way that whenever new X1 values comes, then seasonlity component would be added from where it has ended. For ex if new data comes on 25-07-2019 the 0.3 to be added to X1 column and similarly -0.2, 0.5 will be added if data comes on 26-07-2019 and 27-07-2019.

6
  • Have you looked at using an iterator? w3schools.com/python/python_iterators.asp Commented Sep 22, 2019 at 12:25
  • @ Maus I would check if that is helpful Commented Sep 22, 2019 at 12:30
  • Do you know in advance the seasonality loop or must be inferred from the data? Commented Sep 22, 2019 at 12:32
  • @ Valentino Yes, I have extracted the seasonal component from the data. In this sample example, the pattern is repeating in every 5 days. I want to add seasonality component in forecasted value in similar sequence. Commented Sep 22, 2019 at 12:41
  • Please, remember to show your data as text, not as image. People cannot copy-paste an image to test their answers. Commented Sep 22, 2019 at 14:35

1 Answer 1

1

If you already know the seasonality you can do it like this

import pandas as pd

seasonality = np.array([0.5, -0.4, 0.1, 0.3, -0.2])
d = {
    "Date": pd.date_range("7-12-2019", "7-23-2019", freq="1D"),
    "X1": np.random.choice(9, 12)
}
df = pd.DataFrame(d)

## calculate seasonality based on date diff between date in 1st row and current row, this will work even if there are some dates missing

df["seasonality"] = seasonality[(df["Date"]-df["Date"].iloc[0]).dt.days%5]

df["X1 + seasonality"] = df["seasonality"] + df["X1"]


display(df)

if you need to infer the seasonality from the data then you need to add some logic to infer the seasonality 1st than do these operations.

if you want to add rows incrementally than you can perform these calculation on rows being added then append it to the original df. Note you must use 0th row of original dataframe in substraction

Sign up to request clarification or add additional context in comments.

2 Comments

What is the logic of having random.choice (9,12) ? How it would vary if I have hourly datetime ?
I have just put random.choice (9,12) to create dummy data to test. It should also work if datetime has hour. If you want to do it in hourly interval then instead to finding difference in days you need to find hour difference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.