Skip to content

API: Return BoolArray for string ops when backed by StringArray #30239

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

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
types
  • Loading branch information
TomAugspurger committed Dec 12, 2019
commit b83d677048ec550dff52e61a51abf068feb226d4
3 changes: 2 additions & 1 deletion pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from functools import wraps
import re
import textwrap
from typing import TYPE_CHECKING, Any, Callable, Dict, List
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Type, Union
import warnings

import numpy as np
Expand Down Expand Up @@ -158,6 +158,7 @@ def _map_stringarray(
arr = np.asarray(arr)

if is_integer_dtype(dtype) or is_bool_dtype(dtype):
constructor: Union[Type[IntegerArray], Type[BooleanArray]]
Copy link
Member

@WillAyd WillAyd Dec 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
constructor: Union[Type[IntegerArray], Type[BooleanArray]]
constructor: Type[Union[IntegerArray, BooleanArray]]

Optional but less verbose if you put the Union inside of the Type

if is_integer_dtype(dtype):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MyPy apparently doesn't like this...

pandas/core/strings.py:164: error: Incompatible types in assignment (expression has type "Type[BooleanArray]", variable has type "Type[IntegerArray]")

Any suggestions on how to please the type checker?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea if you have assignment in an if...else block the type is inferred from the first one that appears.

So before the block you can just declare constructor: Type[Union[IntegerArray, BooleanArray]] or maybe even something simpler like constructor: Type[ExtensionArray] depending on what is valid

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm, I think I got it.

constructor = IntegerArray
else:
Expand Down