8000 TYP: Typing for ExtensionArray.__getitem__ by Dr-Irv · Pull Request #41258 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

TYP: Typing for ExtensionArray.__getitem__ #41258

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 26 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6c83511
TYP: ExtensionArray.__getitem__
Dr-Irv May 1, 2021
0c8d648
make base class use PositionalIndexer2D
Dr-Irv May 2, 2021
c43eadc
merge with master
Dr-Irv May 16, 2021
94ced76
fix up getitem typing for DateTimeOps
Dr-Irv May 16, 2021
4772f8e
Merge remote-tracking branch 'upstream/master' into extgetitem
Dr-Irv May 23, 2021
2773c8b
Make getitem on EA accept 1D, and change declaration for 2D arrays
Dr-Irv May 23, 2021
b7f2485
Merge branch 'master' into extgetitem
Dr-Irv May 31, 2021
01c0cf5
casting in datetimelike, allow NA in string arrow
Dr-Irv May 31, 2021
3b38de2
fix string arrow NA type
Dr-Irv May 31, 2021
b25d5a3
Merge remote-tracking branch 'upstream/master' into extgetitem
Dr-Irv Jun 13, 2021
d7c545d
change an overload in mixins to use NDArrayBackedExtensionArrayT
Dr-Irv Jun 13, 2021
076e434
categorical returns Any, interval for NA, put back libmissing in stri…
Dr-Irv Jun 14, 2021
01c1a3f
Merge remote-tracking branch 'upstream/master' into extgetitem
Dr-Irv Jul 6, 2021
738ec89
merge with master
Dr-Irv Jul 8, 2021
c5e300c
10000 Merge remote-tracking branch 'upstream/master' into extgetitem
Dr-Irv Jul 8, 2021
1dbb668
change ignore messages
Dr-Irv Jul 8, 2021
3e19841
Merge remote-tracking branch 'upstream/master' into extgetitem
Dr-Irv Jul 14, 2021
73680ac
WIP: merge with master
Dr-Irv Jul 26, 2021
adf3a73
resolve conflicts in core/internals/blocks.py
Dr-Irv Jul 26, 2021
6068976
Merge remote-tracking branch 'upstream/master' into extgetitem
Dr-Irv Jul 28, 2021
0f36e5f
merge with master 0803
Dr-Irv Aug 3, 2021
9557cac
merge with master
Dr-Irv Sep 6, 2021
9a8550d
create types for split of getitem arguments
Dr-Irv Sep 6, 2021
10c454d
merge in delete/searchsorted typing changes
Dr-Irv Sep 6, 2021
a5318bc
merge with astype changes
Dr-Irv Sep 6, 2021
b941732
comments on various indexers in _typing.py
Dr-Irv Sep 6, 2021
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
Next Next commit
casting in datetimelike, allow NA in string arrow
  • Loading branch information
Dr-Irv committed May 31, 2021
commit 01c0cf521a3d4da378415b0a42507ad9c958bcf8
1 change: 1 addition & 0 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
# TODO: add Ellipsis, see
# https://github.com/python/typing/issues/684#issuecomment-548203158
# https://bugs.python.org/issue41810
# Using List[int] here rather than Sequence[int] to disallow tuples.
Copy link
Contributor

Choose a reason for hiding this comment

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

can you followup and give some hints on where these should be used (a.g. a line of description for each; example....)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can you followup and give some hints on where these should be used (a.g. a line of description for each; example....)

Done in a new commit - added comments - let me know if you want more

Copy link
Contributor

Choose a reason for hiding this comment

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

got it, looks good.

PositionalIndexer = Union[int, np.integer, slice, List[int], np.ndarray]
PositionalIndexerTuple = Tuple[PositionalIndexer, PositionalIndexer]
PositionalIndexer2D = Union[PositionalIndexer, PositionalIndexerTuple]
10 changes: 7 additions & 3 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,15 @@ def __getitem__(
This getitem defers to the underlying array, which by-definition can
only handle list-likes, slices, and integer scalars
"""
# Use cast as we know we will get back a DatetimeLikeArray
result = cast(DatetimeLikeArrayT, super().__getitem__(key))
# Use cast as we know we will get back a DatetimeLikeArray or DTScalar
result = cast(
Union[DatetimeLikeArrayT, DTScalarOrNaT], super().__getitem__(key)
)
if lib.is_scalar(result):
return result

else:
# At this point we know the result is an array.
result = cast(DatetimeLikeArrayT, result)
result._freq = self._get_getitem_freq(key)
return result

Expand Down
7 changes: 5 additions & 2 deletions pandas/core/arrays/string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
TYPE_CHECKING,
Any,
Sequence,
Union,
cast,
overload,
)
Expand Down Expand Up @@ -80,6 +81,8 @@
if TYPE_CHECKING:
from pandas import Series

ArrowStringScalarOrNAT = Union[str, libmissing.NA]


@register_extension_dtype
class ArrowStringDtype(StringDtype):
Expand Down Expand Up @@ -344,7 +347,7 @@ def _concat_same_type(cls, to_concat) -> ArrowStringArray:
)

@overload
def __getitem__(self, item: int | np.integer) -> str:
def __getitem__(self, item: int | np.integer) -> ArrowStringScalarOrNAT:
...

@overload
Expand All @@ -355,7 +358,7 @@ def __getitem__(

def __getitem__(
self: ArrowStringArray, item: PositionalIndexer
) -> ArrowStringArray | str:
) -> ArrowStringArray | ArrowStringScalarOrNAT:
"""Select a subset of self.

Parameters
Expand Down
0