Simplify Timedelta init, standardize overflow errors
State: closed

- [ ] closes #xxxx (Replace xxxx with the Github issue number)
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.

Per https://github.com/pandas-dev/pandas/pull/46936#issuecomment-1122981776, this PR extracts the functional updates in #46936, to simplify review. The main motivations are those explained in https://github.com/pandas-dev/pandas/pull/46936#discussion_r869761730:

> - DRY up `Timedelta` creation logic, which was divided and duplicated over several functions
> - Do so outside `Timdelta.__new__`, since (AFAICT) there shouldn't yet be a public API for creating instances with non-nano resolution
> - Add overflow checks, and consistently raise `OutOfBoundsTimedelta` when it's detected.

As a bonus, this appears to speed things up considerably, especially in the case of passing `int`s (by avoiding the unnecessary `int -> td64[ns] -> int` conversion):

```
       before           after         ratio
     [a853022e]       [d4444404]
     <main>           <td-construction>
-      8.41±0.8μs       7.50±0.2μs     0.89  tslibs.timedelta.TimedeltaConstructor.time_from_unit
-      7.99±0.3μs       4.42±0.1μs     0.55  tslibs.timedelta.TimedeltaConstructor.time_from_string
-     2.80±0.03μs         758±40ns     0.27  tslibs.timedelta.TimedeltaConstructor.time_from_missing
-      6.88±0.3μs       1.67±0.2μs     0.24  tslibs.timedelta.TimedeltaConstructor.time_from_int
```

I've left out all the test suite updates from #46936, save for those changes associated with `s/OverflowError/OutOfBoundsTimedelta`.

/cc @jbrockmendel @jreback

Labels:
name: Refactor
description: Internal refactoring of code
name: Timedelta
description: Timedelta data type
name: Stale

Comments:
Hello @patrickmckenna! Thanks for updating this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:















There are currently no PEP 8 issues detected in this Pull Request. Cheers! :beers: 

##### Comment last updated at 2022-05-24 20:22:07 UTC
I'm a little confused by the errors in the "Minimum Versions" check. (The others errors are also confuse me, but appear to be issues in the CI setup phases.)

The last commit resulted in one less test failure: [`test_construction_out_of_bounds_td64[3508-M- months]`](https://github.com/pandas-dev/pandas/blob/f7ee822b0dbd74d1b50bd46098c00ec87c971737/pandas/tests/scalar/timedelta/test_constructors.py#L217-L230) [failed](https://github.com/pandas-dev/pandas/runs/6397652653?check_suite_focus=true#step:11:1290) for 3c21c7b, but [not](https://github.com/pandas-dev/pandas/runs/6398549467?check_suite_focus=true#step:11:1208) for f7ee822. (The other parameterizations of that test failed in both commits.) It's unclear to me why that would happen, given this [one-line change](https://github.com/pandas-dev/pandas/pull/47004/commits/f7ee822b0dbd74d1b50bd46098c00ec87c971737#diff-b3775d3d77156cd44152724f69d63a7c10fcc58fbcce549e40f62d0441f23835), or why that test is failing at all&mdash;but just for the Minimum Versions check.

Thoughts/feedback here appreciated.
Update: [9bf564b](https://github.com/pandas-dev/pandas/pull/47004/commits/9bf564b57340621ce69945edaa0111b37a64fc36) handles a bizarre string interpolation issue that was coming up in the "Minimum Versions" check. The remaining failures are build issues orthogonal to this PR, and which appear to be fixed in https://github.com/pandas-dev/pandas/pull/47015.
> minor comments. any performance impact here?

oh you put that in the top post cool.

then just minor comments.
This pull request is stale because it has been open for thirty days with no activity. Please [update](https://pandas.pydata.org/pandas-docs/stable/development/contributing.html#updating-your-pull-request) and respond to this comment if you're still interested in working on this.
Thanks for the pull request, but it appears to have gone stale. If interested in continuing, please merge in the main branch, address any review comments and/or failing tests, and we can reopen.