Skip to content

Conversation

@3keepmovingforward3
Copy link
Contributor

Handles passing empty list by actually checking if there is one element;

rt.Cat([1]).isin([])
_categorical_compare_check raise ValueError("List was empty.")
rt.Cat([1]).isin([1])
FastArray([ True])
rt.Cat([1]).isin([2])
FastArray([False])
rt.Cat([1]).isin([1,2])
FastArray([ True])

Handles passing empty list by actually checking if there is one element; 
>>>>rt.Cat([1]).isin([]) 
_categorical_compare_check raise ValueError("List was empty.")
>>> rt.Cat([1]).isin([1])
FastArray([ True])
>>> rt.Cat([1]).isin([2])
FastArray([False])
>>> rt.Cat([1]).isin([1,2])
FastArray([ True])
@jack-pappas
Copy link
Collaborator

Thanks! Looks like this is to fix #105, is that right?

Would you mind adding a quick unit test for this case (of the empty input list), both to demonstrate it works correctly and also to ensure it stays working correctly if anyone's making changes to the Categorical code in the future?

@3keepmovingforward3
Copy link
Contributor Author

3keepmovingforward3 commented Mar 23, 2021 via email

@3keepmovingforward3
Copy link
Contributor Author

@jack-pappas it is a fix for #105, working on unit test now

@3keepmovingforward3
Copy link
Contributor Author

@jack-pappas it fails under test_categorical.py line 1606-1611:

def test_empty_init(self):
...
with pytest.raises(ValueError): c = Categorical([])

Is there something else I can do? Do you want it to be more explicit?

@3keepmovingforward3
Copy link
Contributor Author

File "", line 1, in
File "/home/bblouin/.pyenv/versions/anaconda3-2020.11/envs/rtdev/lib/python3.9/site-packages/riptable-1.0.42.post3+gb6455da.d20210324-py3.9.egg/riptable/rt_categorical.py", line 3110, in isin
return self == x
File "/home/bblouin/.pyenv/versions/anaconda3-2020.11/envs/rtdev/lib/python3.9/site-packages/riptable-1.0.42.post3+gb6455da.d20210324-py3.9.egg/riptable/rt_categorical.py", line 3698, in eq
return self._categorical_compare_check('eq', other)
File "/home/bblouin/.pyenv/versions/anaconda3-2020.11/envs/rtdev/lib/python3.9/site-packages/riptable-1.0.42.post3+gb6455da.d20210324-py3.9.egg/riptable/rt_categorical.py", line 3661, in _categorical_compare_check
raise ValueError("List was empty.")

ValueError: List was empty.

@jack-pappas
Copy link
Collaborator

@3keepmovingforward3 Sorry about taking so long to respond to your question, I was caught up with some other projects this past week.

The test at test_categorical.py line 1606-1611 is for creating a Categorical from an empty list. Your change was to fix the .isin() method being called with an empty list on a non-empty Categorical though. I don't think we have a test for that case though -- the test_multikey_categorical_isin() function at the bottom of test_categorical.py does test some cases of .isin() but it's best to add a new test (at the bottom of the file is fine) to verify the new behavior + nail down the expected behavior in case someone accidentally makes a change in the future which breaks this case. For example, something like the following (which you can just add to the bottom of test_categorical.py, update your PR and I'll merge):

@pytest.mark.parametrize(
    'cat',
    [
        pytest.param(rt.Cat(['red' , 'green', 'blue', 'green', 'red', 'red', 'blue', 'green']), id="single-key string cat"),
    ]
)
@pytest.mark.parametrize(
    'seq',
    [
        pytest.param([], id='list'),
        pytest.param(set(), id='set'),
        pytest.param(rt.FA([]), id='FastArray'),
    ]
)
def test_categorical_isin_empty(cat: rt.Categorical, seq) -> None:
    # Test the .isin() method on a Categorical produces an empty result when called with an empty array-like input.
    result = cat.isin(seq)
    assert_array_equal(rt.FA([]), result)

@jack-pappas jack-pappas added area-categorical Issue relates to the Categorical class bug Something isn't working labels Apr 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-categorical Issue relates to the Categorical class bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants