MAINT: Adjust the codebase to the new `np.array`'s `copy` keyword meaning
State: closed

Hi! 

This PR addresses changes planned for NumPy in https://github.com/numpy/numpy/pull/25168 (new `copy` keyword for `np.asarray` and `np.array`).

`np.array(..., copy=False)` will now throw and exception if a copy is needed. To retain the same behavior `np.asarray(...)` can be used, so a copy is made only when needed.

I applied changes in a backward-compatible way, so that no NumPy version check & branching is needed.

Labels:
name: Compat
description: pandas objects compatability with Numpy or Python functions

Comments:
If you have time could you investigate making `test_array_interface` compatible with older versions of numpy?
https://github.com/numpy/numpy/pull/25168 got merged today - once a new nightly build is published, all tests should pass now. 
NumPy nightly build disappeared (https://github.com/numpy/numpy/issues/25907) but once it's back this PR should be ready. 
@mtsokol Please retrigger the CI given https://github.com/numpy/numpy/issues/25907#issuecomment-1973431825.
Hi! I updated the PR with all required changes to be compatible with NumPy dev - now `Numpy Dev` CI job passes.

My question: There's a new `copy` keyword required in the [`__array__` protocol](https://numpy.org/devdocs/reference/arrays.classes.html#numpy.class.__array__) implementations. Would you like to keep this keyword unused (as `dtype` is sometimes ignored in the codebase) or do you want to actually make use of it internally where it makes sense? 
> Would you like to keep this keyword unused (as dtype is sometimes ignored in the codebase) or do you want to actually make use of it internally where it makes sense?

Yes we would probably want to make use of it, but that can be done in a follow up
Looks like there's some typing validations with the new addition of the keyword. Let me know if you'd like help with them: https://github.com/pandas-dev/pandas/actions/runs/8143484703/job/22255440584
> Looks like there's some typing validations with the new addition of the keyword...

These errors look pretty tricky. I guess it's due to the fact that this job installed NumPy 1.x where `copy` signature is `bool | _CopyMode`. Locally I see that installing Numpy 2.0dev yields even more typing errors in other places (like `error: Name "np.bool_" is not defined` as `bool_` typing stub got renamed to `bool`).

So for this PR, maybe `# type: ignore` for these calls would work?

```
error: Argument "copy" to "array" has incompatible type "bool | None"; expected "bool | _CopyMode"  [arg-type]
```
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 2.2.x
git pull
```

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

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

```
git commit -am "Backport PR #57172: MAINT: Adjust the codebase to the new `np.array`'s `copy` keyword meaning"
```

4. Push to a named branch:

```
git push YOURFORK 2.2.x:auto-backport-of-pr-57172-on-2.2.x
```

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

> "Backport PR #57172 on branch 2.2.x (MAINT: Adjust the codebase to the new `np.array`'s `copy` keyword meaning)"

And apply the correct labels and milestones.

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

Remember to remove the `Still Needs Manual Backport` label once the PR gets merged.

If these instructions are inaccurate, feel free to [suggest an improvement](https://github.com/MeeseeksBox/MeeseeksDev).
                
Thanks @mtsokol. Will create a follow up issue about utilizing the new `copy` keyword in `__array__`