Skip to content

Commit 6dfb8c3

Browse files
committed
BUG: Pass copy argument to expanddim constructor in concat.
1 parent 1269bc6 commit 6dfb8c3

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

pandas/core/reshape/concat.py

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

506506
index, columns = self.new_axes
507-
df = cons(data, index=index)
507+
df = cons(data, index=index, copy=self.copy)
508508
df.columns = columns
509509
return df.__finalize__(self, method="concat")
510510

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

+27
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,29 @@ 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({"col": [1, 4, None, 5]}, dtype=object)
755+
df = df.astype({"col": Int16DtypeNoCopy()}, copy=False)

0 commit comments

Comments
 (0)