Python Forum
DF.groupby(col).min works, mean gets a "not implemented" error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DF.groupby(col).min works, mean gets a "not implemented" error
#2
My guess is mpg.csv contains something that confuses pands.read_csv.

I can get the same error like this:
import pandas as pd

df = pd.read_csv("data.csv")

print(df)
print(df.dtypes)
print(df.groupby("model_year").mean()["mpg"])
Output:
model_year mpg 0 1 l 1 2 1 2 2 2 3 3 1 4 3 2 5 3 3 model_year int64 mpg object dtype: object
Error:
Traceback (most recent call last): File "venv\lib\site-packages\pandas\core\groupby\groupby.py", line 1871, in _agg_py_fallback res_values = self.grouper.agg_series(ser, alt, preserve_dtype=True) File "venv\lib\site-packages\pandas\core\groupby\ops.py", line 850, in agg_series result = self._aggregate_series_pure_python(obj, func) File "venv\lib\site-packages\pandas\core\groupby\ops.py", line 871, in _aggregate_series_pure_python res = func(group) File "venv\lib\site-packages\pandas\core\groupby\groupby.py", line 2377, in <lambda> alt=lambda x: Series(x).mean(numeric_only=numeric_only), File "venv\lib\site-packages\pandas\core\series.py", line 6221, in mean return NDFrame.mean(self, axis, skipna, numeric_only, **kwargs) File "venv\lib\site-packages\pandas\core\generic.py", line 11978, in mean return self._stat_function( File "venv\lib\site-packages\pandas\core\generic.py", line 11935, in _stat_function return self._reduce( File "venv\lib\site-packages\pandas\core\series.py", line 6129, in _reduce return op(delegate, skipna=skipna, **kwds) File "venv\lib\site-packages\pandas\core\nanops.py", line 147, in f result = alt(values, axis=axis, skipna=skipna, **kwds) File "venv\lib\site-packages\pandas\core\nanops.py", line 404, in new_func result = func(values, axis=axis, skipna=skipna, mask=mask, **kwargs) File "venv\lib\site-packages\pandas\core\nanops.py", line 720, in nanmean the_sum = _ensure_numeric(the_sum) File "venv\lib\site-packages\pandas\core\nanops.py", line 1693, in _ensure_numeric raise TypeError(f"Could not convert string '{x}' to numeric") TypeError: Could not convert string 'l' to numeric The above exception was the direct cause of the following exception: Traceback (most recent call last): File "test.py", line 7, in <module> print(df.groupby("model_year").mean()["mpg"]) File "venv\lib\site-packages\pandas\core\groupby\groupby.py", line 2375, in mean result = self._cython_agg_general( File "venv\lib\site-packages\pandas\core\groupby\groupby.py", line 1926, in _cython_agg_general new_mgr = data.grouped_reduce(array_func) File "venv\lib\site-packages\pandas\core\internals\managers.py", line 1428, in grouped_reduce applied = sb.apply(func) File "venv\lib\site-packages\pandas\core\internals\blocks.py", line 366, in apply result = func(self.values, **kwargs) File "venv\lib\site-packages\pandas\core\groupby\groupby.py", line 1923, in array_func result = self._agg_py_fallback(how, values, ndim=data.ndim, alt=alt) File "venv\lib\site-packages\pandas\core\groupby\groupby.py", line 1875, in _agg_py_fallback raise type(err)(msg) from err TypeError: agg function failed [how->mean,dtype->object]
In my example my mpg.csv has a lowercase L instead of a one. You get a slightly different error trace, but that may be due to the mpg.csv file having a slightly different error. Can you post mpg.csv?
Reply


Messages In This Thread
RE: DF.groupby(col).min works, mean gets a "not implemented" error - by deanhystad - Feb-29-2024, 04:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas - error when running Pycharm, but works on cmd line zxcv101 1 1,419 Jun-18-2022, 01:09 PM
Last Post: snippsat
Brick Have I implemented this function correctly? naggafin 4 2,466 May-22-2022, 02:52 AM
Last Post: stevendaprano
  NotImplementedError: pseudo-class is not implemented - how to Update Python to solve apollo 1 3,168 May-16-2021, 08:03 AM
Last Post: buran
  Function throws error but then works? Milfredo 10 3,943 Sep-12-2020, 05:16 AM
Last Post: Milfredo
  delete a file works but with error Leon79 4 3,022 Jul-14-2020, 06:51 AM
Last Post: snippsat
  Could I override a fully implemented method zatlas1 2 2,472 Jun-06-2019, 02:20 AM
Last Post: zatlas1
  Script works ok on windows but gives error on ubuntu papampi 3 4,131 Oct-11-2017, 04:17 PM
Last Post: papampi
  How to you find the file where a class or a function was implemented? MisterX 4 4,266 Mar-16-2017, 09:51 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020