Skip to content

CLN: Add tets and fix docstring for concat with sort=True and outer join #47612

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
Jul 7, 2022
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 pandas/core/indexes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def _unique_indices(inds, dtype) -> Index:
Parameters
----------
inds : list of Index or list objects
dtype : dtype to set for the resulting Index
Copy link
Member

Choose a reason for hiding this comment

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

above, it still says The final dtype is inferred.

Is this still true? It is a positional arg and not an optional kwarg, but we can explicitly pass None to get inference?

Copy link
Member Author

@phofl phofl Jul 8, 2022

Choose a reason for hiding this comment

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

Sorry, yeah if None is passed then we infer, should update docstring to reflect this


Returns
-------
Expand Down
31 changes: 27 additions & 4 deletions pandas/tests/reshape/concat/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,13 +399,36 @@ def test_concat_range_index_result(self):
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):
def test_concat_index_keep_dtype(self):
# GH#47329
df1 = DataFrame([[0, 1, 1]], columns=Index([1, 2, 3], dtype="object"))
df2 = DataFrame([[0, 1]], columns=Index([1, 2], dtype="object"))
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="object")
)
tm.assert_frame_equal(result, expected)

def test_concat_index_keep_dtype_ea_numeric(self, any_numeric_ea_dtype):
# GH#47329
df1 = DataFrame(
[[0, 1, 1]], columns=Index([1, 2, 3], dtype=any_numeric_ea_dtype)
)
df2 = DataFrame([[0, 1]], columns=Index([1, 2], dtype=any_numeric_ea_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=any_numeric_ea_dtype),
)
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("dtype", ["Int8", "Int16", "Int32"])
def test_concat_index_find_common(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))
df2 = DataFrame([[0, 1]], columns=Index([1, 2], dtype="Int32"))
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)
[[0, 1, 1.0], [0, 1, np.nan]], columns=Index([1, 2, 3], dtype="Int32")
)
tm.assert_frame_equal(result, expected)