Datetimelike __setitem__
State: closed

Split from #24024.

This is mostly just a move from PeriodArray to DatetimelikeArray, with period-specific things replaced with `_check_compatible_with`, `_unbox_scalar`, etc.

This is under-tested at the moment, just the basics are actually hit. But I think this is OK for two reasons

1. I'll immediately rebase #24024 with these changes, so we'll get the setitem tests shortly after merging (and can revert / whatever as needed)
2. We do run the full suite of tests for PeriodArray, and any type-specific stuff *should* be hit currently with the tests here (`_check_compatible_with`, freq invalidation, etc.).

Labels:
name: Datetime
description: Datetime data dtype
name: ExtensionArray
description: Extending pandas with custom dtypes or arrays.

Comments:
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24477?src=pr&el=h1) Report
> Merging [#24477](https://codecov.io/gh/pandas-dev/pandas/pull/24477?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/80295f9483176b1335226680f1b55f09ded9ad46?src=pr&el=desc) will **decrease** coverage by `49.26%`.
> The diff coverage is `15.38%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/24477/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/24477?src=pr&el=tree)

```diff
@@             Coverage Diff             @@
##           master   #24477       +/-   ##
===========================================
- Coverage   92.32%   43.06%   -49.27%     
===========================================
  Files         166      166               
  Lines       52298    52303        +5     
===========================================
- Hits        48285    22524    -25761     
- Misses       4013    29779    +25766
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `?` | |
| #single | `43.06% <15.38%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/24477?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/arrays/period.py](https://codecov.io/gh/pandas-dev/pandas/pull/24477/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYXJyYXlzL3BlcmlvZC5weQ==) | `40.96% <ø> (-57.52%)` | :arrow_down: |
| [pandas/core/arrays/timedeltas.py](https://codecov.io/gh/pandas-dev/pandas/pull/24477/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYXJyYXlzL3RpbWVkZWx0YXMucHk=) | `40.29% <50%> (-47.34%)` | :arrow_down: |
| [pandas/core/arrays/datetimes.py](https://codecov.io/gh/pandas-dev/pandas/pull/24477/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYXJyYXlzL2RhdGV0aW1lcy5weQ==) | `65.1% <50%> (-33.16%)` | :arrow_down: |
| [pandas/core/arrays/datetimelike.py](https://codecov.io/gh/pandas-dev/pandas/pull/24477/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYXJyYXlzL2RhdGV0aW1lbGlrZS5weQ==) | `46.21% <9.09%> (-49.45%)` | :arrow_down: |
| [pandas/io/formats/latex.py](https://codecov.io/gh/pandas-dev/pandas/pull/24477/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvbGF0ZXgucHk=) | `0% <0%> (-100%)` | :arrow_down: |
| [pandas/core/categorical.py](https://codecov.io/gh/pandas-dev/pandas/pull/24477/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvY2F0ZWdvcmljYWwucHk=) | `0% <0%> (-100%)` | :arrow_down: |
| [pandas/io/sas/sas\_constants.py](https://codecov.io/gh/pandas-dev/pandas/pull/24477/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL3Nhcy9zYXNfY29uc3RhbnRzLnB5) | `0% <0%> (-100%)` | :arrow_down: |
| [pandas/tseries/plotting.py](https://codecov.io/gh/pandas-dev/pandas/pull/24477/diff?src=pr&el=tree#diff-cGFuZGFzL3RzZXJpZXMvcGxvdHRpbmcucHk=) | `0% <0%> (-100%)` | :arrow_down: |
| [pandas/tseries/converter.py](https://codecov.io/gh/pandas-dev/pandas/pull/24477/diff?src=pr&el=tree#diff-cGFuZGFzL3RzZXJpZXMvY29udmVydGVyLnB5) | `0% <0%> (-100%)` | :arrow_down: |
| [pandas/io/formats/html.py](https://codecov.io/gh/pandas-dev/pandas/pull/24477/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvaHRtbC5weQ==) | `0% <0%> (-98.65%)` | :arrow_down: |
| ... and [128 more](https://codecov.io/gh/pandas-dev/pandas/pull/24477/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24477?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24477?src=pr&el=footer). Last update [80295f9...2dc85b7](https://codecov.io/gh/pandas-dev/pandas/pull/24477?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24477?src=pr&el=h1) Report
> Merging [#24477](https://codecov.io/gh/pandas-dev/pandas/pull/24477?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/80295f9483176b1335226680f1b55f09ded9ad46?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/24477/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/24477?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #24477      +/-   ##
==========================================
+ Coverage   92.32%   92.32%   +<.01%     
==========================================
  Files         166      166              
  Lines       52298    52306       +8     
==========================================
+ Hits        48285    48294       +9     
+ Misses       4013     4012       -1
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.75% <100%> (ø)` | :arrow_up: |
| #single | `43.06% <17.24%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/24477?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/arrays/period.py](https://codecov.io/gh/pandas-dev/pandas/pull/24477/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYXJyYXlzL3BlcmlvZC5weQ==) | `98.39% <ø> (-0.08%)` | :arrow_down: |
| [pandas/core/arrays/datetimelike.py](https://codecov.io/gh/pandas-dev/pandas/pull/24477/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYXJyYXlzL2RhdGV0aW1lbGlrZS5weQ==) | `95.84% <100%> (+0.19%)` | :arrow_up: |
| [pandas/core/indexes/datetimelike.py](https://codecov.io/gh/pandas-dev/pandas/pull/24477/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9kYXRldGltZWxpa2UucHk=) | `97.53% <100%> (ø)` | :arrow_up: |
| [pandas/core/arrays/timedeltas.py](https://codecov.io/gh/pandas-dev/pandas/pull/24477/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYXJyYXlzL3RpbWVkZWx0YXMucHk=) | `87.68% <100%> (+0.05%)` | :arrow_up: |
| [pandas/core/arrays/datetimes.py](https://codecov.io/gh/pandas-dev/pandas/pull/24477/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYXJyYXlzL2RhdGV0aW1lcy5weQ==) | `98.43% <100%> (+0.17%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24477?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24477?src=pr&el=footer). Last update [80295f9...e1b7d1d](https://codecov.io/gh/pandas-dev/pandas/pull/24477?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

Tested and fixed in 2dc85b7b1fd1483a74998f1f08e5901446c9bd54.


On Fri, Dec 28, 2018 at 8:49 PM Jeff Reback <notifications@github.com>
wrote:

> *@jreback* commented on this pull request.
> ------------------------------
>
> In pandas/core/arrays/datetimelike.py
> <https://github.com/pandas-dev/pandas/pull/24477#discussion_r244444532>:
>
> > +    def __setitem__(
> +            self,
> +            key,    # type: Union[int, Sequence[int], Sequence[bool], slice]
> +            value,  # type: Union[NaTType, Scalar, Sequence[Scalar]]
> +    ):
> +        # type: (...) -> None
> +        # I'm fudging the types a bit here. The "Scalar" above really depends
> +        # on type(self). For PeriodArray, it's Period (or stuff coercible
> +        # to a period in from_sequence). For DatetimeArray, it's Timestamp...
> +        # I don't know if mypy can do that, possibly with Generics.
> +        # https://mypy.readthedocs.io/en/latest/generics.html
> +
> +        if is_list_like(value):
> +            is_slice = isinstance(key, slice)
> +            if (not is_slice
> +                    and len(key) != len(value)
>
> key could be a scalar here right? (in which case u will get an odd
> exception about len of unsized object)
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/pandas-dev/pandas/pull/24477#pullrequestreview-188392118>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/ABQHIng3dwWdFOZL3dAEkKHtBC6vT-bNks5u9thUgaJpZM4ZkX7a>
> .
>

All green.
thanks!