Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
BUG: concat losing columns dtypes for join=outer
  • Loading branch information
phofl committed Jul 2, 2022
commit 75d4fda70f9c47ce7e07a75dc7cb424b51e8ffb1
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@ Reshaping
- Bug in :func:`get_dummies` that selected object and categorical dtypes but not string (:issue:`44965`)
- Bug in :meth:`DataFrame.align` when aligning a :class:`MultiIndex` to a :class:`Series` with another :class:`MultiIndex` (:issue:`46001`)
- Bug in concatenation with ``IntegerDtype``, or ``FloatingDtype`` arrays where the resulting dtype did not mirror the behavior of the non-nullable dtypes (:issue:`46379`)
- Bug in :func:`concat` losing dtype of columns when ``join="outer"`` and ``sort=True`` (:issue:`47329`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue is marked as 1.4.4. ok to leave in 1.5 though (if so just change the issue and ignore this comment)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the milestones.

initially we though this happens only
for ea dtypes, but this was wrong. Occurs also for numpy dtypes

- Bug in :func:`concat` not sorting the column names when ``None`` is included (:issue:`47331`)
- Bug in :func:`concat` with identical key leads to error when indexing :class:`MultiIndex` (:issue:`46519`)
- Bug in :meth:`DataFrame.join` with a list when using suffixes to join DataFrames with duplicate column names (:issue:`46396`)
Expand Down
18 changes: 14 additions & 4 deletions pandas/core/indexes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)
from pandas.errors import InvalidIndexError

from pandas.core.dtypes.cast import find_common_type
from pandas.core.dtypes.common import is_dtype_equal

from pandas.core.algorithms import safe_sort
Expand Down Expand Up @@ -223,7 +224,7 @@ def union_indexes(indexes, sort: bool | None = True) -> Index:

indexes, kind = _sanitize_and_check(indexes)

def _unique_indices(inds) -> Index:
def _unique_indices(inds, dtype) -> Index:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the docstring also needs updating at some point.

"""
Convert indexes to lists and concatenate them, removing duplicates.

Expand All @@ -243,7 +244,10 @@ def conv(i):
i = i.tolist()
return i

return Index(lib.fast_unique_multiple_list([conv(i) for i in inds], sort=sort))
return Index(
lib.fast_unique_multiple_list([conv(i) for i in inds], sort=sort),
dtype=dtype,
)

if kind == "special":
result = indexes[0]
Expand Down Expand Up @@ -283,16 +287,22 @@ def conv(i):
return result

elif kind == "array":
dtype = find_common_type(
[idx.dtype for idx in indexes if isinstance(idx, Index)]
)
index = indexes[0]
if not all(index.equals(other) for other in indexes[1:]):
index = _unique_indices(indexes)
index = _unique_indices(indexes, dtype)

name = get_unanimous_names(*indexes)[0]
if name != index.name:
index = index.rename(name)
return index
else: # kind='list'
return _unique_indices(indexes)
dtype = find_common_type(
[idx.dtype for idx in indexes if isinstance(idx, Index)]
)
return _unique_indices(indexes, dtype)


def _sanitize_and_check(indexes):
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/reshape/concat/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,14 @@ def test_concat_range_index_result(self):
tm.assert_frame_equal(result, expected)
expected_index = pd.RangeIndex(0, 2)
tm.assert_index_equal(result.index, expected_index, exact=True)

@pytest.mark.parametrize("dtype", ["Int64", "object"])
def test_concat_index_keep_dtype(self, dtype):
# GH#47329
df1 = DataFrame([[0, 1, 1]], columns=Index([1, 2, 3], dtype=dtype))
df2 = DataFrame([[0, 1]], columns=Index([1, 2], dtype=dtype))
result = concat([df1, df2], ignore_index=True, join="outer", sort=True)
expected = DataFrame(
[[0, 1, 1.0], [0, 1, np.nan]], columns=Index([1, 2, 3], dtype=dtype)
)
tm.assert_frame_equal(result, expected)