본문 바로가기
  • Hello_
Python/Error

[Python_Error] TypeError: ufunc 'true_divide' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

by LDwDL 2022. 9. 29.
728x90
반응형

본 포스팅은 업무 중 발생했던 오류들을 예시를 통해 어떻게 해결을 했는지 적을 예정이다.

 

Error 메세지

 

TypeError: ufunc 'true_divide' not supported for the input 
types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

이 오류는 숫자와 np.array로 싸인 string 변수를 나눌 때 발생했던 오류이다.

 

[예시]

import numpy as np

number_1 = np.array([3,6,9]) # float array
number_2 = np.array(str(3))  # string

number_1/number_2

"""
TypeError                                 Traceback (most recent call last)
----> 1 number_1/number_2

TypeError: ufunc 'true_divide' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
"""

 

[해결]

해결법은 간단하다. np.array의 변수가 string이기에 이를 int 혹은 float의 숫자 형태로 변경해준 뒤 나누면 해결된다.

number_1/int(number_2)
# array([1., 2., 3.])

 

 

 

728x90
반응형

댓글