BUG: apply on DataFrame results in TypeError: copy() missing 1 required positional argument: 'self'
· Issue #46684 · pandas-dev/pandas (original) (raw)
Pandas version checks
- I have checked that this issue has not already been reported.
- I have confirmed this bug exists on the latest version of pandas.
- I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
pd.DataFrame(list(range(100))).apply(lambda x: type(x))
pd.DataFrame(list(range(100))).apply(lambda x: type(x), axis=1)
Issue Description
Both of above lines produce errors now while they used to work in the past:
>>> pd.DataFrame(list(range(100))).apply(lambda x: type(x))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [15], in <module>
----> 1 pd.DataFrame(list(range(100))).apply(lambda x: type(x))
File /usr/local/lib/python3.9/site-packages/pandas/core/frame.py:8839, in DataFrame.apply(self, func, axis, raw, result_type, args, **kwargs)
8828 from pandas.core.apply import frame_apply
8830 op = frame_apply(
8831 self,
8832 func=func,
(...)
8837 kwargs=kwargs,
8838 )
-> 8839 return op.apply().__finalize__(self, method="apply")
File /usr/local/lib/python3.9/site-packages/pandas/core/apply.py:727, in FrameApply.apply(self)
724 elif self.raw:
725 return self.apply_raw()
--> 727 return self.apply_standard()
File /usr/local/lib/python3.9/site-packages/pandas/core/apply.py:851, in FrameApply.apply_standard(self)
850 def apply_standard(self):
--> 851 results, res_index = self.apply_series_generator()
853 # wrap results
854 return self.wrap_results(results, res_index)
File /usr/local/lib/python3.9/site-packages/pandas/core/apply.py:871, in FrameApply.apply_series_generator(self)
867 results[i] = self.f(v)
868 if isinstance(results[i], ABCSeries):
869 # If we have a view on v, we need to make a copy because
870 # series_generator will swap out the underlying data
--> 871 results[i] = results[i].copy(deep=False)
873 return results, res_index
TypeError: copy() missing 1 required positional argument: 'self'
>>> pd.DataFrame(list(range(100))).apply(lambda x: type(x), axis=1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [14], in <module>
----> 1 pd.DataFrame(list(range(100))).apply(lambda x: type(x), axis=1)
File /usr/local/lib/python3.9/site-packages/pandas/core/frame.py:8839, in DataFrame.apply(self, func, axis, raw, result_type, args, **kwargs)
8828 from pandas.core.apply import frame_apply
8830 op = frame_apply(
8831 self,
8832 func=func,
(...)
8837 kwargs=kwargs,
8838 )
-> 8839 return op.apply().__finalize__(self, method="apply")
File /usr/local/lib/python3.9/site-packages/pandas/core/apply.py:727, in FrameApply.apply(self)
724 elif self.raw:
725 return self.apply_raw()
--> 727 return self.apply_standard()
File /usr/local/lib/python3.9/site-packages/pandas/core/apply.py:851, in FrameApply.apply_standard(self)
850 def apply_standard(self):
--> 851 results, res_index = self.apply_series_generator()
853 # wrap results
854 return self.wrap_results(results, res_index)
File /usr/local/lib/python3.9/site-packages/pandas/core/apply.py:871, in FrameApply.apply_series_generator(self)
867 results[i] = self.f(v)
868 if isinstance(results[i], ABCSeries):
869 # If we have a view on v, we need to make a copy because
870 # series_generator will swap out the underlying data
--> 871 results[i] = results[i].copy(deep=False)
873 return results, res_index
TypeError: copy() missing 1 required positional argument: 'self'
Expected Behavior
pd.__version__
'0.24.2'
pd.DataFrame(list(range(10))).apply(lambda x: type(x))
0 <class 'pandas.core.series.Series'>
dtype: object
pd.DataFrame(list(range(10))).apply(lambda x: type(x), axis=1)
0 <class 'pandas.core.series.Series'>
1 <class 'pandas.core.series.Series'>
2 <class 'pandas.core.series.Series'>
3 <class 'pandas.core.series.Series'>
4 <class 'pandas.core.series.Series'>
5 <class 'pandas.core.series.Series'>
6 <class 'pandas.core.series.Series'>
7 <class 'pandas.core.series.Series'>
8 <class 'pandas.core.series.Series'>
9 <class 'pandas.core.series.Series'>
dtype: object
Installed Versions
INSTALLED VERSIONS
commit : 4bfe3d0
python : 3.9.0.final.0
python-bits : 64
OS : Linux
OS-release : 3.10.0-1127.13.1.el7.x86_64
Version : #1 SMP Tue Jun 23 15:46:38 UTC 2020
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.4.2
numpy : 1.19.2
pytz : 2021.3
dateutil : 2.8.2
pip : 20.2.3
setuptools : 50.3.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.7.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.0.1
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : 2022.01.0
gcsfs : None
markupsafe : 2.0.1
matplotlib : 3.5.1
numba : None
numexpr : 2.8.1
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.7.3
snappy : None
sqlalchemy : None
tables : 3.7.0
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : None
zstandard : None