Skip to content

Boolean string methods on empty Series return object dtype #29624

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
TomAugspurger opened this issue Nov 14, 2019 · 2 comments · Fixed by #29680
Closed

Boolean string methods on empty Series return object dtype #29624

TomAugspurger opened this issue Nov 14, 2019 · 2 comments · Fixed by #29680
Labels
Strings String extension data type and string data
Milestone

Comments

@TomAugspurger
Copy link
Contributor

Code Sample, a copy-pastable example if possible

In [7]: pd.Series([], dtype=object).str.isalnum()
Out[7]: Series([], dtype: object)

Problem description

On empty series, we return an object-dtype result, rather than bool. I might expect
this to be a boolean dtype to match the non-empty case.

Expected Output

In [14]: pd.Series([], dtype=object).str.isalnum()
Out[14]: Series([], dtype: bool)
@TomAugspurger TomAugspurger added the Strings String extension data type and string data label Nov 14, 2019
@TomAugspurger
Copy link
Contributor Author

Given that other boolean-returning methods do return a bool dtype for empty Series, I'm comfortable calling this a bug:

In [5]: pd.Series([], dtype=object).str.contains("a")
Out[5]: Series([], dtype: bool)

@ellequelle
Copy link
Contributor

It looks to me like this can be solved by adding dtype=bool as an argument to _noarg_wrapper for the "ismethods" the StringMethods class:

isalnum = _noarg_wrapper(
    lambda x: x.isalnum(),
    name="isalnum",
    docstring=_shared_docs["ismethods"] % _doc_args["isalnum"],
    returns_string=False,
    dtype=bool,
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Strings String extension data type and string data
Projects
None yet
3 participants