-
-
Notifications
You must be signed in to change notification settings - Fork 146
Fix series.map overloads #960
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3276,6 +3276,7 @@ def test_map_na() -> None: | |
|
||
mapping = {1: "a", 2: "b", 3: "c"} | ||
check(assert_type(s.map(mapping, na_action=None), "pd.Series[str]"), pd.Series, str) | ||
check(assert_type(s.map(mapping), "pd.Series[str]"), pd.Series, str) | ||
|
||
def callable(x: int | NAType) -> str | NAType: | ||
if isinstance(x, int): | ||
|
@@ -3285,9 +3286,45 @@ def callable(x: int | NAType) -> str | NAType: | |
check( | ||
assert_type(s.map(callable, na_action=None), "pd.Series[str]"), pd.Series, str | ||
) | ||
check(assert_type(s.map(callable), "pd.Series[str]"), pd.Series, str) | ||
|
||
series = pd.Series(["a", "b", "c"]) | ||
check(assert_type(s.map(series, na_action=None), "pd.Series[str]"), pd.Series, str) | ||
check(assert_type(s.map(series), "pd.Series[str]"), pd.Series, str) | ||
|
||
s2: pd.Series[float] = pd.Series([1.0, pd.NA, 3.0]) | ||
|
||
def callable2(x: float) -> float: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Float (and int, complex, bool) callables pass |
||
return x + 1 | ||
|
||
check( | ||
assert_type(s2.map(callable2, na_action="ignore"), "pd.Series[float]"), | ||
pd.Series, | ||
float, | ||
) | ||
check( | ||
assert_type(s2.map(callable2), "pd.Series[float]"), | ||
pd.Series, | ||
float, | ||
) | ||
if TYPE_CHECKING_INVALID_USAGE: | ||
s2.map(callable2, na_action=None) # type: ignore[arg-type] # pyright: ignore[reportCallIssue, reportArgumentType] | ||
|
||
s3: pd.Series[str] = pd.Series(["A", pd.NA, "C"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar comment. Use |
||
|
||
def callable3(x: str) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-numerical functions pass only the explicit |
||
return x.lower() | ||
|
||
check( | ||
assert_type(s3.map(callable3, na_action="ignore"), "pd.Series[str]"), | ||
pd.Series, | ||
str, | ||
) | ||
if TYPE_CHECKING_INVALID_USAGE: | ||
s3.map( | ||
callable3, na_action=None # type: ignore[arg-type] # pyright: ignore[reportCallIssue, reportArgumentType] | ||
) | ||
s3.map(callable3) # type: ignore[type-var] # pyright: ignore[reportCallIssue, reportArgumentType] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little concerned that this won't pass the type checker. A user should be able to do: s = pd.Series({"a", "b", "c"], dtype=str)
s.map(callable3) That will execute correctly and should pass the type checker. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately i dont think the type system supports something like this at the moment. All that If the user does know more than the type system he could either specify There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand your point of view. But the issue here is that we need to support the "normal" usage of pandas. The type system can't keep track of what is inside of a The default of the We've had other cases where we were too restrictive in the stubs, and people complained, so we had to pull back. See https://github.com/pandas-dev/pandas-stubs/blob/main/docs/philosophy.md#narrow-vs-wide-arguments for a discussion of this issue. IMHO, you're proposing that the stubs will be too narrow. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, i think im just too much of a typing purist in this case. Because, just because your code doesnt cause runtime errors with the data that you are testing it with, doesnt mean that it isnt unsound from a typing perspective. Normally every interaction should be proveably valid just from the involved signatures. And i am using a type checker to actually ensure this. But i guess that is not fully possible anyway with the setup of pandas. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I agree. We've chosen a middle ground with the stubs that supports the common ways people use pandas, but doesn't necessarily support all possible best typing practices. |
||
|
||
|
||
def test_case_when() -> None: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally don't use the annotations in the tests. So you can do
Then the type checkers should recognize
s2
asSeries[float]
Note that internally,
pd.Series([1.0, pd.NA, 3.0])
has dtypeobject
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This first actually gives me an error
TypeError: float() argument must be a string or a real number, not 'NAType'
. While the second version does work, i cant get it to work for the integer case ofs = pd.Series([1, pd.NA, 3])
. While that does work at the construction point and for inferrings: Series[int]
, the values that are passed intocallable
ats.map(callable)
are actually floats.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For
Series[int]
, you can uses = pd.Series([1, pd.NA, 2], dtype="Int64")
Sorry about my error on the
dtype=float
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually still gives me this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a known pandas issue. See pandas-dev/pandas#57189