Skip to content

Commit 92bf41a

Browse files
authored
BUG: assign consensus name to index union in array case GH13475 (#35338)
1 parent 16bd49d commit 92bf41a

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ Reshaping
162162
^^^^^^^^^
163163

164164
- Bug in :meth:`DataFrame.pivot_table` with ``aggfunc='count'`` or ``aggfunc='sum'`` returning ``NaN`` for missing categories when pivoted on a ``Categorical``. Now returning ``0`` (:issue:`31422`)
165+
- Bug in :func:`union_indexes` where input index names are not preserved in some cases. Affects :func:`concat` and :class:`DataFrame` constructor (:issue:`13475`)
165166
-
166167

167168
Sparse

pandas/core/indexes/api.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,8 @@ def conv(i):
218218
return result
219219
elif kind == "array":
220220
index = indexes[0]
221-
for other in indexes[1:]:
222-
if not index.equals(other):
223-
return _unique_indices(indexes)
221+
if not all(index.equals(other) for other in indexes[1:]):
222+
index = _unique_indices(indexes)
224223

225224
name = get_consensus_names(indexes)[0]
226225
if name != index.name:

pandas/tests/frame/test_constructors.py

+36
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,42 @@ def test_constructor_Series_differently_indexed(self):
16181618
tm.assert_index_equal(df2.index, other_index)
16191619
tm.assert_frame_equal(df2, exp2)
16201620

1621+
@pytest.mark.parametrize(
1622+
"name_in1,name_in2,name_in3,name_out",
1623+
[
1624+
("idx", "idx", "idx", "idx"),
1625+
("idx", "idx", None, "idx"),
1626+
("idx", None, None, "idx"),
1627+
("idx1", "idx2", None, None),
1628+
("idx1", "idx1", "idx2", None),
1629+
("idx1", "idx2", "idx3", None),
1630+
(None, None, None, None),
1631+
],
1632+
)
1633+
def test_constructor_index_names(self, name_in1, name_in2, name_in3, name_out):
1634+
# GH13475
1635+
indices = [
1636+
pd.Index(["a", "b", "c"], name=name_in1),
1637+
pd.Index(["b", "c", "d"], name=name_in2),
1638+
pd.Index(["c", "d", "e"], name=name_in3),
1639+
]
1640+
series = {
1641+
c: pd.Series([0, 1, 2], index=i) for i, c in zip(indices, ["x", "y", "z"])
1642+
}
1643+
result = pd.DataFrame(series)
1644+
1645+
exp_ind = pd.Index(["a", "b", "c", "d", "e"], name=name_out)
1646+
expected = pd.DataFrame(
1647+
{
1648+
"x": [0, 1, 2, np.nan, np.nan],
1649+
"y": [np.nan, 0, 1, 2, np.nan],
1650+
"z": [np.nan, np.nan, 0, 1, 2],
1651+
},
1652+
index=exp_ind,
1653+
)
1654+
1655+
tm.assert_frame_equal(result, expected)
1656+
16211657
def test_constructor_manager_resize(self, float_frame):
16221658
index = list(float_frame.index[:5])
16231659
columns = list(float_frame.columns[:3])

pandas/tests/reshape/test_concat.py

+37
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,43 @@ def test_concat_ignore_index(self, sort):
12791279

12801280
tm.assert_frame_equal(v1, expected)
12811281

1282+
@pytest.mark.parametrize(
1283+
"name_in1,name_in2,name_in3,name_out",
1284+
[
1285+
("idx", "idx", "idx", "idx"),
1286+
("idx", "idx", None, "idx"),
1287+
("idx", None, None, "idx"),
1288+
("idx1", "idx2", None, None),
1289+
("idx1", "idx1", "idx2", None),
1290+
("idx1", "idx2", "idx3", None),
1291+
(None, None, None, None),
1292+
],
1293+
)
1294+
def test_concat_same_index_names(self, name_in1, name_in2, name_in3, name_out):
1295+
# GH13475
1296+
indices = [
1297+
pd.Index(["a", "b", "c"], name=name_in1),
1298+
pd.Index(["b", "c", "d"], name=name_in2),
1299+
pd.Index(["c", "d", "e"], name=name_in3),
1300+
]
1301+
frames = [
1302+
pd.DataFrame({c: [0, 1, 2]}, index=i)
1303+
for i, c in zip(indices, ["x", "y", "z"])
1304+
]
1305+
result = pd.concat(frames, axis=1)
1306+
1307+
exp_ind = pd.Index(["a", "b", "c", "d", "e"], name=name_out)
1308+
expected = pd.DataFrame(
1309+
{
1310+
"x": [0, 1, 2, np.nan, np.nan],
1311+
"y": [np.nan, 0, 1, 2, np.nan],
1312+
"z": [np.nan, np.nan, 0, 1, 2],
1313+
},
1314+
index=exp_ind,
1315+
)
1316+
1317+
tm.assert_frame_equal(result, expected)
1318+
12821319
def test_concat_multiindex_with_keys(self):
12831320
index = MultiIndex(
12841321
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],

0 commit comments

Comments
 (0)