Skip to content

BUG: apply on DataFrame results in TypeError: copy() missing 1 required positional argument: 'self' #46684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 of 3 tasks
emsi opened this issue Apr 7, 2022 · 2 comments · Fixed by #46719
Closed
2 of 3 tasks
Labels
Apply Apply, Aggregate, Transform, Map Bug Internals Related to non-user accessible pandas implementation Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@emsi
Copy link

emsi commented Apr 7, 2022

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

@emsi emsi added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 7, 2022
@rhshadrach
Copy link
Member

Thanks for the report! This is due to

from pandas.core.dtypes.generic import ABCSeries
print(isinstance(type(pd.Series(dtype=object)), ABCSeries))

resulting in True. This can be fixed by modifying pandas.core.dtypes.generic.create_pandas_abc_type to make sure that inst is not a type, i.e. not isinstance(inst, type).

@emsi - would you be interested in putting up a PR to fix?

@rhshadrach rhshadrach added Regression Functionality that used to work in a prior pandas version Typing type annotations, mypy/pyright type checking and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 8, 2022
@rhshadrach rhshadrach modified the milestones: Contributions Welcome, 1.4.3 Apr 8, 2022
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Apr 8, 2022
@simonjayhawkins
Copy link
Member

Both of above lines produce errors now while they used to work in the past:

first bad commit: [91802a9] PERF: avoid creating many Series in apply_standard (#34909)

so regression is between 1.0.5 and 1.1.0

@simonjayhawkins simonjayhawkins modified the milestones: 1.4.3, Contributions Welcome Apr 8, 2022
@rhshadrach rhshadrach added Internals Related to non-user accessible pandas implementation Apply Apply, Aggregate, Transform, Map and removed Typing type annotations, mypy/pyright type checking labels Apr 9, 2022
@rhshadrach rhshadrach modified the milestones: Contributions Welcome, 1.5 Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Bug Internals Related to non-user accessible pandas implementation Regression Functionality that used to work in a prior pandas version
Projects
None yet
3 participants