0

I am writing a time serie prediction algorithm where I need to iterate over multiple times series within the same df. It looks something like:

    def predict(self, ts, cat=None, encoding="utf-8", num_samples=100, quantiles=["0.1", "0.75", "0.9"]):
  
        prediction_times = [x.index[-1]+1 for x in ts]
        req = self.__encode_request(ts, cat, encoding, num_samples, quantiles)
        res = super(DeepARPredictor, self).predict(req)
        return self.__decode_response(res, prediction_times, encoding)

when calling the function later on in my code, the line

prediction_times = [x.index[-1]+1 for x in ts]
#index is a DateTimeIndex format

throws the following error:

TypeError: Addition/subtraction of integers and integer-arrays with Timestamp is no longer supported.  Instead of adding/subtracting `n`, use `n * obj.freq`

ts looks something like that:

Name: 100000ST, Length: 1989, dtype: int64, 2015-01-05    0
2015-01-06    0
2015-01-07    0
2015-01-08    0
2015-01-09    0
             ..
2020-06-11    0
2020-06-12    0
2020-06-13    0
2020-06-14    0
2020-06-15    0
Name: 100005ST, Length: 1989, dtype: int64, 2015-01-05     5
2015-01-06     0
2015-01-07     0
2015-01-08    40
2015-01-09     0
              ..
2020-06-11    75
2020-06-12    21
2020-06-13     0
2020-06-14     0
2020-06-15     0

I am not able to find a way to change this line to achieve the same result without the error. Any suggestion?

UPDATE: trying out prediction_times = [x.index.shift(freq=1) for x in ts] yields the same error

Murcielago
  • 905
  • 1
  • 8
  • 30
  • Are you looking for `prediction_times = x.index.shift()`? – Marat Nov 18 '20 at 16:38
  • I have tried ```prediction_times = [x.index.shift(freq=1) for x in ts]``` but it yields the same error – Murcielago Nov 18 '20 at 16:41
  • I mean, this should replace the whole line. There should be no list comprehension there – Marat Nov 18 '20 at 16:54
  • let me try it out on its own, so far using ```[x.index[-1]+datetime.timedelta(days=1)]``` removed the error according to this post https://stackoverflow.com/questions/61153546/addition-subtraction-of-integers-and-integer-arrays-with-timestamp-is-no-longer – Murcielago Nov 18 '20 at 16:59
  • same issue with index.shift on its own – Murcielago Nov 18 '20 at 17:02

1 Answers1

2

Pandas doesn't allow to add integer to datetime and vice-versa anymore.
You'll need to use an acceptable data type, like:

[x.index[-1]+pd.DateOffset() for x in ts]

or

[x.index[-1]+pd.Timedelta(days=1) for x in ts]
Cainã Max Couto-Silva
  • 4,839
  • 1
  • 11
  • 35