CLN: Remove/deprecate unused/misleading dtype functions
State: closed

For "misleading" the leading case is `is_int_or_datetime_dtype`, which includes timedelta64 but excludes datetime64tz.

Several branches in `lib.infer_dtype` are unreachable, are removed.

`is_datetimetz` is redundant with `is_datetime64tz_dtype`, is deprecated, and internal uses of it are changed.

`is_period` is both redundant and misleading, is deprecated (no internal usages outside of tests)

`maybe_convert_string_to_object` and `maybe_convert_scalar` are never used outside of tests, removed

xref #22137

cc: @h-vetinari IIRC you've been looking at other functions in core.dtypes.cast with an eye towards cleanup or de-duplication.  Suggestions welcome.

Labels:
name: Dtype Conversions
description: Unexpected or buggy dtype conversions
name: Clean

Comments:
Hello @jbrockmendel! Thanks for submitting the PR.

 - There are no PEP8 issues in the file [`pandas/core/algorithms.py`](https://github.com/jbrockmendel/pandas/blob/8e86599f33fb70891e0f12fa3f5a10c87a0452cb/pandas/core/algorithms.py) !

 - There are no PEP8 issues in the file [`pandas/core/dtypes/cast.py`](https://github.com/jbrockmendel/pandas/blob/8e86599f33fb70891e0f12fa3f5a10c87a0452cb/pandas/core/dtypes/cast.py) !

 - There are no PEP8 issues in the file [`pandas/core/dtypes/common.py`](https://github.com/jbrockmendel/pandas/blob/8e86599f33fb70891e0f12fa3f5a10c87a0452cb/pandas/core/dtypes/common.py) !

 - There are no PEP8 issues in the file [`pandas/core/dtypes/concat.py`](https://github.com/jbrockmendel/pandas/blob/8e86599f33fb70891e0f12fa3f5a10c87a0452cb/pandas/core/dtypes/concat.py) !

 - There are no PEP8 issues in the file [`pandas/core/frame.py`](https://github.com/jbrockmendel/pandas/blob/8e86599f33fb70891e0f12fa3f5a10c87a0452cb/pandas/core/frame.py) !

 - There are no PEP8 issues in the file [`pandas/core/indexes/datetimes.py`](https://github.com/jbrockmendel/pandas/blob/8e86599f33fb70891e0f12fa3f5a10c87a0452cb/pandas/core/indexes/datetimes.py) !

 - There are no PEP8 issues in the file [`pandas/core/internals/blocks.py`](https://github.com/jbrockmendel/pandas/blob/8e86599f33fb70891e0f12fa3f5a10c87a0452cb/pandas/core/internals/blocks.py) !

 - There are no PEP8 issues in the file [`pandas/core/internals/concat.py`](https://github.com/jbrockmendel/pandas/blob/8e86599f33fb70891e0f12fa3f5a10c87a0452cb/pandas/core/internals/concat.py) !

 - There are no PEP8 issues in the file [`pandas/core/nanops.py`](https://github.com/jbrockmendel/pandas/blob/8e86599f33fb70891e0f12fa3f5a10c87a0452cb/pandas/core/nanops.py) !

 - There are no PEP8 issues in the file [`pandas/core/reshape/merge.py`](https://github.com/jbrockmendel/pandas/blob/8e86599f33fb70891e0f12fa3f5a10c87a0452cb/pandas/core/reshape/merge.py) !

 - There are no PEP8 issues in the file [`pandas/io/formats/format.py`](https://github.com/jbrockmendel/pandas/blob/8e86599f33fb70891e0f12fa3f5a10c87a0452cb/pandas/io/formats/format.py) !

 - There are no PEP8 issues in the file [`pandas/tests/api/test_types.py`](https://github.com/jbrockmendel/pandas/blob/8e86599f33fb70891e0f12fa3f5a10c87a0452cb/pandas/tests/api/test_types.py) !

 - There are no PEP8 issues in the file [`pandas/tests/dtypes/test_cast.py`](https://github.com/jbrockmendel/pandas/blob/8e86599f33fb70891e0f12fa3f5a10c87a0452cb/pandas/tests/dtypes/test_cast.py) !

 - There are no PEP8 issues in the file [`pandas/tests/dtypes/test_common.py`](https://github.com/jbrockmendel/pandas/blob/8e86599f33fb70891e0f12fa3f5a10c87a0452cb/pandas/tests/dtypes/test_common.py) !

 - There are no PEP8 issues in the file [`pandas/tests/dtypes/test_dtypes.py`](https://github.com/jbrockmendel/pandas/blob/8e86599f33fb70891e0f12fa3f5a10c87a0452cb/pandas/tests/dtypes/test_dtypes.py) !

 - There are no PEP8 issues in the file [`pandas/tests/dtypes/test_inference.py`](https://github.com/jbrockmendel/pandas/blob/8e86599f33fb70891e0f12fa3f5a10c87a0452cb/pandas/tests/dtypes/test_inference.py) !

 - There are no PEP8 issues in the file [`pandas/tests/test_base.py`](https://github.com/jbrockmendel/pandas/blob/8e86599f33fb70891e0f12fa3f5a10c87a0452cb/pandas/tests/test_base.py) !


@jbrockmendel @h-vetinari : Have a look at #16242 as well.  This issue might be resolvable based on what you guys are doing here...
Thanks for tagging me.

There's a couple of things I encountered in #23796, mainly:
* `astype_intsafe` from `_libs.lib` is only used once in `dtypes.cast.astype_nansafe` on the object branch, where there's some confusing/contradictory/redundant branches (related to your integer/datetime/timedelta point)
  * the branch for `if np.issubdtype(dtype.type, np.integer):` catches all the timedeltas (but not the datetimes), both of which would have a dedicated branch further down. However, letting the timedeltas actually hit their dedicated branch means re-calling `astype_nansafe` after explicitly casting object dtype to timedelta, hitting a dedicated timedelta-dtype-branch, which casts to float. This then contradicts/fails the `TestMerge::test_other_timedelta_unit` test, because it expects timedelta output.
  * furthermore, that integer-condition is actually relevant for some `IntegerArray` cases, because there's no dedicated branch for extension array types (on the side of the caller) yet.
* `maybe_promote` and `maybe_upcast_putmask` have some issues, #23823 / #23833 (actually many more than listed in those issues). Both are also completely untested directly, AFAICT. I'm currently in the process of writing tests for the former, and then hoping to fix the behaviour.
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/23917?src=pr&el=h1) Report
> Merging [#23917](https://codecov.io/gh/pandas-dev/pandas/pull/23917?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/d53a4cc856e7fbdfd7d0db75e8e3416b0640ce98?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/23917/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/23917?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #23917      +/-   ##
==========================================
+ Coverage   92.31%   92.31%   +<.01%     
==========================================
  Files         161      161              
  Lines       51488    51471      -17     
==========================================
- Hits        47530    47515      -15     
+ Misses       3958     3956       -2
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.7% <100%> (ø)` | :arrow_up: |
| #single | `42.43% <66.66%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/23917?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/arrays/datetimes.py](https://codecov.io/gh/pandas-dev/pandas/pull/23917/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYXJyYXlzL2RhdGV0aW1lcy5weQ==) | `98.37% <ø> (ø)` | :arrow_up: |
| [pandas/io/formats/format.py](https://codecov.io/gh/pandas-dev/pandas/pull/23917/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvZm9ybWF0LnB5) | `97.76% <100%> (ø)` | :arrow_up: |
| [pandas/core/indexes/datetimes.py](https://codecov.io/gh/pandas-dev/pandas/pull/23917/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9kYXRldGltZXMucHk=) | `96.47% <100%> (ø)` | :arrow_up: |
| [pandas/core/dtypes/common.py](https://codecov.io/gh/pandas-dev/pandas/pull/23917/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZHR5cGVzL2NvbW1vbi5weQ==) | `94.77% <100%> (+0.39%)` | :arrow_up: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/23917/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.02% <100%> (ø)` | :arrow_up: |
| [pandas/core/algorithms.py](https://codecov.io/gh/pandas-dev/pandas/pull/23917/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYWxnb3JpdGhtcy5weQ==) | `95.11% <100%> (ø)` | :arrow_up: |
| [pandas/core/dtypes/concat.py](https://codecov.io/gh/pandas-dev/pandas/pull/23917/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZHR5cGVzL2NvbmNhdC5weQ==) | `96.66% <100%> (ø)` | :arrow_up: |
| [pandas/core/nanops.py](https://codecov.io/gh/pandas-dev/pandas/pull/23917/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvbmFub3BzLnB5) | `95.05% <100%> (ø)` | :arrow_up: |
| [pandas/core/internals/blocks.py](https://codecov.io/gh/pandas-dev/pandas/pull/23917/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW50ZXJuYWxzL2Jsb2Nrcy5weQ==) | `93.71% <100%> (ø)` | :arrow_up: |
| [pandas/core/reshape/merge.py](https://codecov.io/gh/pandas-dev/pandas/pull/23917/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvcmVzaGFwZS9tZXJnZS5weQ==) | `94.27% <100%> (+0.03%)` | :arrow_up: |
| ... and [2 more](https://codecov.io/gh/pandas-dev/pandas/pull/23917/diff?src=pr&el=tree-more) | |

------

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

rebased, also fixed a docstring mistake I made in #23937.
ok! ping on green.
Ping
thanks!