-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Copy categorical codes if empty (fixes #18051) #18279
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 all commits
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 |
---|---|---|
|
@@ -1673,6 +1673,12 @@ def test_unique(self): | |
exp_cat = Categorical(["b", np.nan, "a"], categories=["b", "a"]) | ||
tm.assert_categorical_equal(res, exp_cat) | ||
|
||
# GH 18051 | ||
cat = Categorical([np.nan]) | ||
res = cat.unique() | ||
exp_cat = Categorical([np.nan], categories=[]) | ||
tm.assert_categorical_equal(res, exp_cat) | ||
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. @jreback , do you mean like this? (This is not failing on 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. There should be a test for assert pd.Series(pd.Categorical([np.nan])).nunique() == 0 Note that 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. @topper-123 I have added that test in https://github.com/pandas-dev/pandas/pull/18279/files#diff-ed4f442894a2f521dfac3193a3a8d8a0R2185 (L2185) below. 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. That looks great IMO. Also, you mention above that |
||
|
||
def test_unique_ordered(self): | ||
# keep categories order when ordered=True | ||
cat = Categorical(['b', 'a', 'b'], categories=['a', 'b'], ordered=True) | ||
|
@@ -2174,6 +2180,10 @@ def test_basic(self): | |
result = x.person_name.loc[0] | ||
assert result == expected | ||
|
||
# GH 18051 | ||
s = pd.Series(pd.Categorical([np.nan])) | ||
assert s.nunique() == 0 | ||
|
||
def test_creation_astype(self): | ||
l = ["a", "b", "c", "a"] | ||
s = pd.Series(l) | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
return codes
causes writable flag to beFalse
hence we get the error reported in #18051