Skip to content

Commit b58116a

Browse files
dsaxtonmeeseeksmachine
authored andcommitted
Backport PR pandas-dev#35473: REGR: Fix conversion of mixed dtype DataFrame to numpy str
1 parent c1676ce commit b58116a

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

doc/source/whatsnew/v1.1.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ including other versions of pandas.
1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
1717

18+
- Fixed regression where :meth:`DataFrame.to_numpy` would raise a ``RuntimeError`` for mixed dtypes when converting to ``str`` (:issue:`35455`)
1819
- Fixed regression where :func:`read_csv` would raise a ``ValueError`` when ``pandas.options.mode.use_inf_as_na`` was set to ``True`` (:issue:`35493`).
1920
- Fixed regression in :class:`pandas.core.groupby.RollingGroupby` where column selection was ignored (:issue:`35486`)
2021
- Fixed regression in :meth:`DataFrame.shift` with ``axis=1`` and heterogeneous dtypes (:issue:`35488`)

pandas/core/frame.py

+2
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,8 @@ def to_numpy(
13711371
result = self._mgr.as_array(
13721372
transpose=self._AXIS_REVERSED, dtype=dtype, copy=copy, na_value=na_value
13731373
)
1374+
if result.dtype is not dtype:
1375+
result = np.array(result, dtype=dtype, copy=False)
13741376

13751377
return result
13761378

pandas/core/internals/managers.py

+2
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,8 @@ def _interleave(self, dtype=None, na_value=lib.no_default) -> np.ndarray:
867867
dtype = dtype.subtype
868868
elif is_extension_array_dtype(dtype):
869869
dtype = "object"
870+
elif is_dtype_equal(dtype, str):
871+
dtype = "object"
870872

871873
result = np.empty(self.shape, dtype=dtype)
872874

pandas/tests/frame/test_api.py

+7
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,13 @@ def test_to_numpy_copy(self):
367367
assert df.to_numpy(copy=False).base is arr
368368
assert df.to_numpy(copy=True).base is not arr
369369

370+
def test_to_numpy_mixed_dtype_to_str(self):
371+
# https://github.com/pandas-dev/pandas/issues/35455
372+
df = pd.DataFrame([[pd.Timestamp("2020-01-01 00:00:00"), 100.0]])
373+
result = df.to_numpy(dtype=str)
374+
expected = np.array([["2020-01-01 00:00:00", "100.0"]], dtype=str)
375+
tm.assert_numpy_array_equal(result, expected)
376+
370377
def test_swapaxes(self):
371378
df = DataFrame(np.random.randn(10, 5))
372379
tm.assert_frame_equal(df.T, df.swapaxes(0, 1))

0 commit comments

Comments
 (0)