Skip to content

BUG: Pass copy argument to expanddim constructor in concat. #42823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Fixed regressions
- Fixed regression where :meth:`pandas.read_csv` raised a ``ValueError`` when parameters ``names`` and ``prefix`` were both set to None (:issue:`42387`)
- Fixed regression in comparisons between :class:`Timestamp` object and ``datetime64`` objects outside the implementation bounds for nanosecond ``datetime64`` (:issue:`42794`)
- Fixed regression in :meth:`.Styler.highlight_min` and :meth:`.Styler.highlight_max` where ``pandas.NA`` was not successfully ignored (:issue:`42650`)
- Fixed regression in :meth:`pandas.concat` where copy=False was not honored in ``axis=1`` Series concatenation. (:issue:`42501`)

.. ---------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/reshape/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ def get_result(self):
cons = sample._constructor_expanddim

index, columns = self.new_axes
df = cons(data, index=index)
df = cons(data, index=index, copy=self.copy)
df.columns = columns
return df.__finalize__(self, method="concat")

Expand Down
13 changes: 1 addition & 12 deletions pandas/tests/extension/decimal/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,7 @@ def test_dataframe_constructor_with_dtype():
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"frame",
[
pytest.param(
True,
marks=pytest.mark.xfail(
reason="pd.concat call inside NDFrame.astype reverts the dtype"
),
),
False,
],
)
@pytest.mark.parametrize("frame", [True, False])
def test_astype_dispatches(frame):
# This is a dtype-specific test that ensures Series[decimal].astype
# gets all the way through to ExtensionArray.astype
Expand Down
30 changes: 30 additions & 0 deletions pandas/tests/frame/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
option_context,
)
import pandas._testing as tm
from pandas.core.arrays.integer import coerce_to_array


def _check_cast(df, v):
Expand Down Expand Up @@ -726,3 +727,32 @@ def test_astype_categorical_to_string_missing(self):
cat = df.astype("category")
result = cat.astype(str)
tm.assert_frame_equal(result, expected)


class IntegerArrayNoCopy(pd.core.arrays.IntegerArray):
# GH 42501

@classmethod
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
values, mask = coerce_to_array(scalars, dtype=dtype, copy=copy)
return IntegerArrayNoCopy(values, mask)

def copy(self):
assert False


class Int16DtypeNoCopy(pd.Int16Dtype):
# GH 42501

@classmethod
def construct_array_type(cls):
return IntegerArrayNoCopy


def test_frame_astype_no_copy():
# GH 42501
df = DataFrame({"a": [1, 4, None, 5], "b": [6, 7, 8, 9]}, dtype=object)
result = df.astype({"a": Int16DtypeNoCopy()}, copy=False)

tm.assert_frame_equal(df, result, check_dtype=False)
tm.assert_numpy_array_equal(df.b.values, result.b.values, check_same="same")