파이썬/Pandas

[Python] Pandas mean Error(TypeError)

2023. 4. 24. 15:29
목차
  1. 1. 문제
  2. 2. 원인
  3. 3. 해결

1. 문제

판다스를 2.0.0 으로 업그레이드 하고 다음과 같은 오류가 발생 하였다.

df = pd.DataFrame([['a',1],['b',1]])df.mean()
df.mean()

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
File /opt/homebrew/Caskroom/miniconda/base/envs/test2/lib/python3.9/site-packages/pandas/core/nanops.py:1680, in _ensure_numeric(x)
   1679 try:
-> 1680     x = x.astype(np.complex128)
   1681 except (TypeError, ValueError):

ValueError: complex() arg is a malformed string

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
File /opt/homebrew/Caskroom/miniconda/base/envs/test2/lib/python3.9/site-packages/pandas/core/nanops.py:1683, in _ensure_numeric(x)
   1682 try:
-> 1683     x = x.astype(np.float64)
   1684 except ValueError as err:
   1685     # GH#29941 we get here with object arrays containing strs

ValueError: could not convert string to float: 'ab'

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
Cell In[7], line 1
----> 1 df.mean()

File /opt/homebrew/Caskroom/miniconda/base/envs/test2/lib/python3.9/site-packages/pandas/core/generic.py:11563, in NDFrame._add_numeric_operations.<locals>.mean(self, axis, skipna, numeric_only, **kwargs)
  11546 @doc(
  11547     _num_doc,
  11548     desc="Return the mean of the values over the requested axis.",
   (...)
  11561     **kwargs,
  11562 ):
> 11563     return NDFrame.mean(self, axis, skipna, numeric_only, **kwargs)

File /opt/homebrew/Caskroom/miniconda/base/envs/test2/lib/python3.9/site-packages/pandas/core/generic.py:11208, in NDFrame.mean(self, axis, skipna, numeric_only, **kwargs)
  11201 def mean(
  11202     self,
  11203     axis: Axis | None = 0,
   (...)
  11206     **kwargs,
  11207 ) -> Series | float:
> 11208     return self._stat_function(
  11209         "mean", nanops.nanmean, axis, skipna, numeric_only, **kwargs
  11210     )

File /opt/homebrew/Caskroom/miniconda/base/envs/test2/lib/python3.9/site-packages/pandas/core/generic.py:11165, in NDFrame._stat_function(self, name, func, axis, skipna, numeric_only, **kwargs)
  11161     nv.validate_stat_func((), kwargs, fname=name)
  11163 validate_bool_kwarg(skipna, "skipna", none_allowed=False)
> 11165 return self._reduce(
  11166     func, name=name, axis=axis, skipna=skipna, numeric_only=numeric_only
  11167 )

File /opt/homebrew/Caskroom/miniconda/base/envs/test2/lib/python3.9/site-packages/pandas/core/frame.py:10519, in DataFrame._reduce(self, op, name, axis, skipna, numeric_only, filter_type, **kwds)
  10515     df = df.T
  10517 # After possibly _get_data and transposing, we are now in the
  10518 #  simple case where we can use BlockManager.reduce
> 10519 res = df._mgr.reduce(blk_func)
  10520 out = df._constructor(res).iloc[0]
  10521 if out_dtype is not None:

File /opt/homebrew/Caskroom/miniconda/base/envs/test2/lib/python3.9/site-packages/pandas/core/internals/managers.py:1534, in BlockManager.reduce(self, func)
   1532 res_blocks: list[Block] = []
   1533 for blk in self.blocks:
-> 1534     nbs = blk.reduce(func)
   1535     res_blocks.extend(nbs)
   1537 index = Index([None])  # placeholder

File /opt/homebrew/Caskroom/miniconda/base/envs/test2/lib/python3.9/site-packages/pandas/core/internals/blocks.py:339, in Block.reduce(self, func)
    333 @final
    334 def reduce(self, func) -> list[Block]:
    335     # We will apply the function and reshape the result into a single-row
    336     #  Block with the same mgr_locs; squeezing will be done at a higher level
    337     assert self.ndim == 2
--> 339     result = func(self.values)
    341     if self.values.ndim == 1:
    342         # TODO(EA2D): special case not needed with 2D EAs
    343         res_values = np.array([[result]])

File /opt/homebrew/Caskroom/miniconda/base/envs/test2/lib/python3.9/site-packages/pandas/core/frame.py:10482, in DataFrame._reduce.<locals>.blk_func(values, axis)
  10480     return values._reduce(name, skipna=skipna, **kwds)
  10481 else:
> 10482     return op(values, axis=axis, skipna=skipna, **kwds)

File /opt/homebrew/Caskroom/miniconda/base/envs/test2/lib/python3.9/site-packages/pandas/core/nanops.py:96, in disallow.__call__.<locals>._f(*args, **kwargs)
     94 try:
     95     with np.errstate(invalid="ignore"):
