BUG: Fixes unwanted casting in .isin (GH21804)
State: closed

- [x] closes #21804 
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry

Results from running asvs on algorithms are:
```
       before           after         ratio
     [365eac4d]       [ee66578f]
+        1.91±0ms      2.28±0.02ms     1.20  algorithms.Hashing.time_series_timedeltas
-     2.34±0.02ms         1.88±0ms     0.80  algorithms.Hashing.time_series_int

SOME BENCHMARKS HAVE CHANGED SIGNIFICANTLY.
```

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

Comments:
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/21893?src=pr&el=h1) Report
> Merging [#21893](https://codecov.io/gh/pandas-dev/pandas/pull/21893?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/0a0b2b9fc56eb0753b6d07a55d497b970c8ccfe0?src=pr&el=desc) will **decrease** coverage by `<.01%`.
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff             @@
##           master   #21893      +/-   ##
==========================================
- Coverage   91.96%   91.96%   -0.01%     
==========================================
  Files         166      166              
  Lines       50334    50337       +3     
==========================================
+ Hits        46292    46293       +1     
- Misses       4042     4044       +2
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.36% <100%> (-0.01%)` | :arrow_down: |
| #single | `42.23% <100%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/21893?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/algorithms.py](https://codecov.io/gh/pandas-dev/pandas/pull/21893/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvYWxnb3JpdGhtcy5weQ==) | `94.4% <100%> (-0.29%)` | :arrow_down: |

------

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

Revised benchmarks after 84be606
```
       before           after         ratio
     [365eac4d]       [84be606b]
+        1.82±0ms         2.23±0ms     1.23  algorithms.Hashing.time_series_int
-        2.24±0ms         1.83±0ms     0.82  algorithms.Hashing.time_series_float

SOME BENCHMARKS HAVE CHANGED SIGNIFICANTLY.
```
Any other edits needed? Thanks
Should I run the complete ASV suite? Earlier I only ran it for algorithms...
If the results are not promising, we can just close this PR. If they are we can re-think the approach - any suggestions are welcome as I think I am stuck in my thought pattern. Thanks
run a battery of tests anything relating to value_counts, unique, factorize
@jreback you are correct - indeed many benchmarks have worsened (I actually didn't know where to start, so ran the full asv suite). Let me rebase and run the asv again and then will rethink the approach.

Current results below (pasting only the ones where it worsened):
```ruby
       before           after         ratio
     [537b65cb]       [82386c31]
+        1.46±0ms      15.9±0.08ms    10.86  series_methods.IsIn.time_isin('int64')
+          9.20ms           78.3ms     8.51  categoricals.Isin.time_isin_categorical('int64')
+      9.56±0.4ms       79.7±0.4ms     8.33  categoricals.Isin.time_isin_categorical('object')
+     2.47±0.02ms       15.0±0.1ms     6.05  series_methods.IsIn.time_isin('object')
+     8.21±0.08ms       20.5±0.2ms     2.50  indexing.MultiIndexing.time_index_slice
+     2.89±0.01ms       4.49±0.1ms     1.56  binary_ops.Ops.time_frame_comparison(True, 1)
+        77.3±2ms          120±3ms     1.56  binary_ops.Ops.time_frame_comparison(False, 1)
+      6.74±0.4ms       9.97±0.4ms     1.48  groupby.Categories.time_groupby_nosort
+      78.4±0.7ms         115±10ms     1.47  binary_ops.Ops.time_frame_comparison(False, 'default')
+     2.93±0.02ms      4.29±0.01ms     1.46  binary_ops.Ops.time_frame_add(False, 'default')
+     2.94±0.05ms       4.06±0.2ms     1.38  binary_ops.Ops.time_frame_add(False, 1)
+     3.98±0.07ms       5.07±0.2ms     1.28  gil.ParallelRolling.time_rolling('mean')
+          86.0μs            108μs     1.25  frame_methods.XS.time_frame_xs(0)
+     2.32±0.05ms      2.87±0.01ms     1.24  rolling.VariableWindowMethods.time_rolling('DataFrame', '1h', 'float', 'count')
+      69.6±0.3μs       85.7±0.2μs     1.23  groupby.GroupByMethods.time_dtype_as_group('int', 'any', 'transformation')
+     4.40±0.04ms      5.32±0.08ms     1.21  rolling.Methods.time_rolling('Series', 1000, 'float', 'std')
+     2.49±0.01ms      2.98±0.04ms     1.20  ctors.SeriesConstructors.time_series_constructor(<function SeriesConstructors.<lambda> at 0x7f06c40db1e0>, True)
+          12.9ms           15.1ms     1.17  categoricals.CategoricalSlicing.time_getitem_bool_array('non_monotonic')
+     1.93±0.07ms       2.27±0.1ms     1.17  frame_methods.NSort.time_nsmallest('last')
+     2.07±0.09ms         2.35±0ms     1.13  algorithms.Hashing.time_series_dates
+      7.29±0.2μs           8.26μs     1.13  categoricals.CategoricalSlicing.time_getitem_list_like('non_monotonic')
+     1.94±0.04ms       2.19±0.1ms     1.13  frame_methods.NSort.time_nlargest('all')
+      57.2±0.5ms       64.5±0.4ms     1.13  groupby.Groups.time_series_groups('object_small')
+           313ms            347ms     1.11  gil.ParallelReadCSV.time_read_csv('float')
+     35.7±0.06μs         39.5±1μs     1.11  ctors.SeriesConstructors.time_series_constructor(<function SeriesConstructors.<lambda> at 0x7f06c40db2f0>, True)
```
thanks @KalyanGokhale the code here *looks* really simple, but quickly get into perf issues.
@jreback @gfyoung 

Did some further tinkering today, and with the original code for `.isin` the results are as follows:
```python
>>> import pandas as pd
>>> import numpy as np
>>> import pandas.core.algorithms as algos
>>> comps=[1,0]
>>> values=[1,0.5]
>>> algos.isin(comps, values)
array([ True, False])
```
Above is as expected, whereas the result below is not...
```python
>>> pd.Series(comps).isin(values)
0    True
1    True
dtype: bool
```
Now thinking about it, its not an isolated casting issue in `.isin` (which actually seems to be working fine) - but rather a fundamental issue of casting with `Series` in general or something else upstream? 
e.g.
```python
>>> s = pd.Series([])
>>> s[0]=100
>>> s
0    100
dtype: int64
>>> s[0]=100.589
>>> s
0    100
dtype: int64
```
(was also thinking of the issue #21881 and related ones) - not sure of whether my thinking is on the correct lines...

The current issue can be addressed (probably even improving the perf) - but now seems rather like a band-aid, unless we address the casting issue(s) for Series....(have not yet checked if this was already discussed and the consensus is to not do it for some valid reason...)
Thoughts?

p.s:
This means that the current test cases I had written certainly need to be updated specifically to include `Series` - though had tested it specifically for `Series` on command line with the original test case :)
@KalyanGokhale there are very thorny casting issues involved, so certainly add the other test cases
can you rebase and update to comments
> can you rebase and update to comments

@jreback yes - will do over coming few days
closing as stale. if you'd like to continue, pls ping.