-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Fix construction of Categorical from pd.NA #31939
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 15 commits
99dbff4
81516a6
78d62f9
38fede6
52466ab
1a71728
bad5be3
b051bf0
563b673
9066789
7da4e44
d1a953b
baab1d5
062f5f7
2d45b21
14a737d
f0eb9f3
a54fe0d
17de660
78e38ec
0efcdb0
a04df9b
3c5082e
d50f963
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 |
---|---|---|
|
@@ -458,6 +458,14 @@ def test_constructor_with_categorical_categories(self): | |
result = Categorical(["a", "b"], categories=CategoricalIndex(["a", "b", "c"])) | ||
tm.assert_categorical_equal(result, expected) | ||
|
||
def test_construction_with_null(self, nulls_fixture): | ||
# https://github.com/pandas-dev/pandas/issues/31927 | ||
values = ["a", nulls_fixture, "b"] | ||
result = Categorical(np.array(values, dtype=object)) | ||
expected = Categorical(values) | ||
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. I am not sure this is a very good test. I mean: it is testing that lists vs object array are giving the same result (which is useful anyhow, as those should be consistent), but it is not testing how they are now constructed (eg it won't "preserve" pd.NA, and this is also not tested) 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. @dsaxton can you parameterize this on klass (np.array and list), then hard code the results in a categorical (meaning use _from_codes and an explict list of categories) |
||
|
||
tm.assert_categorical_equal(result, expected) | ||
|
||
def test_from_codes(self): | ||
|
||
# too few categories | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,3 +111,13 @@ def test_nested_tuples_duplicates(self): | |
df3 = df.copy(deep=True) | ||
df3.loc[[(dti[0], "a")], "c2"] = 1.0 | ||
tm.assert_frame_equal(df3, expected) | ||
|
||
def test_multiindex_from_product_contains_na(self): | ||
# https://github.com/pandas-dev/pandas/issues/31883 | ||
values = [np.array([0.0, pd.NA], dtype="object"), ["a", "b"]] | ||
tuples = [(0.0, "a"), (0.0, "b"), (np.nan, "a"), (np.nan, "b")] | ||
|
||
result = pd.MultiIndex.from_product(values) | ||
expected = pd.MultiIndex.from_tuples(tuples) | ||
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. this is not correct, pd.NA should be preserved. 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. this might be a non-trivial patch and i would separate it from this PR. 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. remove this issue from the PR as this is not correct. |
||
|
||
tm.assert_index_equal(result, expected) |
Uh oh!
There was an error while loading. Please reload this page.