Skip to content

Commit 4ebc5a6

Browse files
committed
whatsnew naming example; split off new tests
1 parent d9305d4 commit 4ebc5a6

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

doc/source/whatsnew/v1.2.0.rst

+19
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,25 @@ Beginning with this version, the default is now to use the more accurate parser
109109
``floating_precision="legacy"`` to use the legacy parser. The change to using the higher precision
110110
parser by default should have no impact on performance. (:issue:`17154`)
111111

112+
.. _whatsnew_120.index_name_preservation:
113+
114+
Index/column name preservation when aggregating
115+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
116+
117+
When aggregating using :meth:`concat` or the :class:`DataFrame` constructor, Pandas
118+
will attempt to preserve index (and column) names whenever possible. In the case where
119+
all inputs share a common name, this name will be assigned to the result. When the input
120+
names do not all agree, the result name unnamed.
121+
122+
.. ipython:: python
123+
124+
idx = pd.Index(range(5), name='abc')
125+
ser = pd.Series(range(5, 10), index=idx)
126+
pd.concat({'x': ser[1:], 'y': ser[:-1]}, axis=1)
127+
128+
The same is true for :class:`MultiIndex`, but the logic is applied separately on a
129+
level-by-level basis.
130+
112131
.. _whatsnew_120.enhancements.other:
113132

114133
Other enhancements

pandas/tests/indexes/test_common.py

+28
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,20 @@ def test_corner_union(self, index, fname, sname, expected_name):
124124
expected = index.drop(index).set_names(expected_name)
125125
tm.assert_index_equal(union, expected)
126126

127+
@pytest.mark.parametrize(
128+
"fname, sname, expected_name",
129+
[
130+
("A", "A", "A"),
131+
("A", "B", None),
132+
("A", None, None),
133+
(None, "B", None),
134+
(None, None, None),
135+
],
136+
)
137+
def test_union_unequal(self, index, fname, sname, expected_name):
138+
if isinstance(index, MultiIndex) or not index.is_unique:
139+
pytest.skip("Not for MultiIndex or repeated indices")
140+
127141
# test copy.union(subset) - need sort for unicode and string
128142
first = index.copy().set_names(fname)
129143
second = index[1:].set_names(sname)
@@ -176,6 +190,20 @@ def test_corner_intersect(self, index, fname, sname, expected_name):
176190
expected = index.drop(index).set_names(expected_name)
177191
tm.assert_index_equal(intersect, expected)
178192

193+
@pytest.mark.parametrize(
194+
"fname, sname, expected_name",
195+
[
196+
("A", "A", "A"),
197+
("A", "B", None),
198+
("A", None, None),
199+
(None, "B", None),
200+
(None, None, None),
201+
],
202+
)
203+
def test_intersect_unequal(self, index, fname, sname, expected_name):
204+
if isinstance(index, MultiIndex) or not index.is_unique:
205+
pytest.skip("Not for MultiIndex or repeated indices")
206+
179207
# test copy.intersection(subset) - need sort for unicode and string
180208
first = index.copy().set_names(fname)
181209
second = index[1:].set_names(sname)

0 commit comments

Comments
 (0)