---> 96         return f(*args, **kwargs)
     97 except ValueError as e:
     98     # we want to transform an object array
     99     # ValueError message to the more typical TypeError
    100     # e.g. this is normally a disallowed function on
    101     # object arrays that contain strings
    102     if is_object_dtype(args[0]):

File /opt/homebrew/Caskroom/miniconda/base/envs/test2/lib/python3.9/site-packages/pandas/core/nanops.py:158, in bottleneck_switch.__call__.<locals>.f(values, axis, skipna, **kwds)
    156         result = alt(values, axis=axis, skipna=skipna, **kwds)
    157 else:
--> 158     result = alt(values, axis=axis, skipna=skipna, **kwds)
    160 return result

File /opt/homebrew/Caskroom/miniconda/base/envs/test2/lib/python3.9/site-packages/pandas/core/nanops.py:421, in _datetimelike_compat.<locals>.new_func(values, axis, skipna, mask, **kwargs)
    418 if datetimelike and mask is None:
    419     mask = isna(values)
--> 421 result = func(values, axis=axis, skipna=skipna, mask=mask, **kwargs)
    423 if datetimelike:
    424     result = _wrap_results(result, orig_values.dtype, fill_value=iNaT)

File /opt/homebrew/Caskroom/miniconda/base/envs/test2/lib/python3.9/site-packages/pandas/core/nanops.py:727, in nanmean(values, axis, skipna, mask)
    724     dtype_count = dtype
    726 count = _get_counts(values.shape, mask, axis, dtype=dtype_count)
--> 727 the_sum = _ensure_numeric(values.sum(axis, dtype=dtype_sum))
    729 if axis is not None and getattr(the_sum, "ndim", False):
    730     count = cast(np.ndarray, count)

File /opt/homebrew/Caskroom/miniconda/base/envs/test2/lib/python3.9/site-packages/pandas/core/nanops.py:1686, in _ensure_numeric(x)
   1683         x = x.astype(np.float64)
   1684     except ValueError as err:
   1685         # GH#29941 we get here with object arrays containing strs
-> 1686         raise TypeError(f"Could not convert {x} to numeric") from err
   1687 else:
   1688     if not np.any(np.imag(x)):

TypeError: Could not convert ['ab'] to numeric

2. 원인

2.0.0으로 업그레이드 되면서 numeric_only라는 인수의 Default 값이 None 에서 False로 바뀌어져 있다.

Signature:
df.mean(
    axis: 'AxisInt | None' = 0,
    skipna: 'bool_t' = True,
    numeric_only: 'bool_t' = False,
    **kwargs,
)

3. 해결

numeric_only 인수를 True 로 주면 잘 실행된다.

df.mean(numeric_only=True)
  • 1. 문제
  • 2. 원인
  • 3. 해결
easysheep
easysheep
easysheep
나의 개발자 일기
easysheep
전체
오늘
어제
  • 분류 전체보기 (95)
    • 파이썬 (7)
      • 자료형 (0)
      • matplotlib (2)
      • Tensorflow (1)
      • Selenium (1)
      • Numpy (2)
      • Pandas (1)
    • 장난감 프로젝트 (3)
    • AI_수학 (0)
      • 통계 (0)
    • 알고리즘 (63)
      • 브루트 포스 (3)
      • 그리드 알고리즘 (5)
      • 너비 우선 탐색(bfs) (12)
      • 깊이 우선 탐색(DFS) (1)
      • 최단 경로 구하기(플로이드 워셜) (1)
      • 동적 계획법(Dynamic Programming) (8)
      • Dijkstra algorithm(데이크스트라 알.. (6)
    • Backend (1)
      • Django (1)
    • 딥러닝 (1)
      • Regression(회귀) (0)
    • 머신러닝 (3)
      • Daycon (1)
      • 직접 구현 (1)
    • AWS (3)
    • DB (2)
      • MongoDB (2)
    • kubernetes (3)
    • Docker (4)
    • Stress Test Toll (0)
      • Jmeter (0)
      • nGrinder (0)
    • Ubuntu (3)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • grafana
  • gradio
  • matplotlib
  • error
  • mysql
  • Mac
  • Python
  • dynamic programming
  • helm
  • heap
  • Numpy
  • 백준
  • 2*n 타일링
  • ubuntu
  • 문자열
  • aws
  • 머신 러닝
  • 헬름 설치
  • 우분투에 헬름 설치
  • 파이썬
  • 너비 우선 탐색
  • Docker
  • Cannot stat file /proc/528/fd/0: Permission denied
  • Bind Mounts
  • 알고리즘
  • BFS
  • validate service connection
  • error: version in "./docker-compose.yaml" is unsupported.
  • ML
  • 데이크스트라

최근 댓글

최근 글

hELLO · Designed By 정상우.
easysheep
[Python] Pandas mean Error(TypeError)
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.