Sktime: When the index of the data is a timestamp, it leads to "TypeError: Addition/subtraction of integers and integer-arrays with Timestamp is no longer supported. Instead of adding/subtracting `n`, use `n * obj.freq`"

Created on 18 Jul 2020  路  6Comments  路  Source: alan-turing-institute/sktime

Describe the bug
When I use a Series which has TimeStamp as the index, I get the following error in various steps:

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

This error occurs when I try a plot (plot_ys) and when I try to predict (y_pred = forecaster.predict(fh)).

For fixing the plot, I modified the file "sktime/utils/plotting/forecasting.py" in the line:

_-continuous_index = np.arange(y.index.min(), y.index.max() + 1)
+continuous_index = np.arange(1, len(y) + 1)

After the above change, I got a plot with the timestamp as the index.

image

Similarly, for predict, the following file needs fixing : sktime/forecasting/base/_fh.py in line:

-values = self + cutoff
+values = (self * cutoff.freq) + cutoff

Because, here direct addition is not possible due to the TimeStamp index.

To Reproduce
https://gist.github.com/shruthibalaji2307/e10dcae314773e75f6a03d4c43246f71

Input Data
nation_level_daily.zip

Expected behavior
I expected plot_ys to plot my data without cribbing about the TimeStamp index. It only works when I use a simple numerical index.

Versions
Linux-5.4.0-40-generic-x86_64-with-glibc2.29
Python 3.8.2 (default, Apr 27 2020, 15:53:34)
[GCC 9.3.0]
NumPy 1.19.0
SciPy 1.5.1
Pandas 1.0.5
sktime 0.4.1

Code Changes required
1) "sktime/utils/plotting/forecasting.py" :

_-continuous_index = np.arange(y.index.min(), y.index.max() + 1)
+continuous_index = np.arange(1, len(y) + 1)

2) sktime/forecasting/base/_fh.py in line:

-values = self + cutoff
+values = (self * cutoff.freq) + cutoff

3) Wherever addition/subtraction is done on the index of the data.

bug

Most helpful comment

Hello @mloning , I would be very interested. Sktime is a huge step towards easing time series analysis in Python and it would be great if we could support datetime indices as a part of it. Shall I go ahead and create a feature request and assign it to myself?

Also, please let me know if you have specific requirements in mind. I will pick it up right away.

All 6 comments

Dear @shruthibalaji2307 thanks for the detailed bug description! As you found out, we're currently not supporting date time indices. I believe adding support for them will require some substantial work. Would you be interested in working on that?

Hello @mloning , I would be very interested. Sktime is a huge step towards easing time series analysis in Python and it would be great if we could support datetime indices as a part of it. Shall I go ahead and create a feature request and assign it to myself?

Also, please let me know if you have specific requirements in mind. I will pick it up right away.

Hello everyone, it would be great to support time series with DatetimeIndex, as many APIs provide them, and they are useful in general. In the meantime there's a simple way to circumvent this problem, by resetting the index of the pandas series. This way the DatetimeIndex will be replaced by integer values. Here's a quick example, where I'm resetting the index of a series named 'Solar', and plotting the values with plot_ys():

plot_ys(df['Solar'].reset_index(drop=True))

Hi @derevirn, thanks for your comment! We're working on a next iteration of the forecasting module to add full support for date/time indices.

I am not sure if this bug will be fixed soon in coming version. By now I change the code to allow it support timestampindex in the series. Meanwhile, I also check if the timestamps are continuous.
if isinstance(y.index, pd.DatetimeIndex):
y = y.asfreq(pd.infer_freq(y.index))
is_continuous = not (pd.isna(y).any())
else:
continuous_index = np.arange(y.index.min(), y.index.max() + 1)
is_continuous = np.array_equal(y.index.values, continuous_index)
if len(y) < 3 or not is_continuous:
ax.scatter(y.index.values, y.values, label=label)
# otherwise use line plot
else:
ax.plot(y.index.values, y.values, label=label)
`

Hi all, and thanks @bingblackbean for sharing your solution here!

We added much more comprehensive date-time support in PR #392. It hasn't made it's way into a release yet, but should work when you install the development version.

You can find my solution for the plotting function here (might have also renamed it!)

I'll close this issue for now, please open a new one if anything else doesn't work!

Was this page helpful?
0 / 5 - 0 ratings