-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
TST: split up test_concat.py #37243 - more follows up #37387
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
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3903d6f
TST: split up test_concat.py #37243
kamilt5 990575d
TST: split up test_concat.py #37243
kamilt5 a06c50f
TST: split up test_concat.py #37243
kamilt5 1d2e424
TST: split up test_concat.py #37243
kamilt5 73d54f5
Merge branch 'master' into i37243_more_follows_up
kamilt5 84d0415
TST: split up test_concat.py #37243
kamilt5 743811f
Merge branch 'i37243_more_follows_up' of https://github.com/laffite56…
kamilt5 ca9a84c
TST: split up test_concat.py #37243
kamilt5 e995aba
Merge branch 'master' into i37243_more_follows_up
kamilt5 ad7a7c6
TST: split up test_concat.py #37243
kamilt5 faa0e0b
TST: split up test_concat.py #37243
kamilt5 665d932
TST: split up test_concat.py #37243
kamilt5 6acddc1
Merge branch 'master' into i37243_more_follows_up
kamilt5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas.core.dtypes.dtypes import CategoricalDtype | ||
|
||
import pandas as pd | ||
from pandas import Categorical, DataFrame, Series | ||
import pandas._testing as tm | ||
|
||
|
||
@pytest.fixture(params=[True, False]) | ||
def sort(request): | ||
"""Boolean sort keyword for concat and DataFrame.append.""" | ||
return request.param | ||
|
||
|
||
class TestCategoricalConcat: | ||
def test_categorical_concat(self, sort): | ||
# See GH 10177 | ||
df1 = DataFrame( | ||
np.arange(18, dtype="int64").reshape(6, 3), columns=["a", "b", "c"] | ||
) | ||
|
||
df2 = DataFrame(np.arange(14, dtype="int64").reshape(7, 2), columns=["a", "c"]) | ||
|
||
cat_values = ["one", "one", "two", "one", "two", "two", "one"] | ||
df2["h"] = Series(Categorical(cat_values)) | ||
|
||
res = pd.concat((df1, df2), axis=0, ignore_index=True, sort=sort) | ||
exp = DataFrame( | ||
{ | ||
"a": [0, 3, 6, 9, 12, 15, 0, 2, 4, 6, 8, 10, 12], | ||
"b": [ | ||
1, | ||
4, | ||
7, | ||
10, | ||
13, | ||
16, | ||
np.nan, | ||
np.nan, | ||
np.nan, | ||
np.nan, | ||
np.nan, | ||
np.nan, | ||
np.nan, | ||
], | ||
"c": [2, 5, 8, 11, 14, 17, 1, 3, 5, 7, 9, 11, 13], | ||
"h": [None] * 6 + cat_values, | ||
} | ||
) | ||
tm.assert_frame_equal(res, exp) | ||
|
||
def test_categorical_concat_dtypes(self): | ||
|
||
# GH8143 | ||
index = ["cat", "obj", "num"] | ||
cat = Categorical(["a", "b", "c"]) | ||
obj = Series(["a", "b", "c"]) | ||
num = Series([1, 2, 3]) | ||
df = pd.concat([Series(cat), obj, num], axis=1, keys=index) | ||
|
||
result = df.dtypes == "object" | ||
expected = Series([False, True, False], index=index) | ||
tm.assert_series_equal(result, expected) | ||
|
||
result = df.dtypes == "int64" | ||
expected = Series([False, False, True], index=index) | ||
tm.assert_series_equal(result, expected) | ||
|
||
result = df.dtypes == "category" | ||
expected = Series([True, False, False], index=index) | ||
tm.assert_series_equal(result, expected) | ||
|
||
def test_concat_categoricalindex(self): | ||
# GH 16111, categories that aren't lexsorted | ||
categories = [9, 0, 1, 2, 3] | ||
|
||
a = Series(1, index=pd.CategoricalIndex([9, 0], categories=categories)) | ||
b = Series(2, index=pd.CategoricalIndex([0, 1], categories=categories)) | ||
c = Series(3, index=pd.CategoricalIndex([1, 2], categories=categories)) | ||
|
||
result = pd.concat([a, b, c], axis=1) | ||
|
||
exp_idx = pd.CategoricalIndex([9, 0, 1, 2], categories=categories) | ||
exp = DataFrame( | ||
{ | ||
0: [1, 1, np.nan, np.nan], | ||
1: [np.nan, 2, 2, np.nan], | ||
2: [np.nan, np.nan, 3, 3], | ||
}, | ||
columns=[0, 1, 2], | ||
index=exp_idx, | ||
) | ||
tm.assert_frame_equal(result, exp) | ||
|
||
def test_categorical_concat_preserve(self): | ||
|
||
# GH 8641 series concat not preserving category dtype | ||
# GH 13524 can concat different categories | ||
s = Series(list("abc"), dtype="category") | ||
s2 = Series(list("abd"), dtype="category") | ||
|
||
exp = Series(list("abcabd")) | ||
res = pd.concat([s, s2], ignore_index=True) | ||
tm.assert_series_equal(res, exp) | ||
|
||
exp = Series(list("abcabc"), dtype="category") | ||
res = pd.concat([s, s], ignore_index=True) | ||
tm.assert_series_equal(res, exp) | ||
|
||
exp = Series(list("abcabc"), index=[0, 1, 2, 0, 1, 2], dtype="category") | ||
res = pd.concat([s, s]) | ||
tm.assert_series_equal(res, exp) | ||
|
||
a = Series(np.arange(6, dtype="int64")) | ||
b = Series(list("aabbca")) | ||
|
||
df2 = DataFrame({"A": a, "B": b.astype(CategoricalDtype(list("cab")))}) | ||
res = pd.concat([df2, df2]) | ||
exp = DataFrame( | ||
{ | ||
"A": pd.concat([a, a]), | ||
"B": pd.concat([b, b]).astype(CategoricalDtype(list("cab"))), | ||
} | ||
) | ||
tm.assert_frame_equal(res, exp) | ||
|
||
def test_categorical_index_preserver(self): | ||
|
||
a = Series(np.arange(6, dtype="int64")) | ||
b = Series(list("aabbca")) | ||
|
||
df2 = DataFrame( | ||
{"A": a, "B": b.astype(CategoricalDtype(list("cab")))} | ||
).set_index("B") | ||
result = pd.concat([df2, df2]) | ||
expected = DataFrame( | ||
{ | ||
"A": pd.concat([a, a]), | ||
"B": pd.concat([b, b]).astype(CategoricalDtype(list("cab"))), | ||
} | ||
).set_index("B") | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# wrong categories | ||
df3 = DataFrame( | ||
{"A": a, "B": Categorical(b, categories=list("abe"))} | ||
).set_index("B") | ||
msg = "categories must match existing categories when appending" | ||
with pytest.raises(TypeError, match=msg): | ||
pd.concat([df2, df3]) | ||
|
||
def test_concat_categorical_tz(self): | ||
# GH-23816 | ||
a = Series(pd.date_range("2017-01-01", periods=2, tz="US/Pacific")) | ||
b = Series(["a", "b"], dtype="category") | ||
result = pd.concat([a, b], ignore_index=True) | ||
expected = Series( | ||
[ | ||
pd.Timestamp("2017-01-01", tz="US/Pacific"), | ||
pd.Timestamp("2017-01-02", tz="US/Pacific"), | ||
"a", | ||
"b", | ||
] | ||
) | ||
tm.assert_series_equal(result, expected) | ||
|
||
def test_concat_categorical_unchanged(self): | ||
# GH-12007 | ||
# test fix for when concat on categorical and float | ||
# coerces dtype categorical -> float | ||
df = DataFrame(Series(["a", "b", "c"], dtype="category", name="A")) | ||
ser = Series([0, 1, 2], index=[0, 1, 3], name="B") | ||
result = pd.concat([df, ser], axis=1) | ||
expected = DataFrame( | ||
{ | ||
"A": Series(["a", "b", "c", np.nan], dtype="category"), | ||
"B": Series([0, 1, np.nan, 2], dtype="float"), | ||
} | ||
) | ||
tm.assert_equal(result, expected) | ||
|
||
def test_categorical_concat_gh7864(self): | ||
# GH 7864 | ||
# make sure ordering is preserved | ||
df = DataFrame({"id": [1, 2, 3, 4, 5, 6], "raw_grade": list("abbaae")}) | ||
df["grade"] = Categorical(df["raw_grade"]) | ||
df["grade"].cat.set_categories(["e", "a", "b"]) | ||
|
||
df1 = df[0:3] | ||
df2 = df[3:] | ||
|
||
tm.assert_index_equal(df["grade"].cat.categories, df1["grade"].cat.categories) | ||
tm.assert_index_equal(df["grade"].cat.categories, df2["grade"].cat.categories) | ||
|
||
dfx = pd.concat([df1, df2]) | ||
tm.assert_index_equal(df["grade"].cat.categories, dfx["grade"].cat.categories) | ||
|
||
dfa = df1.append(df2) | ||
tm.assert_index_equal(df["grade"].cat.categories, dfa["grade"].cat.categories) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
i would move fixtures to a concat/conftest.py (unless they are truly specific to a particular file)