Separate _TSObject into conversion
State: closed

This is what we've been building towards folks.  `convert_to_tsobject`, `convert_str_to_tsobject`, and `convert_datetime_to_tsobject` are moved to `tslibs.conversion`.  Concerns regarding `_TSObjects`s can be considered separated.

This isn't quite cut/paste:
- `convert_to_tsobject` calls `is_timestamp` which is not available in the namespace.  Similarly `_localize_pydatetime` checks `isinstance(dt, Timestamp`).  So this re-implements private versions:
- `conversion._is_timestamp` checks that its input has type `datetime`.  IIUC correctly this check should be as performant as `tslib.is_timestamp`.  (moderate sized "if")
- `conversion._localize_pydatetime` includes small optimizations that are available because we know the context in which it is being called.  Specifically, 1) it can skip the `Timestamp` case since any nanos get dropped in `pydatetime_to_dt64`, and 2) the `tz` arg is typed as a `tzinfo` object, 3) it skips unnecessary `None` check.

Other notes:
- `_get_datetime64_nanos` is called by `convert_to_tsobject`, so also had to be moved.  It fits well with the "conversion" theme.


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

Comments:
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/18060?src=pr&el=h1) Report
> Merging [#18060](https://codecov.io/gh/pandas-dev/pandas/pull/18060?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/881ee30b443f8b9651068b819ecd43adca5789b5?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/18060/graphs/tree.svg?token=eZ4WkYLtcO&src=pr&width=650&height=150)](https://codecov.io/gh/pandas-dev/pandas/pull/18060?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #18060      +/-   ##
==========================================
- Coverage   91.25%   91.23%   -0.02%     
==========================================
  Files         163      163              
  Lines       50115    50115              
==========================================
- Hits        45730    45721       -9     
- Misses       4385     4394       +9
```

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

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/18060?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/18060?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `25% <0%> (-58.34%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/18060?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/18060?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/18060?src=pr&el=footer). Last update [881ee30...3e8b102](https://codecov.io/gh/pandas-dev/pandas/pull/18060?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/18060?src=pr&el=h1) Report
> Merging [#18060](https://codecov.io/gh/pandas-dev/pandas/pull/18060?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/0fd3bd73e17e4123f1d334f4ebd305e33cd75fe1?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/18060/graphs/tree.svg?height=150&width=650&token=eZ4WkYLtcO&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/18060?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #18060      +/-   ##
==========================================
- Coverage   91.25%   91.23%   -0.02%     
==========================================
  Files         163      163              
  Lines       50120    50120              
==========================================
- Hits        45737    45728       -9     
- Misses       4383     4392       +9
```

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

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/18060?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/18060?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `25% <0%> (-58.34%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/18060?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/18060?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/18060?src=pr&el=footer). Last update [0fd3bd7...c337c53](https://codecov.io/gh/pandas-dev/pandas/pull/18060?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

+1 for refactoring and logic decoupling, but let's make sure performance didn't take a hit by accident (FYI, if you do plan on doing anymore of these types of PR's, I would suggest you provide that from the get go).
Appveyor error looks like it can't import scipy; saw the same error in #18016 a few minutes ago.


Not sure if its related, but trying to run asv it keeps timing out, stalling at the "Creating environments" step.
 <b>update</b> not related
Suggestions for the scipy import error on appveyor?
@jreback : This looks like a `scipy` issue to me, as I'm seeing that in my PR as well.
Forgot to use affinity on the first run here:
```
asv continuous -f 1.1 -E virtualenv master HEAD -b timeseries
[...]
      before           after         ratio
     [881ee30b]       [b129a9e1]
+     8.95±0.06μs      11.0±0.07μs     1.23  timeseries.Offsets.time_timeseries_year_apply
+       163±0.2ms        187±0.4ms     1.15  timeseries.ToDatetime.time_iso8601_tz_spaceformat
-      34.5±0.1μs      31.1±0.07μs     0.90  timeseries.Offsets.time_custom_bday_cal_decr
-      59.1±0.4μs      53.1±0.04μs     0.90  timeseries.SemiMonthOffset.time_end_incr
-           1.51s          405±9ms     0.27  timeseries.AsOfDataFrame.time_asof
-           2.00s        421±0.8ms     0.21  timeseries.AsOfDataFrame.time_asof_nan
```

Note master got updated to a new commit between runs here.

```
taskset 5 asv continuous -f 1.1 -E virtualenv master HEAD -b timeseries
[...]
       before           after         ratio
     [15fa4bd6]       [b129a9e1]
+       96.1±30ms            154ms     1.61  timeseries.DatetimeIndex.time_dti_tz_factorize
+           1.71s            2.06s     1.20  timeseries.AsOfDataFrame.time_asof_nan
-           2.17s       1.95±0.03s     0.90  timeseries.AsOfDataFrame.time_asof
-      29.0±0.1μs       24.8±0.1μs     0.86  timeseries.AsOf.time_asof_single
-          51.7ms       8.82±0.4ms     0.17  timeseries.DatetimeIndex.time_dti_factorize

taskset 5 asv continuous -f 1.1 -E virtualenv master HEAD -b timeseries
[...]
      before           after         ratio
     [15fa4bd6]       [b129a9e1]
+        25.5±2ms         36.0±2ms     1.41  timeseries.DatetimeIndex.time_dti_tz_factorize
-      25.6±0.2μs       22.3±0.2μs     0.87  timeseries.Offsets.time_timeseries_day_apply
-     4.08±0.01ms      3.41±0.01ms     0.83  timeseries.ToDatetime.time_iso8601
-        509±50ms          413±1ms     0.81  timeseries.DatetimeIndex.time_add_offset_slow

taskset 6 asv continuous -f 1.1 -E virtualenv master HEAD -b timeseries
[...]
       before           after         ratio
     [15fa4bd6]       [b129a9e1]
+      11.9±0.2μs       14.8±0.3μs     1.24  timeseries.Offsets.time_timeseries_year_incr
+       173±0.4ms          198±5ms     1.15  timeseries.ToDatetime.time_iso8601_tz_spaceformat
+     7.15±0.05ms       8.04±0.4ms     1.12  timeseries.DatetimeIndex.time_add_offset_fast
+      49.0±0.4μs         54.3±1μs     1.11  timeseries.SemiMonthOffset.time_begin_apply
-        539±20μs        477±0.4μs     0.89  timeseries.DatetimeIndex.time_reset_index_tz
-      444±0.01μs        387±0.3μs     0.87  timeseries.DatetimeIndex.time_reset_index
-      22.8±0.2μs      19.7±0.09μs     0.87  timeseries.Offsets.time_custom_bday_apply_dt64
-      2.21±0.2ms         1.72±0ms     0.78  timeseries.DatetimeIndex.time_add_offset_delta
```

Given the excess variability caused by my local entropy field, this looks about unchanged.
looks good. pls rebase
thanks!