-
-
Notifications
You must be signed in to change notification settings - Fork 133
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
Allow Iterable
and Sequence
for index
and columns
parameters
#790
Comments
No. Because we don't want to accept plain strings or sets as arguments, both of which would match your use of So I think the type of your function is too wide - you are allowing arguments that are not accepted by pandas. Side note - this is fixed for I'm going to close this because I believe the behavior of the stubs is correct here, but if you can provide an example that works with pandas with direct arguments to pandas functions, I'm willing to reopen it. |
Ah, of course. I completely agree that import pandas as pd
def foo(data: list[int], d: dict[str, int]) -> pd.Series:
s = pd.Series(data, index=d.keys())
return s
if __name__ == '__main__':
data =[1, 2, 3]
d = {str(value): value for value in data}
print(foo(data, d)) But foo.py:5: error: No overload variant of "Series" matches argument types "list[int]", "dict_keys[str, int]" [call-overload]
Ah, great. I'm glad my initial report was at least indirectly helpful :) |
While they work, the docs say "arraylike" and |
Got it. Thanks! |
Since the import numpy.typing as npt
import pandas as pd
def foo(data: list[int], idx: npt.ArrayLike) -> pd.Series:
s = pd.Series(data, index=idx)
return s
print(foo([1, 2, 3], ["a", "b", "c"])) This runs, but
I could, of course, define a project-specific from typing import TypeAlias
import numpy as np
import numpy.typing as npt
import pandas as pd
ArrayLike: TypeAlias = list[str] | tuple[str, ...] | np.ndarray | pd.Index | pd.Series
def foo(
data: list[int],
idx: ArrayLike,
) -> pd.Series:
s = pd.Series(data, index=idx)
return s
print(foo([1, 2, 3], ["a", "b", "c"])) Is that what you'd advise? Thanks again! |
The following would work: import pandas as pd
from pandas._typing import Axes
def foo(
data: list[int],
idx: Axes,
) -> pd.Series:
s = pd.Series(data, index=idx)
return s
print(foo([1, 2, 3], ["a", "b", "c"])) The |
OK, great. Thanks again for all your help. |
Describe the bug
mypy
complains whenindex
andcolumns
parameters are passedIterable
orSequence
, but these are valid.To Reproduce
pandas
example that is not properly checked by the stubs.mypy
orpyright
).mypy
Please complete the following information:
mypy
1.5.1pandas-stubs
: 2.1.1.230928Additional context
Would it be appropriate to update
index: Axes | None
(e.g., here) to something likeindex: Axes | Iterable | Sequence | None
?The text was updated successfully, but these errors were encountered: