Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

303
Vistas
Can't use /= on numpy array

On a numpy array, why is it I can successfully use / 2:

>>> a=np.array([2, 4, 6])
>>> a = a / 2
>>> a
array([ 1.,  2.,  3.])

But I cannot use a /= 2?

>>> a=np.array([2, 4, 6])
>>> a /= 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: No loop matching the specified signature and casting
was found for ufunc true_divide

I've seen numpy Issue 6464, but don't understand from reading it and the linked release notes the reason this doesn't work.

Is there any way to get /= to work as expected?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

As pointed out in the comment, the change from int (which is how a is created) to float (which is the result of /) is not allowed when using /=. To "fix" this the dtype of a just has to be a float from the beginning:

a=np.array([2, 4, 6], dtype=np.float64)
a/=2
print(str(a))
>>>array([1., 2., 3.])
over 4 years ago · Santiago Trujillo Denunciar

0

As mentioned in the comments, a / 2 produces a float array, but the type of a is integer. Since NumPy's assignment operators are optimized to reuse the same array (that is a = a + 2 and a += 2 are not exactly the same, the first creates a new array while the second just reuses the existing one), you can not use them when the result has a different dtype. If what you want is an integer division, you can use the //= assignment operation:

>>> a = np.array([2, 4, 6])
>>> a //= 2
>>> a
array([1, 2, 3])
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda