简体   繁体   English

类型错误:copy() 在 Python 3.x 中不接受关键字参数

[英]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). 'a' 是一个 (23,2) 数据框,它已按名为 'Area Code' 的列分组(并由 SQL 查询填充)。 'data_list' is to be a dict that uses the 'Area Code' column as the keys. 'data_list' 是一个使用 'Area Code' 列作为键的字典。 I'm a little confused as to why the error involves copy() without it being in the line itself.我有点困惑为什么错误涉及 copy() 而它不在行本身中。

I'm still fairly new to Python and got help for that particular line so not sure of the underlying nature.我对 Python 还是很陌生,并获得了该特定行的帮助,因此不确定潜在的性质。

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:更新:该问题已在Pandas 1.0.1 中修复,您可以安装它,例如,使用 pip:

pip install pandas==1.0.1

This is a known issue ( 31441 and 31456 ):这是一个已知问题( 3144131456 ):

GroupBy.apply was raising TypeError if called with function which returned list (:issue: 31441 )如果使用返回列表的函数调用GroupBy.apply引发TypeError (:issue: 31441 )

This has been solved in 1.0.x , which you can for example install through pip :这已在1.0.x 中解决,例如您可以通过pip安装:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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