pandas-ta: Numpy ImportError: cannot import name 'sliding_window_view' from 'numpy.lib.stride_tricks'

Which version are you running? The lastest version is on Github. Pip is for major releases.

version v0.2.75

Running Windows 10

Describe the bug

I just installed the version 0.2.75 from github by downloading the .zip file, then installed using pip3 install pandas-ta-master.zip

Received a notification that I didn’t have ‘wheels’ installed so it used legacy method of install, but installation was successful.

But when I tried to add the library I get the error shown below.

I uninstalled pandas-ta, then I installed wheels. I then reinstalled pandas-ta successfully:

C:\Users\chuck\Downloads>pip3 install pandas-ta-master.zip
<snip>a bunch of installation details....</snip>
Successfully built pandas-ta
Installing collected packages: pandas-ta
Successfully installed pandas-ta-0.2.75b0

C:\Users\chuck\Downloads>

=== Below is the result of simply trying to import the library =====

>>> import pandas_ta as pta
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import pandas_ta as pta
  File "C:\Users\chuck\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas_ta\__init__.py", line 116, in <module>
    from pandas_ta.core import *
  File "C:\Users\chuck\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas_ta\core.py", line 4, in <module>
    from pandas_ta.candles.cdl_pattern import ALL_PATTERNS
  File "C:\Users\chuck\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas_ta\candles\__init__.py", line 2, in <module>
    from .cdl_doji import cdl_doji
  File "C:\Users\chuck\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas_ta\candles\cdl_doji.py", line 2, in <module>
    from pandas_ta.overlap import sma
  File "C:\Users\chuck\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas_ta\overlap\__init__.py", line 6, in <module>
    from .hilo import hilo
  File "C:\Users\chuck\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas_ta\overlap\hilo.py", line 4, in <module>
    from .ma import ma
  File "C:\Users\chuck\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas_ta\overlap\ma.py", line 8, in <module>
    from .linreg import linreg
  File "C:\Users\chuck\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas_ta\overlap\linreg.py", line 6, in <module>
    from numpy.lib.stride_tricks import sliding_window_view
ImportError: cannot import name 'sliding_window_view' from 'numpy.lib.stride_tricks' (C:\Users\chuck\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\lib\stride_tricks.py)
>>> 

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Ran into the same issue. Resolved it by updating numpy (current is 1.21.x):

$ pip install numpy --upgrade

Then reinstall pandas_ta:

$ pip uninstall pandas_ta $ pip install pandas_ta

Worked for me - so might work for you too.

Yep, that was the issue, which I thought was a red herring. But in fact it is new in 1.20 and I was running 1.19. Thanks for that.

Same issue!!!

My numpy version is ‘1.18.4’ import pandas_ta as ta I got same error and I did uninstall and reinstall.

~/anaconda3/lib/python3.7/site-packages/pandas_ta/overlap/linreg.py in <module>
      4 from numpy import nan as npNaN
      5 from numpy import pi as npPi
----> 6 from numpy.lib.stride_tricks import sliding_window_view
      7 from pandas import Series
      8 from pandas_ta.utils import get_offset, verify_series
pip uninstall pandas_ta
Found existing installation: pandas-ta 0.2.93b0
Uninstalling pandas-ta-0.2.93b0:

pip install pandas_ta
Collecting pandas_ta
  Using cached pandas_ta-0.2.45b0-py3-none-any.whl

My version is ‘Pandas TA v0.2.45b0’ and working perfectly!

Hello @ctilly,

This is unfortunate bug. However it is a Numpy ImportError and not a Pandas TA bug as noted by the last statement during the import:

ImportError: cannot import name 'sliding_window_view' from 'numpy.lib.stride_tricks'

According to Numpy, sliding_window_view documentation says that it is New in version 1.20.0. I would ensure that you are running the correct or latest version of Numpy. If updating Numpy continues to produce this ImportError, then you need to make an Issue with Numpy.

For further information about this Issue, you can dig deeper into this search if need.

Please let me know when you resolve the ImportError and get Pandas TA working. 😎 Hope this helps!

Kind Regards, KJ

@twopirllc Works well for me too - thanks!

I am aware of this. I am open to contributions that will incorporate both versions. sunglasses

I found two potential solutions. The first uses rolling as shown in the for window in s.rolling(window=2) example on the Pandas docs (https://pandas.pydata.org/pandas-docs/stable/user_guide/window.html#windowing-operations). Since it also yields windows which are less than length, this was the solution I came up with, adding the len(_) == length condition:

linreg_ = [linear_regression(_) for _ in close.rolling(length) if len(_) == length]

(Replaces the following line) https://github.com/twopirllc/pandas-ta/blob/3b40553dfa74954d99b8aed9dac2c813c4e91f39/pandas_ta/overlap/linreg.py#L57

This is very slow for some reason however, and I found a solution which is many times faster (~1.7s vs ~0.15s). It uses another similar numpy function which is present in the older versions and is almost exactly as fast as sliding_window_view.

def rolling_window(arr, window):
    shape = arr.shape[:-1] + (arr.shape[-1] - window + 1, window)
    strides = arr.strides + (arr.strides[-1],)
    return as_strided(arr, shape=shape, strides=strides)

[...]

linreg_ = [linear_regression(_) for _ in rolling_window(npArray(close), length)]

I found this solution at https://stackoverflow.com/a/6811241.