-
-
Notifications
You must be signed in to change notification settings - Fork 128
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
Series.map()
needs type for arg
and result
#941
Comments
Solution is to do something like this: def map(self, arg:dict[S1, S2], na_action: Literal["ignore"] | None = ...) -> Series[S2]: ... where This happens with pyright defaults - no "strict" needed. However, the PR with tests welcome |
Will try to have a look if i find the time. The pandas internal signature is I am also not 100% sure how to handle Nominally the function version would have to be something like
Overall it should be:
|
The internal types are meant to check the pandas code, not user code. So they are wider than what we use in the stubs.
Neither am I. You could try what you propose, or just leave |
The changes in #942 have caused a type error in our code - I'm not sure the types are quite correct. Our code is along the lines of: df = pd.read_sql_query("SELECT x, y, z FROM table", dtype={"x": "object"})
df["x"] = df["x"].map({1: True, 0: False, None: None}) The code does work in Pandas but the TypeVar in the |
The issue here is that the update to Solution that should work is to add this overload: @overload
def map(
self,
arg: Callable[[Any], Any] | Mapping[Any, Any] | Series[Any],
na_action: Literal["ignore"] | None = ...,
) -> Series[Any]: ... And a test something like this: df = pd.DataFrame({"x": [1, 0, None]})
check(assert_type( df["x"].map({1: True, 0: False, None: None}), pd.Series), pd.Series) PR welcome. |
Specifically i am trying to typehint this function:
There are 2 issues here. The type hint for series.map says that the return type (parameter) is the same as the initial one, which is not the case here (but it still works when running it).
The other is that the type hint does not specify the argument type.
Which results in the classic unknown warning.
Is there anything that i can currently do to adress this or is there just no way with the stubs and pyright strict mode.
Originally posted by @JanEricNitschke in #940
The text was updated successfully, but these errors were encountered: