Skip to content

BUG: assign consensus name to index union in array case GH13475 #35338

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 1 commit into from
Aug 7, 2020
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.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Reshaping
^^^^^^^^^

- 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`)
- Bug in :func:`union_indexes` where input index names are not preserved in some cases. Affects :func:`concat` and :class:`DataFrame` constructor (:issue:`13475`)
-

Sparse
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/indexes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,8 @@ def conv(i):
return result
elif kind == "array":
index = indexes[0]
for other in indexes[1:]:
if not index.equals(other):
return _unique_indices(indexes)
if not all(index.equals(other) for other in indexes[1:]):
index = _unique_indices(indexes)

name = get_consensus_names(indexes)[0]
if name != index.name:
Expand Down
36 changes: 36 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,42 @@ def test_constructor_Series_differently_indexed(self):
tm.assert_index_equal(df2.index, other_index)
tm.assert_frame_equal(df2, exp2)

@pytest.mark.parametrize(
"name_in1,name_in2,name_in3,name_out",
[
("idx", "idx", "idx", "idx"),
("idx", "idx", None, "idx"),
("idx", None, None, "idx"),
("idx1", "idx2", None, None),
("idx1", "idx1", "idx2", None),
("idx1", "idx2", "idx3", None),
(None, None, None, None),
],
)
def test_constructor_index_names(self, name_in1, name_in2, name_in3, name_out):
# GH13475
indices = [
pd.Index(["a", "b", "c"], name=name_in1),
pd.Index(["b", "c", "d"], name=name_in2),
pd.Index(["c", "d", "e"], name=name_in3),
]
series = {
c: pd.Series([0, 1, 2], index=i) for i, c in zip(indices, ["x", "y", "z"])
}
result = pd.DataFrame(series)

exp_ind = pd.Index(["a", "b", "c", "d", "e"], name=name_out)
expected = pd.DataFrame(
{
"x": [0, 1, 2, np.nan, np.nan],
"y": [np.nan, 0, 1, 2, np.nan],
"z": [np.nan, np.nan, 0, 1, 2],
},
index=exp_ind,
)

tm.assert_frame_equal(result, expected)

def test_constructor_manager_resize(self, float_frame):
index = list(float_frame.index[:5])
columns = list(float_frame.columns[:3])
Expand Down
37 changes: 37 additions & 0 deletions pandas/tests/reshape/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,43 @@ def test_concat_ignore_index(self, sort):

tm.assert_frame_equal(v1, expected)

@pytest.mark.parametrize(
"name_in1,name_in2,name_in3,name_out",
[
("idx", "idx", "idx", "idx"),
("idx", "idx", None, "idx"),
("idx", None, None, "idx"),
("idx1", "idx2", None, None),
("idx1", "idx1", "idx2", None),
("idx1", "idx2", "idx3", None),
(None, None, None, None),
],
)
def test_concat_same_index_names(self, name_in1, name_in2, name_in3, name_out):
# GH13475
indices = [
pd.Index(["a", "b", "c"], name=name_in1),
pd.Index(["b", "c", "d"], name=name_in2),
pd.Index(["c", "d", "e"], name=name_in3),
]
frames = [
pd.DataFrame({c: [0, 1, 2]}, index=i)
for i, c in zip(indices, ["x", "y", "z"])
]
result = pd.concat(frames, axis=1)

exp_ind = pd.Index(["a", "b", "c", "d", "e"], name=name_out)
expected = pd.DataFrame(
{
"x": [0, 1, 2, np.nan, np.nan],
"y": [np.nan, 0, 1, 2, np.nan],
"z": [np.nan, np.nan, 0, 1, 2],
},
index=exp_ind,
)

tm.assert_frame_equal(result, expected)

def test_concat_multiindex_with_keys(self):
index = MultiIndex(
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],
Expand Down