-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Conversation
…ith missing data pandas-dev#20714 SOLVED
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') |
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.
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')
...
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.
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?
can you merge master and update |
@manuhortet do you still want to take this on? @jreback if we don't hear back I will take this |
@manuhortet or @alimcmaster1 if interested pls merge master |
merge master and update |
closing as stale if you want to continue working, please ping. |
git diff upstream/master -u -- "*.py" | flake8 --diff