Skip to content

No type hint for Series containing lists of strings in Series.str.split method with expand=False #1074

Closed
@pan-vlados

Description

@pan-vlados

Describe the bug

The stubs for pandas_stubs.core.strings.StringMethods.split do not return pandas.Series[list[str]] type with expand=False parameter for Series/Index containing lists of strings as described in docs.

To Reproduce

Use VS Code with Pylance extension:

  1. The following:
    from pandas import Series
    s = Series(["one", "two"])
    def split(s: "Series[str]") -> "Series[list[str]]":  # error
        return s.str.split(expand=False)
    split(s)
    cause Pylance error below:
    Type "list[str]" cannot be assigned to type variable "S1@Series"
    Type "list[str]" is not assignable to upper bound "Dtype | bytes | date | time | bool | int | float | complex | datetime | timedelta | Period | Interval[Unknown] | CategoricalDtype | BaseOffset" for type variable "S1@Series"
        Type "list[str]" is not assignable to type "Dtype | bytes | date | time | bool | int | float | complex | datetime | timedelta | Period | Interval[Unknown] | CategoricalDtype | BaseOffset"
        "list[str]" is not assignable to "str"
        "list[str]" is not assignable to "bytes"
        "list[str]" is not assignable to "date"
        "list[str]" is not assignable to "time"
        "list[str]" is not assignable to "bool"
        "list[str]" is not assignable to "int"
    ...PylancereportInvalidTypeArguments
    
  2. The following is type of "Series[Unknown]":
    from pandas import Series
    s = Series(["one", "two"])
    s = s.str.split(expand=False)
    reveal_type(s)  # Type of "s" is "Series[Unknown]"

Please complete the following information:

  • OS : MacOS
  • OS Version : Sonoma 14.7.1
  • python : 3.10.14
  • processor : arm64
  • VS Code : 1.96.0 (Universal)
  • pandas : 2.2.3
  • pandas-stubs : 2.2.3.241126
  • Pylance : 2024.12.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions