Pandas version checks

  • [X] I have checked that this issue has not already been reported.

  • [X] 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 : 2e218d10984e9919f0296931d92ea851c6a6faf5 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

Comment From: phofl

Please post a reproducible example

Comment From: alexprincel

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

Comment From: Ali-Flt

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))