Skip to content

bug in ak.where for ak.Categorical #4881

Closed

Description

@ajpotts

ak.where does not behave as expected in some examples involving ak.Categorical, For example:

In [14]: cat = ak.Categorical(ak.array(["a","a","a","a"]))

In [15]: fv = "None"

In [16]: mask = ak.array([False, True, False, True])

In [17]: ak.where(mask, fv, cat)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[17], line 1
----> 1 ak.where(mask, fv, cat)

File ~/anaconda3/envs/arkouda-dev/lib/python3.13/site-packages/typeguard/__init__.py:891, in typechecked.<locals>.wrapper(*args, **kwargs)
    889 memo = _CallMemo(python_func, _localns, args=args, kwargs=kwargs)
    890 check_argument_types(memo)
--> 891 retval = func(*args, **kwargs)
    892 check_return_type(retval, memo)
    894 # If a generator is returned, wrap it if its yield/send/return types can be checked

File ~/git/arkouda/arkouda/numpy/numeric.py:2048, in where(condition, A, B)
   2039     if (
   2040         not isinstance(A, (str, Strings, Categorical))  # type: ignore
   2041         or not isinstance(B, (str, Strings, Categorical))  # type: ignore
   2042     ):
   2043         # fmt:on
   2044         raise TypeError(
   2045             "both A and B must be an int, np.int64, float, np.float64, pdarray OR"
   2046             " both A and B must be an str, Strings, Categorical"
   2047         )
-> 2048     return _str_cat_where(condition, A, B)
   2050 #   The code below creates a command string for wherevv, wherevs, wheresv or wheress,
   2051 #   based on A and B.
   2053 if isinstance(A, pdarray) and isinstance(B, pdarray):

File ~/git/arkouda/arkouda/numpy/numeric.py:1906, in _str_cat_where(condition, A, B)
   1904         b_code = A.codes.size + 1
   1905     new_codes = where(condition, A.codes, b_code)
-> 1906     return Categorical.from_codes(new_codes, new_categories, NAvalue=A.NAvalue).reset_categories()
   1908 # both cat
   1909 if isinstance(A, Categorical) and isinstance(B, Categorical):

File ~/anaconda3/envs/arkouda-dev/lib/python3.13/site-packages/typeguard/__init__.py:891, in typechecked.<locals>.wrapper(*args, **kwargs)
    889 memo = _CallMemo(python_func, _localns, args=args, kwargs=kwargs)
    890 check_argument_types(memo)
--> 891 retval = func(*args, **kwargs)
    892 check_return_type(retval, memo)
    894 # If a generator is returned, wrap it if its yield/send/return types can be checked

File ~/git/arkouda/arkouda/pandas/categorical.py:316, in Categorical.from_codes(cls, codes, categories, permutation, segments, **kwargs)
    314 if codes.dtype != akint64:
    315     raise TypeError("Codes must be pdarray of int64")
--> 316 return cls(
    317     None,
    318     codes=codes,
    319     categories=categories,
    320     permutation=permutation,
    321     segments=segments,
    322     **kwargs,
    323 )

File ~/git/arkouda/arkouda/pandas/categorical.py:168, in Categorical.__init__(self, values, **kwargs)
    166 self.categories = kwargs["categories"]
    167 if (self.codes.min() < 0) or (self.codes.max() >= self.categories.size):
--> 168     raise ValueError(
    169         f"Codes out of bounds for categories: min = {self.codes.min()},"
    170         f" max = {self.codes.max()}, categories = {self.categories.size}"
    171     )
    172 self.permutation = kwargs.get("permutation", None)
    173 self.segments = kwargs.get("segments", None)

ValueError: Codes out of bounds for categories: min = 0, max = 5, categories = 3

In [18]: ak.where(mask, cat, cat)
Out[18]: array(['a', 'a', 'a', 'a'])

Activity

  1. added
    bug
    on Sep 9, 2025
  2. self-assigned this
    on Sep 16, 2025
  3. added 2 commits that reference this issue on Sep 16, 2025
    db96b8a
    6128643
  4. added a commit that references this issue on Sep 22, 2025
    d9717ed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions