Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,14 @@ def astype_nansafe(
ValueError
The dtype was a datetime64/timedelta64 dtype, but it had no unit.
"""
if arr.ndim > 1:
# Make sure we are doing non-copy ravel and reshape.
flags = arr.flags
flat = arr.ravel("K")
result = astype_nansafe(flat, dtype, copy=copy, skipna=skipna)
order = "F" if flags.f_contiguous else "C"
return result.reshape(arr.shape, order=order)

# dispatch on extension dtype if needed
if isinstance(dtype, ExtensionDtype):
return dtype.construct_array_type()._from_sequence(arr, dtype=dtype, copy=copy)
Expand Down
8 changes: 1 addition & 7 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,10 +629,6 @@ def astype(self, dtype, copy: bool = False, errors: str = "raise"):
else:
raise

if isinstance(new_values, np.ndarray):
# TODO(EA2D): special case not needed with 2D EAs
new_values = new_values.reshape(self.shape)

newb = self.make_block(new_values)
if newb.is_numeric and self.is_numeric:
if newb.shape != self.shape:
Expand Down Expand Up @@ -691,9 +687,7 @@ def _astype(self, dtype: DtypeObj, copy: bool) -> ArrayLike:
# We still need to go through astype_nansafe for
# e.g. dtype = Sparse[object, 0]

# astype_nansafe works with 1-d only
vals1d = values.ravel()
values = astype_nansafe(vals1d, dtype, copy=True)
values = astype_nansafe(values, dtype, copy=True)

return values

Expand Down