Cannot import name ‘_centered’ from ‘scipy.signal.signaltools’

We Are Going To Discuss About Cannot import name ‘_centered’ from ‘scipy.signal.signaltools’. So lets Start this Python Article.

Cannot import name ‘_centered’ from ‘scipy.signal.signaltools’

  1. How to solve Cannot import name '_centered' from 'scipy.signal.signaltools'

    I encountered the same problem while using statsmodels~=0.12.x. Increasing the statsmodels package to version 0.13.2, this import issue is resolved.
    UPDATE with more notes:
    before: installation of fixed version of statsmodels==0.12.2 which is dependent on scipy
    there was newly released scipy==1.8.0 – 2022-02-05 when installing it, got this problem:
    from statsmodels.tsa.seasonal import seasonal_decompose File "/usr/local/lib/python3.8/site-packages/statsmodels/tsa/seasonal.py", line 12, in <module> from statsmodels.tsa.filters.filtertools import convolution_filter File "/usr/local/lib/python3.8/site-packages/statsmodels/tsa/filters/filtertools.py", line 18, in <module> from scipy.signal.signaltools import _centered as trim_centered ImportError: cannot import name '_centered' from 'scipy.signal.signaltools' (/usr/local/lib/python3.8/site-packages/scipy/signal/signaltools.py)
    after:
    when bumping up statsmodels to the latest version available 0.13.2 release 2022-02-08, it works
    If you are not using statsmodels but other package which is dependent on scipy, have a look if there is newer version available (after the release of scipy to v.1.8.0)

  2. Cannot import name '_centered' from 'scipy.signal.signaltools'

    I encountered the same problem while using statsmodels~=0.12.x. Increasing the statsmodels package to version 0.13.2, this import issue is resolved.
    UPDATE with more notes:
    before: installation of fixed version of statsmodels==0.12.2 which is dependent on scipy
    there was newly released scipy==1.8.0 – 2022-02-05 when installing it, got this problem:
    from statsmodels.tsa.seasonal import seasonal_decompose File "/usr/local/lib/python3.8/site-packages/statsmodels/tsa/seasonal.py", line 12, in <module> from statsmodels.tsa.filters.filtertools import convolution_filter File "/usr/local/lib/python3.8/site-packages/statsmodels/tsa/filters/filtertools.py", line 18, in <module> from scipy.signal.signaltools import _centered as trim_centered ImportError: cannot import name '_centered' from 'scipy.signal.signaltools' (/usr/local/lib/python3.8/site-packages/scipy/signal/signaltools.py)
    after:
    when bumping up statsmodels to the latest version available 0.13.2 release 2022-02-08, it works
    If you are not using statsmodels but other package which is dependent on scipy, have a look if there is newer version available (after the release of scipy to v.1.8.0)

Solution 1

I encountered the same problem while using statsmodels~=0.12.x. Increasing the statsmodels package to version 0.13.2, this import issue is resolved.

UPDATE with more notes:

  • before:
    • installation of fixed version of statsmodels==0.12.2 which is dependent on scipy
    • there was newly released scipy==1.8.0 – 2022-02-05
      • when installing it, got this problem:
    from statsmodels.tsa.seasonal import seasonal_decompose
  File "/usr/local/lib/python3.8/site-packages/statsmodels/tsa/seasonal.py", line 12, in <module>
    from statsmodels.tsa.filters.filtertools import convolution_filter
  File "/usr/local/lib/python3.8/site-packages/statsmodels/tsa/filters/filtertools.py", line 18, in <module>
    from scipy.signal.signaltools import _centered as trim_centered
ImportError: cannot import name '_centered' from 'scipy.signal.signaltools' (/usr/local/lib/python3.8/site-packages/scipy/signal/signaltools.py)
  • after:

    • when bumping up statsmodels to the latest version available 0.13.2 release 2022-02-08, it works
  • If you are not using statsmodels but other package which is dependent on scipy, have a look if there is newer version available (after the release of scipy to v.1.8.0)

Original Author AndrejHan Of This Content

Solution 2

If you need to use that specific version of statsmodels 0.12.x with scipy 1.8.0 I have the following hack.
Basically it just re-publishes the existing (but private) _centered function as a public attribute to the module already imported in RAM.

It is a workaround, and if you can simply upgrade your dependencies to the latest versions. Only use this if you are forced to use those specific versions.

import  scipy.signal.signaltools

def _centered(arr, newsize):
    # Return the center newsize portion of the array.
    newsize = np.asarray(newsize)
    currsize = np.array(arr.shape)
    startind = (currsize - newsize) // 2
    endind = startind + newsize
    myslice = [slice(startind[k], endind[k]) for k in range(len(endind))]
    return arr[tuple(myslice)]

scipy.signal.signaltools._centered = _centered

Original Author Rod Senra Of This Content

Solution 3

With scipy 1.8.0, the deprecated form

from scipy.signal.signaltools import _centered

should become

from scipy.signal._signaltools import _centered

Note the underscore in front of signaltools

Original Author Thomas Cokelaer Of This Content

Solution 4

As it was mentioned by AndrejHan and ThomasCokelaer, you need to upgrade to statsmodels-0.13.2 with:

$ pip install statsmodels --upgrade

or

$ python3 -m pip install statsmodels --upgrade

Your error should disappear

Original Author ilciavo Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment