PERF/REGR: astype changing order of some 2d data
State: closed

- [x] closes #42396
- [x] closes #38592
- [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
- [x] whatsnew entry

This just lets `numpy` stick with the default of `order="C"` for both `ravel` and `reshape` so no reordering occurs. 

Benchmarks:

```
       before           after         ratio
     [87d78559]       [0c9d5496]
     <master>         <regr_astype>
-      9.34±0.7ms       4.95±0.9ms     0.53  stat_ops.FrameOps.time_op('kurt', 'int', 1)
-      5.08±0.8ms       2.63±0.1ms     0.52  stat_ops.FrameOps.time_op('skew', 'Int64', 0)
-      4.46±0.2ms       2.25±0.3ms     0.50  stat_ops.FrameOps.time_op('std', 'int', 1)
-      5.76±0.2ms       2.73±0.6ms     0.47  stat_ops.FrameOps.time_op('var', 'int', 1)
-      11.5±0.7ms       5.09±0.3ms     0.44  stat_ops.FrameOps.time_op('sem', 'int', 0)
-      1.36±0.2ms         600±50μs     0.44  stat_ops.FrameOps.time_op('prod', 'int', 0)
-        17.5±5ms       7.60±0.7ms     0.43  stat_ops.FrameOps.time_op('skew', 'int', 1)
-      8.64±0.7ms       3.37±0.4ms     0.39  stat_ops.FrameOps.time_op('kurt', 'int', 0)
-      9.03±0.6ms       3.21±0.2ms     0.36  stat_ops.FrameOps.time_op('skew', 'int', 0)
-      9.49±0.6ms       3.27±0.4ms     0.34  stat_ops.FrameOps.time_op('mad', 'int', 0)
-     1.31±0.08ms         420±20μs     0.32  stat_ops.FrameOps.time_op('sum', 'int', 1)
-      5.87±0.5ms       1.87±0.5ms     0.32  stat_ops.FrameOps.time_op('var', 'int', 0)
-     1.49±0.09ms         467±20μs     0.31  stat_ops.FrameOps.time_op('sum', 'int', 0)
-      4.44±0.3ms       1.39±0.1ms     0.31  stat_ops.FrameOps.time_op('std', 'int', 0)
-     1.90±0.06ms         569±70μs     0.30  stat_ops.FrameOps.time_op('mean', 'int', 0)
-      1.22±0.1ms          346±4μs     0.28  stat_ops.FrameOps.time_op('prod', 'int', 1)
-      1.86±0.1ms         519±60μs     0.28  stat_ops.FrameOps.time_op('mean', 'int', 1)
```

Will look to come up with a benchmark hitting this regression more squarely (which would help test if a more clever contiguity approach than always defaulting to "C" can improve performance).

EDIT: after regression is fixed, might be worth looking into reinstating something like the original approach. Some timings show performance benefit we lose here:

```
In [1]: import numpy as np

In [2]: import pandas as pd

In [3]: data = np.ones((2000, 2000), order="F")

In [4]: df = pd.DataFrame(data)

In [5]: %timeit df.astype(np.int32)
5.63 ms ± 114 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)  # this pr
5.7 ms ± 115 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)  # master

In [6]: data = np.ones((2000, 2000), order="C")

In [7]: df = pd.DataFrame(data)

In [8]: %timeit df.astype(np.int32)
26.8 ms ± 313 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)  # this pr
5.63 ms ± 77.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)  # master
```

EDIT: This slowdown comes down to the ravel copying because of contiguity that doesn't match. Let me know if it's better to try to fix the slowdown shown above in this pr or to leave as a followup for 1.4

Labels:
name: Performance
description: Memory or execution speed performance
name: Dtype Conversions
description: Unexpected or buggy dtype conversions
name: Regression
description: Functionality that used to work in a prior pandas version
name: Reduction Operations
description: sum, mean, min, max, etc.

Comments:
im a bit confused as to what parts of this are about a regression[bug] vs a regression[perf].  @mzeitlin11 will this be obvious once i get some caffeine?
> im a bit confused as to what parts of this are about a regression[bug] vs a regression[perf]. @mzeitlin11 will this be obvious once i get some caffeine?

Haha, I'll try to clarify :)

- Bug: The `ravel` and `reshape` could happen with different orders, garbling the data. This can be fixed by specifying the same order in `ravel` as used for `reshape` (described in more detail below your other comment). However, this will not fix the perf regression
- Perf: With behavior fix above, we can end up with F-contiguous data depending on what contiguity goes into the `astype` call. For the reduction regressions at the top of the issue, this F-contiguity is what caused the regression - the routines are just slower on F-contig data. Just leaving default order ensures C-contiguity, fixing the perf regression.
ok thanks for the explanation @mzeitlin11 
@meeseeksdev backport 1.3.x
Something went wrong ... Please have  a look at my logs.