简体   繁体   中英

TypeError: copy() takes no keyword arguments in Python 3.x

For the last few months I have been using the following lines of code without issue:

a['Area Code']=a['mpan'].str[:2]
a.groupby(a['Area Code'])
a=a[a['Area Code'].astype(int)<=23]

data_list=a.groupby('Area Code')[['mpan']].apply(lambda g: list (map(tuple, g.values.tolist()))).to_dict()

All of a sudden I am running into the following error from the last line:

TypeError: copy() takes no keyword arguments

'a' is a (23,2) dataframe that has been grouped by a column called 'Area Code' (and is populated by an SQL query). 'data_list' is to be a dict that uses the 'Area Code' column as the keys. I'm a little confused as to why the error involves copy() without it being in the line itself.

I'm still fairly new to Python and got help for that particular line so not sure of the underlying nature.

Traceback (most recent call last):

  File "C:\ProgramData\Anaconda3\lib\site- 
  packages\pandas\core\groupby\groupby.py", line 735, in apply
  result = self._python_apply_general(f)

  File "C:\ProgramData\Anaconda3\lib\site- 
  packages\pandas\core\groupby\groupby.py", line 751, in _python_apply_general
  keys, values, mutated = self.grouper.apply(f, self._selected_obj, self.axis)

  File "C:\ProgramData\Anaconda3\lib\site- 
  packages\pandas\core\groupby\ops.py", line 171, in apply
  result_values, mutated = splitter.fast_apply(f, group_keys)

  File "C:\ProgramData\Anaconda3\lib\site- 
  packages\pandas\core\groupby\ops.py", line 925, in fast_apply
  return libreduction.apply_frame_axis0(sdata, f, names, starts, ends)

  File "pandas\_libs\reduction.pyx", line 505, in 
  pandas._libs.reduction.apply_frame_axis0

  TypeError: copy() takes no keyword arguments

  During handling of the above exception, another exception occurred:

  Traceback (most recent call last):

  File "<ipython-input-31-84e07b1b1635>", line 1, in <module>
  a.groupby('Area Code')[['mpan']].apply(lambda g: list (map(tuple, 
  g.values.tolist()))).to_dict()

  File "C:\ProgramData\Anaconda3\lib\site- 
  packages\pandas\core\groupby\groupby.py", line 746, in apply
  return self._python_apply_general(f)

  File "C:\ProgramData\Anaconda3\lib\site- 
  packages\pandas\core\groupby\groupby.py", line 751, in _python_apply_general
  keys, values, mutated = self.grouper.apply(f, self._selected_obj, self.axis)

  File "C:\ProgramData\Anaconda3\lib\site- 
  packages\pandas\core\groupby\ops.py", line 171, in apply
  result_values, mutated = splitter.fast_apply(f, group_keys)

  File "C:\ProgramData\Anaconda3\lib\site- 
  packages\pandas\core\groupby\ops.py", line 925, in fast_apply
  return libreduction.apply_frame_axis0(sdata, f, names, starts, ends)

  File "pandas\_libs\reduction.pyx", line 505, in 
  pandas._libs.reduction.apply_frame_axis0

  TypeError: copy() takes no keyword arguments

Update : the issue has been fixed in Pandas 1.0.1 , you can install it, for example, with pip:

pip install pandas==1.0.1

This is a known issue ( 31441 and 31456 ):

GroupBy.apply was raising TypeError if called with function which returned list (:issue: 31441 )

This has been solved in 1.0.x , which you can for example install through pip :

pip install git+ssh://git@github.com/pandas-dev/pandas.git@1.0.x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM