Skip to content

BUG: running apply on a cell with NoneType returns error. #53105

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
Ali-Flt opened this issue May 5, 2023 · 3 comments
Closed
2 of 3 tasks

BUG: running apply on a cell with NoneType returns error. #53105

Ali-Flt opened this issue May 5, 2023 · 3 comments
Labels
Bug Needs Info Clarification about behavior needed to assess issue

Comments

@Ali-Flt
Copy link

Ali-Flt commented May 5, 2023

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

import pandas as pd
pre_df = pd.DataFrame(...)

pre_df['row_with_nonetype'].apply(lambda x: func(x))

Issue Description

Because apply is checking the len(s) here:

....pandas/core/apply.py:1123), in SeriesApply.apply(self)
   1120     return self.apply_str()
   1122 # self.f is Callable
...
--> 907     index = default_index(len(s))
    909 if id(index) in indexer_cache:
    910     indexer = indexer_cache[id(index)]

when s is NoneType it returns error.

Expected Behavior

No error

Installed Versions

INSTALLED VERSIONS

commit : 2e218d1
python : 3.8.16.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-146-generic
Version : #163-Ubuntu SMP Fri Mar 17 18:26:02 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.3
numpy : 1.24.2
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 67.6.0
pip : 23.0.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.11.0
pandas_datareader: None
bs4 : 4.11.2
bottleneck : None
brotli :
fastparquet : None
fsspec : 2023.3.0
gcsfs : None
matplotlib : 3.7.1
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 11.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@Ali-Flt Ali-Flt added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 5, 2023
@phofl
Copy link
Member

phofl commented May 5, 2023

Please post a reproducible example

@mroeschke mroeschke added Needs Info Clarification about behavior needed to assess issue and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 5, 2023
@alexprincel
Copy link
Contributor

Adding the exact error message observed would be helpful as well.

@Ali-Flt
Copy link
Author

Ali-Flt commented May 8, 2023

I apologize,
I believe my mistake was that I was trying to write a pandas Series into a pandas DataFrame's cell and the error put me off. The scenario was something like this:

def func(x):
    try:
        return x[1]
    except Exception:
        return x
df['new_col] = df['old_col']apply(lambda x: func(x))

Where df['old_col'] had pd.Series() in its cells, some of which were a pd.Series() with a single element = None and some others were a pd.Series() with a single element = tuple(...).
I used .item() instead and it worked:

def func(x):
    try:
        return x.item()[1]
    except Exception:
        try:
            return x[1]
        except Exception:
            try:
                return x.item()
            except Exception:
                return x
df['new_col] = df['old_col']apply(lambda x: func(x))

@Ali-Flt Ali-Flt closed this as completed May 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Info Clarification about behavior needed to assess issue
Projects
None yet
Development

No branches or pull requests

4 participants