Skip to content

ENH: allow attrs to be propagated via pd.concat #42252

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 25 commits into from
Dec 5, 2021
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ representation of :class:`DataFrame` objects (:issue:`4889`).

Other enhancements
^^^^^^^^^^^^^^^^^^
- :meth:`concat` will preserve the ``attrs`` when it is the same for all objects and discard the ``attrs`` when they are different. (:issue:`41828`)
- :class:`DataFrameGroupBy` operations with ``as_index=False`` now correctly retain ``ExtensionDtype`` dtypes for columns being grouped on (:issue:`41373`)
- Add support for assigning values to ``by`` argument in :meth:`DataFrame.plot.hist` and :meth:`DataFrame.plot.box` (:issue:`15079`)
- :meth:`Series.sample`, :meth:`DataFrame.sample`, and :meth:`.GroupBy.sample` now accept a ``np.random.Generator`` as input to ``random_state``. A generator will be more performant, especially with ``replace=False`` (:issue:`38100`)
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5522,6 +5522,12 @@ def __finalize__(
object.__setattr__(self, name, getattr(other, name, None))

if method == "concat":
attrs = other.objs[0].attrs
check_attrs = all(objs.attrs == attrs for objs in other.objs[1:])
if check_attrs:
for name in attrs:
self.attrs[name] = attrs[name]

allows_duplicate_labels = all(
x.flags.allows_duplicate_labels for x in other.objs
)
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/generic/test_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,6 @@ def test_groupby_finalize(obj, method):
"method",
[
lambda x: x.agg(["sum", "count"]),
lambda x: x.transform(lambda y: y),
lambda x: x.apply(lambda y: y),
lambda x: x.agg("std"),
lambda x: x.agg("var"),
Expand Down
46 changes: 46 additions & 0 deletions pandas/tests/reshape/concat/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,3 +698,49 @@ def test_concat_posargs_deprecation():
result = concat([df, df2], 0)
expected = DataFrame([[1, 2, 3], [4, 5, 6]], index=["a", "b"])
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"data",
[
Series(data=[1, 2]),
DataFrame(
data={
"col1": [1, 2],
}
),
DataFrame(dtype=float),
Series(dtype=float),
],
)
def test_concat_drop_attrs(data):
# GH#41828
df1 = data.copy()
df1.attrs = {1: 1}
df2 = data.copy()
df2.attrs = {1: 2}
df = concat([df1, df2])
assert len(df.attrs) == 0


@pytest.mark.parametrize(
"data",
[
Series(data=[1, 2]),
DataFrame(
data={
"col1": [1, 2],
}
),
DataFrame(dtype=float),
Series(dtype=float),
],
)
def test_concat_retain_attrs(data):
# GH#41828
df1 = data.copy()
df1.attrs = {1: 1}
df2 = data.copy()
df2.attrs = {1: 1}
df = concat([df1, df2])
assert df.attrs[1] == 1