Skip to content

Commit 245cd11

Browse files
authored
REF: handle ravel inside astype_nansafe (#38507)
1 parent af45b0a commit 245cd11

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

pandas/core/dtypes/cast.py

+8
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,14 @@ def astype_nansafe(
973973
ValueError
974974
The dtype was a datetime64/timedelta64 dtype, but it had no unit.
975975
"""
976+
if arr.ndim > 1:
977+
# Make sure we are doing non-copy ravel and reshape.
978+
flags = arr.flags
979+
flat = arr.ravel("K")
980+
result = astype_nansafe(flat, dtype, copy=copy, skipna=skipna)
981+
order = "F" if flags.f_contiguous else "C"
982+
return result.reshape(arr.shape, order=order)
983+
976984
# dispatch on extension dtype if needed
977985
if isinstance(dtype, ExtensionDtype):
978986
return dtype.construct_array_type()._from_sequence(arr, dtype=dtype, copy=copy)

pandas/core/internals/blocks.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -629,10 +629,6 @@ def astype(self, dtype, copy: bool = False, errors: str = "raise"):
629629
else:
630630
raise
631631

632-
if isinstance(new_values, np.ndarray):
633-
# TODO(EA2D): special case not needed with 2D EAs
634-
new_values = new_values.reshape(self.shape)
635-
636632
newb = self.make_block(new_values)
637633
if newb.is_numeric and self.is_numeric:
638634
if newb.shape != self.shape:
@@ -691,9 +687,7 @@ def _astype(self, dtype: DtypeObj, copy: bool) -> ArrayLike:
691687
# We still need to go through astype_nansafe for
692688
# e.g. dtype = Sparse[object, 0]
693689

694-
# astype_nansafe works with 1-d only
695-
vals1d = values.ravel()
696-
values = astype_nansafe(vals1d, dtype, copy=True)
690+
values = astype_nansafe(values, dtype, copy=True)
697691

698692
return values
699693

0 commit comments

Comments
 (0)