move implementation of Timedelta to tslibs.timedeltas
State: closed

- [ ] closes #xxxx
- [ ] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry


Labels:
name: Internals
description: Related to non-user accessible pandas implementation
name: Clean

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

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

```diff
@@            Coverage Diff             @@
##           master   #18085      +/-   ##
==========================================
- Coverage   91.27%   91.26%   -0.02%     
==========================================
  Files         163      163              
  Lines       50120    50120              
==========================================
- Hits        45749    45740       -9     
- Misses       4371     4380       +9
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `89.07% <ø> (ø)` | :arrow_up: |
| #single | `40.32% <ø> (-0.06%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/18085?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/18085?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `25% <0%> (-58.34%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/18085?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.75% <0%> (-0.1%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/18085?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/18085?src=pr&el=footer). Last update [b4375bd...60c9a9d](https://codecov.io/gh/pandas-dev/pandas/pull/18085?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/18085?src=pr&el=h1) Report
> Merging [#18085](https://codecov.io/gh/pandas-dev/pandas/pull/18085?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/93c755e0ea64b6039c8ecd55fc637d1dd67dbd6e?src=pr&el=desc) will **decrease** coverage by `0.01%`.
> The diff coverage is `86.66%`.

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

```diff
@@            Coverage Diff             @@
##           master   #18085      +/-   ##
==========================================
- Coverage    91.4%   91.39%   -0.02%     
==========================================
  Files         163      163              
  Lines       50073    50134      +61     
==========================================
+ Hits        45769    45819      +50     
- Misses       4304     4315      +11
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `89.2% <86.66%> (ø)` | :arrow_up: |
| #single | `40.33% <40%> (-0.05%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/18085?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/tseries/offsets.py](https://codecov.io/gh/pandas-dev/pandas/pull/18085?src=pr&el=tree#diff-cGFuZGFzL3RzZXJpZXMvb2Zmc2V0cy5weQ==) | `97.15% <100%> (+0.04%)` | :arrow_up: |
| [pandas/core/indexes/datetimelike.py](https://codecov.io/gh/pandas-dev/pandas/pull/18085?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9kYXRldGltZWxpa2UucHk=) | `97.11% <100%> (ø)` | :arrow_up: |
| [pandas/core/resample.py](https://codecov.io/gh/pandas-dev/pandas/pull/18085?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvcmVzYW1wbGUucHk=) | `96.16% <100%> (ø)` | :arrow_up: |
| [pandas/core/indexes/period.py](https://codecov.io/gh/pandas-dev/pandas/pull/18085?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9wZXJpb2QucHk=) | `92.89% <66.66%> (+0.01%)` | :arrow_up: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/18085?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `25% <0%> (-58.34%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/18085?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.8% <0%> (-0.1%)` | :arrow_down: |
| [pandas/core/indexes/datetimes.py](https://codecov.io/gh/pandas-dev/pandas/pull/18085?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9kYXRldGltZXMucHk=) | `95.41% <0%> (-0.1%)` | :arrow_down: |
| [pandas/core/nanops.py](https://codecov.io/gh/pandas-dev/pandas/pull/18085?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvbmFub3BzLnB5) | `96.67% <0%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/18085?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/18085?src=pr&el=footer). Last update [93c755e...f5e8fe0](https://codecov.io/gh/pandas-dev/pandas/pull/18085?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

@jbrockmendel : Minor comments.  Also, let's `asv` to make sure as usual.
Several asv runs:

```
taskset 5 asv continuous -E virtualenv -f 1.1 master HEAD -b timeseries
[...]
       before           after         ratio
     [b4375bde]       [98e339a5]
+     19.7±0.04μs       22.0±0.1μs     1.12  timeseries.Offsets.time_timeseries_day_incr
-     16.5±0.06ms      14.9±0.08ms     0.90  timeseries.Iteration.time_iter_periodindex_preexit
-     7.62±0.01μs      6.81±0.01μs     0.89  timeseries.DatetimeIndex.time_timestamp_tzinfo_cons

       before           after         ratio
     [b4375bde]       [98e339a5]
+     2.53±0.01ms      2.87±0.03ms     1.13  timeseries.AsOf.time_asof_nan_single
+      19.9±0.1μs       22.1±0.1μs     1.11  timeseries.Offsets.time_timeseries_day_incr
-      32.9±0.1μs       28.0±0.1μs     0.85  timeseries.Offsets.time_custom_bday_cal_decr
-        137±10ms         35.8±3ms     0.26  timeseries.DatetimeIndex.time_dti_tz_factorize
-        174±30ms         36.8±3ms     0.21  timeseries.DatetimeIndex.time_dti_factorize

       before           after         ratio
     [b4375bde]       [98e339a5]
+         429±6ms            2.56s     5.96  timeseries.AsOfDataFrame.time_asof_nan
+        408±10ms            2.30s     5.64  timeseries.AsOfDataFrame.time_asof
-     56.3±0.05μs      50.6±0.08μs     0.90  timeseries.SemiMonthOffset.time_begin_decr
-     7.26±0.08μs      6.51±0.02μs     0.90  timeseries.DatetimeIndex.time_timestamp_tzinfo_cons
-     21.9±0.09μs       18.7±0.1μs     0.86  timeseries.Offsets.time_custom_bday_apply_dt64
-        27.0±2ms       6.64±0.2ms     0.25  timeseries.DatetimeIndex.time_dti_tz_factorize
-        30.1±4ms       7.17±0.1ms     0.24  timeseries.DatetimeIndex.time_dti_factorize

       before           after         ratio
     [b4375bde]       [98e339a5]
+      17.1±0.2μs      20.1±0.08μs     1.18  timeseries.Offsets.time_custom_bday_apply_dt64
+      27.5±0.1μs       32.1±0.2μs     1.17  timeseries.Offsets.time_custom_bday_decr
+      18.2±0.2μs      20.1±0.06μs     1.11  timeseries.Offsets.time_timeseries_day_apply
-      20.6±0.1μs      18.7±0.06μs     0.91  timeseries.Offsets.time_custom_bday_incr
-     7.39±0.02μs      6.71±0.02μs     0.91  timeseries.DatetimeIndex.time_timestamp_tzinfo_cons
-     31.1±0.07μs       27.4±0.1μs     0.88  timeseries.Offsets.time_custom_bday_cal_incr_neg_n
-        2.77±0ms         1.68±0ms     0.61  timeseries.DatetimeIndex.time_add_timedelta
-         220±6ms       27.5±0.7ms     0.13  timeseries.AsOfDataFrame.time_asof_nan
-         205±3ms      24.2±0.05ms     0.12  timeseries.AsOfDataFrame.time_asof

      before           after         ratio
     [b4375bde]       [98e339a5]
+     2.72±0.01ms      3.31±0.09ms     1.22  timeseries.ToDatetime.time_iso8601_nosep
+     22.3±0.07μs       24.7±0.1μs     1.11  timeseries.AsOf.time_asof_single
+     2.50±0.03ms      2.75±0.02ms     1.10  timeseries.AsOf.time_asof_nan_single
-         443±3ms          397±2ms     0.90  timeseries.SemiMonthOffset.time_begin_incr_rng
-      8.65±0.1μs      7.72±0.04μs     0.89  timeseries.Offsets.time_timeseries_year_apply
-      7.80±0.3μs      6.95±0.03μs     0.89  timeseries.DatetimeIndex.time_timestamp_tzinfo_cons
```
you have some wild variability. pls run for timedeltas too.
> you have some wild variability

This is a truthfact.

> pls run for timedeltas too

```
asv continuous -f 1.1 -E virtualenv master HEAD -b timedelta
[...]
BENCHMARKS NOT SIGNIFICANTLY CHANGED

asv continuous -f 1.1 -E virtualenv master HEAD -b timedelta
[...]
       before           after         ratio
     [b4375bde]       [98e339a5]
+        59.2±7ms          120±6ms     2.03  inference.DtypeInfer.time_timedelta64_2

asv continuous -f 1.1 -E virtualenv master HEAD -b timedelta
[...]
BENCHMARKS NOT SIGNIFICANTLY CHANGED.

asv continuous -f 1.1 -E virtualenv master HEAD -b timedelta
[...]
       before           after         ratio
     [b4375bde]       [98e339a5]
-         106±2ms         84.4±2ms     0.80  inference.DtypeInfer.time_timedelta64_1

```
ok so it doesn't change perf, which is expected, good.
I think you need to add timedeltas.pxd to period in setup.py; I don't think you can remove anything from tslib def (but check). rebase.
If we can push this through we can port most of Timestamp to its own module.  Along with #18086 we can then get the rest.
> fix this too

Get rid of the matching one in `tslib` while we're at it?
thanks @jbrockmendel making nice progress!
rebase any prs that touch this code
Good teamwork; this was a big one.