Skip to content

BUG: str.split("") gives extra leading and trailing elements #60065

Open
@NickCrews

Description

@NickCrews

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

vals = ["foo", "", None]
pd.Series(vals).str.split("").tolist()
# actual: [['', 'f', 'o', 'o', ''], ['', ''], None]
# expected option A, see below: [['f', 'o', 'o'], [], None]
# expected option B, see below: [['f', 'o', 'o'], [''], None]

Issue Description

While I think the current behavior is definitely wrong, I'm not sure if expected behavior should be A or B from above. Here is a brief survey of what peers do:

  • Using vanilla python, eg "foo".split(""), gives an error (I should use list("foo") instead)
  • In duckdb, STR_SPLIT(vals, '') gives [['f', 'o', 'o'], [''], None]
  • in polars, vals.str.split("") gives [['f', 'o', 'o'], [], None]

See duckdb/duckdb#14414 for a discussion of pros and cons of both.

Expected Behavior

In the above linked duckdb issue I argue for why behavior A I think is best.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.11.6
python-bits : 64
OS : Darwin
OS-release : 23.6.0
Version : Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:30 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 2.2.3
numpy : 2.1.2
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 23.2.1
Cython : None
sphinx : None
IPython : 8.28.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.9.0
html5lib : None
hypothesis : 6.115.0
gcsfs : 2024.9.0post1
jinja2 : 3.1.4
lxml.etree : None
matplotlib : 3.9.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : 2.9.9
pymysql : 1.4.6
pyarrow : 17.0.0
pyreadstat : None
pytest : 8.3.3
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.1
sqlalchemy : 2.0.35
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
xlsxwriter : None
zstandard : 0.23.0
tzdata : 2024.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugClosing CandidateMay be closeable, needs more eyeballsStringsString extension data type and string data

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions