REF: move methods into cdef classes
State: closed

A bunch of methods are defined in Timestamp/Timedelta that can be defined in _Timestamp/_Timedelta.  Moving these into the cdef classes is supposedly slightly more efficient.  If we can work around the `__cinit__/__new__` rules, we might be able to get rid of _Timestamp entirely.

Fix a couple of typos while we're at it.

Labels:
name: Refactor
description: Internal refactoring of code
name: Internals
description: Related to non-user accessible pandas implementation
name: Clean

Comments:
iirc u tried this before but was never merged

I don’t think these are actually more efficient (maybe prove me wrong); not sure moving is really that useful here - what is the policy for keeping in the main class vs the subclass? really almost all should be in one or the other (which is the way we have it now)

can u provide some rationale?
> I don’t think these are actually more efficient (maybe prove me wrong)

I'm trusting the cython docs (https://cython.readthedocs.io/en/latest/src/tutorial/cdef_classes.html)

```
They are somewhat restricted compared to Python classes, but are generally more memory efficient and faster than generic Python classes
```

> what is the policy for keeping in the main class vs the subclass? really almost all should be in one or the other (which is the way we have it now)

The policy _should_ be that anything that can be in the cdef class goes in the cdef class.  e.g. `Period` defines only `__new__` and everything else is in `_Period`.  After this PR, `Timestamp` behaves the same way.  `Timedelta` isn't there yet.

> can u provide some rationale?

Besides the slightly-more-performant bit, if we can get everything into the cdef class, then we can change some `import`s to `cimport`s elsewhere in _libs.
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24869?src=pr&el=h1) Report
> Merging [#24869](https://codecov.io/gh/pandas-dev/pandas/pull/24869?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/12bb6d0536b947f74b66ebe18b5f451bfdd7453f?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `n/a`.

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

```diff
@@            Coverage Diff             @@
##           master   #24869      +/-   ##
==========================================
+ Coverage   92.38%   92.39%   +<.01%     
==========================================
  Files         166      166              
  Lines       52412    52412              
==========================================
+ Hits        48423    48424       +1     
+ Misses       3989     3988       -1
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.81% <ø> (ø)` | :arrow_up: |
| #single | `42.89% <ø> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/24869?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/util/testing.py](https://codecov.io/gh/pandas-dev/pandas/pull/24869/diff?src=pr&el=tree#diff-cGFuZGFzL3V0aWwvdGVzdGluZy5weQ==) | `88.13% <0%> (+0.09%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24869?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/24869?src=pr&el=footer). Last update [12bb6d0...fbe9b3f](https://codecov.io/gh/pandas-dev/pandas/pull/24869?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/24869?src=pr&el=h1) Report
> Merging [#24869](https://codecov.io/gh/pandas-dev/pandas/pull/24869?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/12bb6d0536b947f74b66ebe18b5f451bfdd7453f?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `n/a`.

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

```diff
@@            Coverage Diff             @@
##           master   #24869      +/-   ##
==========================================
+ Coverage   92.38%   92.39%   +<.01%     
==========================================
  Files         166      166              
  Lines       52412    52412              
==========================================
+ Hits        48423    48424       +1     
+ Misses       3989     3988       -1
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.81% <ø> (ø)` | :arrow_up: |
| #single | `42.89% <ø> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/24869?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/util/testing.py](https://codecov.io/gh/pandas-dev/pandas/pull/24869/diff?src=pr&el=tree#diff-cGFuZGFzL3V0aWwvdGVzdGluZy5weQ==) | `88.13% <0%> (+0.09%)` | :arrow_up: |

------

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

and does this provide any speedups?
> and does this provide any speedups?

%timeit results are showing very small speedups in `Timestamp.__radd__` and very small slowdown in `td.floor`

```
In [2]: td = pd.Timedelta(hours=25)
In [3]: %timeit td.floor('D')
10000 loops, best of 3: 74.5 µs per loop     # <-- PR
10000 loops, best of 3: 72.6 µs per loop     # <-- master

In [4]: ts = pd.Timestamp.now()
In [5]: %timeit td + ts
100000 loops, best of 3: 2.27 µs per loop  # <-- PR
100000 loops, best of 3: 2.35 µs per loop  # <-- master
```

for both of these benchmarks, the results are consistent across multiple %timeit runs.

I'd speculate that the slowdown for floor is because it has to do a `__mro__` lookup.
> but before moving I would like to understand what you are proposing for the endgame,

The endgame is to have everything in the cdef versions and only have Timestamp, Timedelta, Period, no _Timestamp/_Timedelta/_Period.

It looks like only `__radd__` has a perf improvement, so I'll revert everything except for that (and the typo fixes) for now.  If my `__mro__` guess is correct, most of the rest we don't get any upside until the endgame becomes a reality.