[Python] How to ignore Python warnings for complex numbers

Discussão em 'Python' iniciado por Stack, Julho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I have a list that I need to include both real values and complex values. However, when I don't initialize the array using numpy.zeros(dtype=complex), I get the following error:

    n[2] = 3.7095 + 0.007j


    TypeError: can't convert complex to float
    ​

    However, when I do set the dtype to complex, I get the following warning when I use a real number (which doesn't break my code, but it causes a ton of unnecessary red warning messages that clutter my console:


    ComplexWarning: Casting complex values to real discards the imaginary part
    ​

    How can I disable the warning message, or set it up another way so the values can be imaginary or complex?

    Continue reading...

Compartilhe esta Página