Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ctilly opened this issue May 6, 2021 · 18 comments
Labels
duplicate This issue or pull request already exists info Informational

Comments

@ctilly
Copy link

ctilly commented May 6, 2021

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)
>>> 
@ctilly ctilly added the bug Something isn't working label May 6, 2021
@twopirllc twopirllc removed the bug Something isn't working label May 6, 2021
@twopirllc twopirllc removed their assignment May 6, 2021
@twopirllc
Copy link
Owner

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 twopirllc changed the title Error when attempting to use v0.2.75 Numpy ImportError: cannot import name 'sliding_window_view' from 'numpy.lib.stride_tricks' [Error when attempting to use v0.2.75] May 6, 2021
@twopirllc twopirllc changed the title Numpy ImportError: cannot import name 'sliding_window_view' from 'numpy.lib.stride_tricks' [Error when attempting to use v0.2.75] Numpy ImportError: cannot import name 'sliding_window_view' from 'numpy.lib.stride_tricks' May 6, 2021
@ctilly
Copy link
Author

ctilly commented May 6, 2021

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.

@ctilly ctilly closed this as completed May 6, 2021
@twopirllc
Copy link
Owner

@ctilly,

No worries. Thanks for replying and letting me know. I appreciate it.

Thanks,
KJ

@dikien
Copy link

dikien commented Jun 26, 2021

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!

@twopirllc
Copy link
Owner

Hello @dikien,

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

@peturssonstefan
Copy link

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.

@rushiai
Copy link

rushiai commented Jul 6, 2021

Same issue!!!

@brandonculver
Copy link

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.

This is not a great solution since other libraries require slightly older versions of numpy.

@twopirllc
Copy link
Owner

@brandonculver,

This is not a great solution since other libraries require slightly older versions of numpy.

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

Thanks,
KJ

@abmyii
Copy link

abmyii commented Jul 14, 2021

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)

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

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.

@twopirllc
Copy link
Owner

Hello @abmyii,

This sounds great! Thanks for helping to investigate an efficient and backwards compatible solution! I will give it a try as soon as I can. I appreciate it.

Thanks,
KJ

@AlexandreBourrieau
Copy link

Same problem here !

@twopirllc
Copy link
Owner

@AlexandreBourrieau,

Noted. Working on it.

Kind Regards,
KJ

@opsxcq
Copy link

opsxcq commented Jul 24, 2021

Same issue here, just to report, this makes impossible to have tensorflow and pandas-ta on the same project.

Using version ^1.21.1 for numpy

Updating dependencies
Resolving dependencies... (1.9s)

[SolverProblemError]
Because no versions of tensorflow match >2.4.0,<2.4.1 || >2.4.1,<2.4.2 || >2.4.2,<2.5.0 || >2.5.0,<3.0.0
 and tensorflow (2.4.0) depends on numpy (>=1.19.2,<1.20.0), tensorflow (>=2.4.0,<2.4.1 || >2.4.1,<2.4.2 || >2.4.2,<2.5.0 || >2.5.0,<3.0.0) requires numpy (>=1.19.2,<1.20.0).
And because tensorflow (2.4.1) depends on numpy (>=1.19.2,<1.20.0), tensorflow (>=2.4.0,<2.4.2 || >2.4.2,<2.5.0 || >2.5.0,<3.0.0) requires numpy (>=1.19.2,<1.20.0).
And because tensorflow (2.4.2) depends on numpy (>=1.19.2,<1.20.0)
 and tensorflow (2.5.0) depends on numpy (>=1.19.2,<1.20.0), tensorflow (>=2.4.0,<3.0.0) requires numpy (>=1.19.2,<1.20.0).
So, because XXX depends on both tensorflow (^2.4.0) and numpy (^1.21.1), version solving failed.

@twopirllc
Copy link
Owner

@opsxcq,

Again:

Noted. Working on it.

Kind Regards,
KJ

twopirllc added a commit that referenced this issue Jul 25, 2021
@twopirllc
Copy link
Owner

Hello @abmyii, @AlexandreBourrieau, @brandonculver, @dikien, @opsxcq, @peturssonstefan, @rushiai,

I have an updated version on the development branch that should address the numpy version problem. I would appreciate your feedback on this when you get a chance before I update the main branch.

$ pip install -U git+https://github.com/twopirllc/pandas-ta.git@development

Thanks,
KJ

@abmyii
Copy link

abmyii commented Jul 26, 2021

@twopirllc Works well for me too - thanks!

@twopirllc
Copy link
Owner

Hello @AlexandreBourrieau, @brandonculver, @dikien, @opsxcq, @peturssonstefan, @rushiai,

New stable version v0.3.14b should help resolve this Issue. Please let me know otherwise.

$ pip install pandas_ta

Thanks,
KJ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists info Informational
Projects
None yet
Development

No branches or pull requests

9 participants