BUG: Fix behavior of replace_list with mixed types.
State: closed

- [x] closes #40371 
- [x] tests added / passed
- [x] Ensure all linting tests pass, see [here](https://pandas.pydata.org/pandas-docs/dev/development/contributing.html#code-standards) for how to run them
- [ ] whatsnew entry



Labels:
name: Regression
description: Functionality that used to work in a prior pandas version
name: replace
description: replace method

Comments:
Looks like this caused some test errors elsewhere: https://dev.azure.com/pandas-dev/pandas/_build/results?buildId=56659&view=logs&j=b516280a-8c75-541b-8ba0-ae13561718a7&t=a9217cb1-24f6-5625-717d-72085a70e1d3&l=41
Thanks for the comments.

- I reverted changes in cast.py file since it turns out making changes in cast.py wasn't good idea. 
- I updated the tests.
- I need some help for the PR. 
https://github.com/pandas-dev/pandas/blob/9c4c82112f894d631b09873a64cc74d212b2041d/pandas/core/internals/blocks.py#L801-L804

Removing if statement here will solve the problem but i am wondering is there are better way to solve it?

Thanks @hasan-yaman for the PR. can you fix-up failing tests... it appears actual fix has been lost in a subsequent commit and add a release note (will be 1.2.5 but can add to doc\source\whatsnew\v1.2.4.rst in the first instance and move once doc\source\whatsnew\v1.2.5.rst is available)
> Thanks @hasan-yaman for the PR. can you fix-up failing tests... it appears actual fix has been lost in a subsequent commit and add a release note (will be 1.2.5 but can add to doc\source\whatsnew\v1.2.4.rst in the first instance and move once doc\source\whatsnew\v1.2.5.rst is available)

this is just a tests, no whatsnew needed.
> this is just a tests, no whatsnew needed.

IIUC this PR a regression fix for #40371 but the fix, added in the first commit, has been lost.
Actually, first commit was causing too many problems with the tests, I couldn't fix them. So, I reverted the changes from the first commit. 
I am open to any idea to fix the problem. (Maybe others who know pandas better than me can attempt the fix the problem with separate PR.)  


> I am open to any idea to fix the problem. (Maybe others who know pandas better than me can attempt the fix the problem with separate PR.)

The regression was caused by #38097, so the fix probably involves code changed in that PR. cc @jbrockmendel 

> > this is just a tests, no whatsnew needed.
> 
> IIUC this PR a regression fix for #40371 but the fix, added in the first commit, has been lost.

what do you mean? if this doesn't have a patch then the tests shouldn't pass (or is this *already) fixed?
the regression is not fixed.
This pull request is stale because it has been open for thirty days with no activity. Please update or respond to this comment if you're still interested in working on this.
> > > this is just a tests, no whatsnew needed.
> > 
> > 
> > IIUC this PR a regression fix for #40371 but the fix, added in the first commit, has been lost.
> 
> what do you mean? if this doesn't have a patch then the tests shouldn't pass (or is this *already) fixed?

The tests are not passing.
to_replace is dispatched to _replace_list if is_list_like(to_replace)

_replace_list only accepts lists, but also need to make sure that the list is python floats

```
>>> import numpy as np
>>> 
>>> type(np.array([1.0])[0])
<class 'numpy.float64'>
>>> 
>>> type(list(np.array([1.0]))[0])
<class 'numpy.float64'>
>>> 
>>> type(np.array([1.0]).tolist()[0])
<class 'float'>
>>> 
```

or change _can_hold_element
> or change _can_hold_element

this maybe a better option since this is where we have the inconsistency

```
>>> 
>>> pd.core.dtypes.cast.can_hold_element(arr=pd.Series([1]).values, element=1.0)
True
>>> 
>>> pd.core.dtypes.cast.can_hold_element(
...     arr=pd.Series([1]).values, element=np.array([1.0])[0]
... )
False
>>> 
```
> > or change _can_hold_element
> 
> this maybe a better option since this is where we have the inconsistency

changed my mind again! since we have a mature backport branch and may not have any more patch releases on 1.2.x let's not make this change and just patch the code that was added in #38097 to convert a numpy array to a list of python objects.
@jreback can you take a look. not the best fix, but will be fixed properly ( I think) in #41644, which would be updated to remove the code added here on master straight after this is backported.
will merge (and backport) this shortly if no further comments
yep looks ok (rebase if u can though)
> yep looks ok (rebase if u can though)

merged master an hour ago, no commits to master since then
Thanks @hasan-yaman 
Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

1. Checkout backport branch and update it.

```
$ git checkout 1.2.x
$ git pull
```

2. Cherry pick the first parent branch of the this PR on top of the older branch:
```
$ git cherry-pick -m1 9231b495aadac6e93a0385c24066d84a1fb9eac6
```

3. You will likely have some merge/cherry-pick conflict here, fix them and commit:

```
$ git commit -am 'Backport PR #40555: BUG: Fix behavior of replace_list with mixed types.'
```

4. Push to a named branch :

```
git push YOURFORK 1.2.x:auto-backport-of-pr-40555-on-1.2.x
```

5. Create a PR against branch 1.2.x, I would have named this PR:

> "Backport PR #40555 on branch 1.2.x"

And apply the correct labels and milestones.

Congratulation you did some good work ! Hopefully your backport PR will be tested by the continuous integration and merged soon!

If these instruction are inaccurate, feel free to [suggest an improvement](https://github.com/MeeseeksBox/MeeseeksDev).