Skip to content

Commit 1b48512

Browse files
jmcomiemeeseeksmachine
authored andcommitted
Backport PR pandas-dev#42823: BUG: Pass copy argument to expanddim constructor in concat.
1 parent b2e5186 commit 1b48512

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

doc/source/whatsnew/v1.3.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Fixed regressions
2323
- Fixed regression where :meth:`pandas.read_csv` raised a ``ValueError`` when parameters ``names`` and ``prefix`` were both set to None (:issue:`42387`)
2424
- Fixed regression in comparisons between :class:`Timestamp` object and ``datetime64`` objects outside the implementation bounds for nanosecond ``datetime64`` (:issue:`42794`)
2525
- Fixed regression in :meth:`.Styler.highlight_min` and :meth:`.Styler.highlight_max` where ``pandas.NA`` was not successfully ignored (:issue:`42650`)
26+
- Fixed regression in :func:`pandas.concat` where ``copy=False`` was not honored in ``axis=1`` Series concatenation (:issue:`42501`)
2627
- Regression in :meth:`Series.nlargest` and :meth:`Series.nsmallest` with nullable integer or float dtype (:issue:`41816`)
2728
- Fixed regression in :meth:`pandas.Series.quantile` with :class:`pandas.Int64Dtype` (:issue:`42626`)
2829

pandas/core/reshape/concat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ def get_result(self):
505505
cons = sample._constructor_expanddim
506506

507507
index, columns = self.new_axes
508-
df = cons(data, index=index)
508+
df = cons(data, index=index, copy=self.copy)
509509
df.columns = columns
510510
return df.__finalize__(self, method="concat")
511511

pandas/tests/extension/decimal/test_decimal.py

+1-12
Original file line numberDiff line numberDiff line change
@@ -261,18 +261,7 @@ def test_dataframe_constructor_with_dtype():
261261
tm.assert_frame_equal(result, expected)
262262

263263

264-
@pytest.mark.parametrize(
265-
"frame",
266-
[
267-
pytest.param(
268-
True,
269-
marks=pytest.mark.xfail(
270-
reason="pd.concat call inside NDFrame.astype reverts the dtype"
271-
),
272-
),
273-
False,
274-
],
275-
)
264+
@pytest.mark.parametrize("frame", [True, False])
276265
def test_astype_dispatches(frame):
277266
# This is a dtype-specific test that ensures Series[decimal].astype
278267
# gets all the way through to ExtensionArray.astype

pandas/tests/frame/methods/test_astype.py

+30
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
option_context,
2424
)
2525
import pandas._testing as tm
26+
from pandas.core.arrays.integer import coerce_to_array
2627

2728

2829
def _check_cast(df, v):
@@ -726,3 +727,32 @@ def test_astype_categorical_to_string_missing(self):
726727
cat = df.astype("category")
727728
result = cat.astype(str)
728729
tm.assert_frame_equal(result, expected)
730+
731+
732+
class IntegerArrayNoCopy(pd.core.arrays.IntegerArray):
733+
# GH 42501
734+
735+
@classmethod
736+
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
737+
values, mask = coerce_to_array(scalars, dtype=dtype, copy=copy)
738+
return IntegerArrayNoCopy(values, mask)
739+
740+
def copy(self):
741+
assert False
742+
743+
744+
class Int16DtypeNoCopy(pd.Int16Dtype):
745+
# GH 42501
746+
747+
@classmethod
748+
def construct_array_type(cls):
749+
return IntegerArrayNoCopy
750+
751+
752+
def test_frame_astype_no_copy():
753+
# GH 42501
754+
df = DataFrame({"a": [1, 4, None, 5], "b": [6, 7, 8, 9]}, dtype=object)
755+
result = df.astype({"a": Int16DtypeNoCopy()}, copy=False)
756+
757+
assert result.a.dtype == pd.Int16Dtype()
758+
assert np.shares_memory(df.b.values, result.b.values)

0 commit comments

Comments
 (0)