Skip to content

Commit 6455912

Browse files
authored
PERF/REGR: astype changing order of some 2d data (#42475)
1 parent 8de8e92 commit 6455912

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

doc/source/whatsnew/v1.3.1.rst

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Fixed regressions
1818
- :class:`DataFrame` constructed with an older version of pandas could not be unpickled (:issue:`42345`)
1919
- Performance regression in constructing a :class:`DataFrame` from a dictionary of dictionaries (:issue:`42248`)
2020
- Fixed regression in :meth:`DataFrame.agg` dropping values when the DataFrame had an Extension Array dtype, a duplicate index, and ``axis=1`` (:issue:`42380`)
21+
- Fixed regression in :meth:`DataFrame.astype` changing the order of noncontiguous data (:issue:`42396`)
22+
- Performance regression in :class:`DataFrame` in reduction operations requiring casting such as :meth:`DataFrame.mean` on integer data (:issue:`38592`)
2123
- Performance regression in :meth:`DataFrame.to_dict` and :meth:`Series.to_dict` when ``orient`` argument one of "records", "dict", or "split" (:issue:`42352`)
2224
- Fixed regression in indexing with a ``list`` subclass incorrectly raising ``TypeError`` (:issue:`42433`, :issue:`42461`)
2325
-

pandas/core/dtypes/cast.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from typing import (
1515
TYPE_CHECKING,
1616
Any,
17-
Literal,
1817
Sized,
1918
TypeVar,
2019
cast,
@@ -1093,14 +1092,11 @@ def astype_nansafe(
10931092
The dtype was a datetime64/timedelta64 dtype, but it had no unit.
10941093
"""
10951094
if arr.ndim > 1:
1096-
# Make sure we are doing non-copy ravel and reshape.
1097-
flags = arr.flags
1098-
flat = arr.ravel("K")
1095+
flat = arr.ravel()
10991096
result = astype_nansafe(flat, dtype, copy=copy, skipna=skipna)
1100-
order: Literal["C", "F"] = "F" if flags.f_contiguous else "C"
11011097
# error: Item "ExtensionArray" of "Union[ExtensionArray, ndarray]" has no
11021098
# attribute "reshape"
1103-
return result.reshape(arr.shape, order=order) # type: ignore[union-attr]
1099+
return result.reshape(arr.shape) # type: ignore[union-attr]
11041100

11051101
# We get here with 0-dim from sparse
11061102
arr = np.atleast_1d(arr)

pandas/tests/frame/methods/test_astype.py

+20
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,26 @@ def test_astype_bytes(self):
670670
result = DataFrame(["foo", "bar", "baz"]).astype(bytes)
671671
assert result.dtypes[0] == np.dtype("S3")
672672

673+
@pytest.mark.parametrize(
674+
"index_slice",
675+
[
676+
np.s_[:2, :2],
677+
np.s_[:1, :2],
678+
np.s_[:2, :1],
679+
np.s_[::2, ::2],
680+
np.s_[::1, ::2],
681+
np.s_[::2, ::1],
682+
],
683+
)
684+
def test_astype_noncontiguous(self, index_slice):
685+
# GH#42396
686+
data = np.arange(16).reshape(4, 4)
687+
df = DataFrame(data)
688+
689+
result = df.iloc[index_slice].astype("int16")
690+
expected = df.iloc[index_slice]
691+
tm.assert_frame_equal(result, expected, check_dtype=False)
692+
673693

674694
class TestAstypeCategorical:
675695
def test_astype_from_categorical3(self):

0 commit comments

Comments
 (0)