Skip to content
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

Closes #1540 - Adds .objType and .dtype to Categorical #1541

Merged
merged 5 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions arkouda/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from arkouda.client import *
from arkouda.client_dtypes import *
from arkouda.dtypes import *
from arkouda.decorators import *
from arkouda.pdarrayclass import *
from arkouda.sorting import *
from arkouda.pdarraysetops import *
Expand Down
10 changes: 8 additions & 2 deletions arkouda/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

import numpy as np # type: ignore
from typeguard import typechecked
from arkouda.decorators import objtypedec

from arkouda.dtypes import bool as akbool
from arkouda.dtypes import int64 as akint64
from arkouda.dtypes import int_scalars, resolve_scalar_dtype, str_scalars
from arkouda.dtypes import int_scalars, resolve_scalar_dtype, str_scalars, str_
from arkouda.groupbyclass import GroupBy, unique
from arkouda.infoclass import information, list_registry
from arkouda.logger import getArkoudaLogger
Expand All @@ -39,6 +40,7 @@
__all__ = ["Categorical"]


@objtypedec
class Categorical:
"""
Represents an array of values belonging to named categories. Converting a
Expand Down Expand Up @@ -77,7 +79,6 @@ class Categorical:
BinOps = frozenset(["==", "!="])
RegisterablePieces = frozenset(["categories", "codes", "permutation", "segments", "_akNAcode"])
RequiredPieces = frozenset(["categories", "codes", "_akNAcode"])
objtype = "category"
permutation = None
segments = None

Expand Down Expand Up @@ -137,8 +138,13 @@ def __init__(self, values, **kwargs) -> None:
self.nlevels = self.categories.size
self.ndim = self.codes.ndim
self.shape = self.codes.shape
self.dtype = str_
self.name: Optional[str] = None

@property
def objtype(self):
return self.objtype

@classmethod
@typechecked
def from_codes(
Expand Down
3 changes: 3 additions & 0 deletions arkouda/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def objtypedec(orig_cls):
orig_cls.objtype = orig_cls.__name__
return orig_cls
2 changes: 1 addition & 1 deletion src/AryUtil.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ module AryUtil
thisSize = g.size;
hasStr = true;
}
when "category" {
when "Categorical" {
// passed only Categorical.codes.name to be sorted on
var g = getGenericTypedArrayEntry(name, st);
thisSize = g.size;
Expand Down
4 changes: 2 additions & 2 deletions src/UniqueMsg.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ module UniqueMsg
for (name, objtype, i) in zip(names, types, 0..) {
var newName = st.nextName();
select objtype {
when "pdarray", "category" {
when "pdarray", "Categorical" {
var g = getGenericTypedArrayEntry(name, st);
// Gathers unique values, stores in SymTab, and returns repMsg chunk
proc gatherHelper(type t) throws {
Expand Down Expand Up @@ -239,7 +239,7 @@ module UniqueMsg
}
for (name, objtype, i) in zip(names, types, 0..) {
select objtype {
when "pdarray", "category" {
when "pdarray", "Categorical" {
var g = getGenericTypedArrayEntry(name, st);
select g.dtype {
when DType.Int64 {
Expand Down
6 changes: 3 additions & 3 deletions tests/categorical_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def testBaseCategorical(self):
).all()
)
self.assertEqual(10, cat.size)
self.assertEqual("category", cat.objtype)
self.assertEqual("Categorical", cat.objtype)

with self.assertRaises(ValueError):
ak.Categorical(ak.arange(0, 5, 10))
Expand Down Expand Up @@ -229,7 +229,7 @@ def testConcatenate(self):
catTwo = self._getCategorical("string-two", 51)

resultCat = catOne.concatenate([catTwo])
self.assertEqual("category", resultCat.objtype)
self.assertEqual("Categorical", resultCat.objtype)
self.assertIsInstance(resultCat, ak.Categorical)
self.assertEqual(100, resultCat.size)

Expand All @@ -239,7 +239,7 @@ def testConcatenate(self):
self.assertFalse(resultCat.segments)

resultCat = ak.concatenate([catOne, catOne], ordered=False)
self.assertEqual("category", resultCat.objtype)
self.assertEqual("Categorical", resultCat.objtype)
self.assertIsInstance(resultCat, ak.Categorical)
self.assertEqual(100, resultCat.size)

Expand Down