Skip to content

BUG: Series index parameter should accept Sequence[str] (or more general Sequence[Optional[Hashable]] #82

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
bashtage opened this issue Jul 4, 2022 · 2 comments
Labels
Index Related to the Index class or subclasses Needs Info Clarification about behavior needed to assess issue Series Series data structure

Comments

@bashtage
Copy link
Contributor

bashtage commented Jul 4, 2022

import pandas as pd
from typing import Sequence

def f(i: Sequence[str]):
    return pd.Series([1,2,3],index=i)

Error is

exp.py: note: In function "f":
exp.py:4: error: Function is missing a return type annotation
exp.py:5: error: No overload variant of "Series" matches argument types "List[int]", "Sequence[str]"
exp.py:5: note: Possible overload variants:
exp.py:5: note:     def [S1 in (str, bytes, date, datetime, timedelta, bool, int, float, complex, Timestamp, Timedelta, datetime64)] Series(cls, data: DatetimeIndex, index: Union[str, int, Series[Any], List[Any], Index] = ..., dtype: Any = ..., name: Optional[Hashable] = ..., copy: bool = ..., fastpath: bool = ...) -> TimestampSeries
exp.py:5: note:     def [S1 in (str, bytes, date, datetime, timedelta, bool, int, float, complex, Timestamp, Timedelta, datetime64)] Series(cls, data: object, dtype: Type[S1], index: Union[str, int, Series[Any], List[Any], Index] = ..., name: Optional[Hashable] = ..., copy: bool = ..., fastpath: bool = ...) -> Series[S1]
exp.py:5: note:     def [S1 in (str, bytes, date, datetime, timedelta, bool, int, float, complex, Timestamp, Timedelta, datetime64)] Series(cls, data: object = ..., index: Union[str, int, Series[Any], List[Any], Index] = ..., dtype: Any = ..., name: Optional[Hashable] = ..., copy: bool = ..., fastpath: bool = ...) -> Series[Any]
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jul 4, 2022

Can't accept Sequence[str] because a str will match, and you can't write pd.Series([1,2,3], index="abc")

So in your example, f("abc") would pass the type checker on the call to f, but then pandas will not accept the argument at runtime.

Open to other suggestions, but I can't accept your suggested change.

@Dr-Irv Dr-Irv added Needs Info Clarification about behavior needed to assess issue Index Related to the Index class or subclasses Series Series data structure labels Jul 4, 2022
@bashtage
Copy link
Contributor Author

Going to close as I think the Sequence[str] issue is resolved as not possible. Need concrete container types due to the unfortunate properties of str being a Sequence.

@bashtage bashtage closed this as not planned Won't fix, can't repro, duplicate, stale Jul 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Index Related to the Index class or subclasses Needs Info Clarification about behavior needed to assess issue Series Series data structure
Projects
None yet
Development

No branches or pull requests

2 participants