Skip to content

Inconsistent behaviour when apply() used on categorical with NaN values #22191

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

Closed
wants to merge 1 commit into from

Conversation

manuhortet
Copy link

@manuhortet manuhortet commented Aug 3, 2018

s1 = pd.Series(['1-1', '1-1', np.NaN], dtype='category')
s1 = s1.apply(lambda x: x.split('-')[0])

s2 = pd.Series(['1', '1', np.NaN], dtype='category')
Copy link
Member

Choose a reason for hiding this comment

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

Can you use the result/expected variable naming convention for this test, e.g.

s = pd.Series(['1-1', '1-1', np.NaN], dtype='category')
result = s.apply(lambda x: x.split('-')[0])
expected = pd.Series(['1', '1', np.NaN], dtype='category')

Additionally, the second example in the issue is the one that needs to be tested: pd.Series(['1-1','1-2',np.NaN], dtype='category'). The example you're testing is already working, so it doesn't test if the bug was actually fixed. Perhaps you could parametrize over the values of the Series and test both since expected is the same in both cases, e.g.

@pytest.mark.parametrize('values', [
    ['1-1', '1-1', np.NaN],
    ['1-1', '1-2', np.NaN]])
def test_apply_categorical_with_nan_values(self, values):
    # GH 20714
    s = pd.Series(values, dtype='category')
    ...

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the indications @jschendel !

Anyway I found another problem, doing:

@pytest.mark.parametrize('values', [
    ['1-1', '1-1', np.NaN],
    ['1-1', '1-2', np.NaN]])
def test_apply_categorical_with_nan_values(self, values):
s = pd.Series(values, dtype='category')
result = s.apply(lambda x: x.split('-')[0])
expected = pd.Series(['1', '1', np.NaN], dtype='category')
tm.assert_series_equal(result, expected, check_dtype=False)

I get an error due to the different classes of result and expected:

AssertionError: Categorical Expected type <class 'pandas.core.arrays.categorical.Categorical'>, found <class 'numpy.ndarray'> instead

This situation was already commented on issue #20714 and remarked as out of scope for this PR.
Knowing that, this test should pass. I thought I could do .astype('category') on result, but maybe I'm just missing another assert function inside tm that will work for this. Any ideas?

@jschendel jschendel added Bug Categorical Categorical Data Type labels Aug 3, 2018
@jreback
Copy link
Contributor

jreback commented Nov 1, 2018

can you merge master and update

@alimcmaster1
Copy link
Member

@manuhortet do you still want to take this on? @jreback if we don't hear back I will take this

@jreback
Copy link
Contributor

jreback commented Dec 3, 2018

@manuhortet or @alimcmaster1 if interested pls merge master

@jreback
Copy link
Contributor

jreback commented Dec 23, 2018

merge master and update

@jreback
Copy link
Contributor

jreback commented Jan 14, 2019

closing as stale if you want to continue working, please ping.

@jreback jreback closed this Jan 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent behaviour when calling apply() on a categorical column with missing data
4 participants