-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
PERF/CLN: Preserve concat(keys=range)
RangeIndex level in the result
#57755
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
Changes from 5 commits
9e3dab6
b2ffad3
1384caf
c9258fa
e0c5956
03bc560
54f08ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
Index, | ||
MultiIndex, | ||
PeriodIndex, | ||
RangeIndex, | ||
Series, | ||
concat, | ||
date_range, | ||
|
@@ -395,6 +396,44 @@ def test_concat_keys_with_none(self): | |
expected = concat([df0, df0[:2], df0[:1], df0], keys=["b", "c", "d", "e"]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
@pytest.mark.parametrize("klass", [range, RangeIndex]) | ||
def test_concat_preserves_rangeindex(self, klass): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think you could parametrize with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure thing. Parametrized this test over |
||
df = DataFrame([1, 2]) | ||
df2 = DataFrame([3, 4]) | ||
result = concat([df, df2], keys=klass(2)) | ||
expected = DataFrame( | ||
[1, 2, 3, 4], | ||
index=MultiIndex( | ||
levels=( | ||
RangeIndex(start=0, stop=2, step=1), | ||
RangeIndex(start=0, stop=2, step=1), | ||
), | ||
codes=( | ||
np.array([0, 0, 1, 1], dtype=np.int8), | ||
np.array([0, 1, 0, 1], dtype=np.int8), | ||
), | ||
), | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
df = DataFrame([1, 2]) | ||
df2 = DataFrame([3, 4]) | ||
result = concat([df, None, df2, None], keys=klass(4)) | ||
expected = DataFrame( | ||
[1, 2, 3, 4], | ||
index=MultiIndex( | ||
levels=( | ||
RangeIndex(start=0, stop=4, step=2), | ||
RangeIndex(start=0, stop=2, step=1), | ||
), | ||
codes=( | ||
np.array([0, 0, 1, 1], dtype=np.int8), | ||
np.array([0, 1, 0, 1], dtype=np.int8), | ||
), | ||
), | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_concat_bug_1719(self): | ||
ts1 = Series( | ||
np.arange(10, dtype=np.float64), index=date_range("2020-01-01", periods=10) | ||
|
@@ -705,7 +744,7 @@ def test_concat_multiindex_with_empty_rangeindex(): | |
# GH#41234 | ||
mi = MultiIndex.from_tuples([("B", 1), ("C", 1)]) | ||
df1 = DataFrame([[1, 2]], columns=mi) | ||
df2 = DataFrame(index=[1], columns=pd.RangeIndex(0)) | ||
df2 = DataFrame(index=[1], columns=RangeIndex(0)) | ||
|
||
result = concat([df1, df2]) | ||
expected = DataFrame([[1, 2], [np.nan, np.nan]], columns=mi) | ||
|
@@ -830,14 +869,14 @@ def test_concat_mismatched_keys_length(): | |
sers = [ser + n for n in range(4)] | ||
keys = ["A", "B", "C"] | ||
|
||
msg = r"The behavior of pd.concat with len\(keys\) != len\(objs\) is deprecated" | ||
with tm.assert_produces_warning(FutureWarning, match=msg): | ||
msg = r"The length of the keys" | ||
with pytest.raises(ValueError, match=msg): | ||
concat(sers, keys=keys, axis=1) | ||
with tm.assert_produces_warning(FutureWarning, match=msg): | ||
with pytest.raises(ValueError, match=msg): | ||
concat(sers, keys=keys, axis=0) | ||
with tm.assert_produces_warning(FutureWarning, match=msg): | ||
with pytest.raises(ValueError, match=msg): | ||
concat((x for x in sers), keys=(y for y in keys), axis=1) | ||
with tm.assert_produces_warning(FutureWarning, match=msg): | ||
with pytest.raises(ValueError, match=msg): | ||
concat((x for x in sers), keys=(y for y in keys), axis=0) | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this evolved from the original test. I don't think we need to be testing both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah cool. Removed