Skip to content

Commit

Permalink
Closes Bears-R-Us#3213 Index properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ajpotts committed May 23, 2024
1 parent 38f3041 commit 49847a8
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
26 changes: 26 additions & 0 deletions PROTO_tests/tests/index_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pandas as pd
import pytest
from numpy import dtype as npdtype

import arkouda as ak
from arkouda.dtypes import dtype
Expand Down Expand Up @@ -52,6 +53,15 @@ def test_multiindex_creation(self, size):
with pytest.raises(ValueError):
idx = ak.MultiIndex([ak.arange(size), ak.arange(size - 1)])

def test_name_names(self):
i = ak.Index([1, 2, 3], name="test")
assert i.name == "test"
assert i.names == ["test"]

size = 10
m = ak.MultiIndex([ak.arange(size), ak.arange(size) * -1], names=["test", "test2"])
assert m.names == ["test", "test2"]

def test_nlevels(self):
i = ak.Index([1, 2, 3], name="test")
assert i.nlevels == 1
Expand All @@ -60,6 +70,22 @@ def test_nlevels(self):
m = ak.MultiIndex([ak.arange(size), ak.arange(size) * -1])
assert m.nlevels == 2

def test_ndim(self):
i = ak.Index([1, 2, 3], name="test")
assert i.ndim == 1

size = 10
m = ak.MultiIndex([ak.arange(size), ak.arange(size) * -1])
assert m.ndim == 1

def test_dtypes(self):
size = 10
i = ak.Index(ak.arange(size, dtype="float64"))
assert i.dtype == dtype("float64")

m = ak.MultiIndex([ak.arange(size), ak.arange(size) * -1])
assert m.dtype == npdtype("O")

def test_inferred_type(self):
i = ak.Index([1, 2, 3])
assert i.inferred_type == "integer"
Expand Down
48 changes: 46 additions & 2 deletions arkouda/index.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

import json
from typing import TYPE_CHECKING, List, Optional, Union, Tuple
from typing import TYPE_CHECKING, List, Optional, Tuple, Union

import pandas as pd # type: ignore
from numpy import array as ndarray
from numpy import dtype as npdtype
from typeguard import typechecked

from arkouda import Categorical, Strings
Expand Down Expand Up @@ -194,6 +195,17 @@ def nlevels(self):
"""
return 1

@property
def ndim(self):
"""
Number of dimensions of the underlying data, by definition 1.
See Also
--------
MultiIndex.ndim
"""
return 1

@property
def inferred_type(self) -> str:
"""
Expand All @@ -211,6 +223,13 @@ def inferred_type(self) -> str:
return "string"
return self.values.inferred_type

@property
def names(self):
"""
Return Index or MultiIndex names.
"""
return [self.name]

@property
def index(self):
"""
Expand Down Expand Up @@ -986,7 +1005,7 @@ def __init__(
raise TypeError("MultiIndex should be an iterable")
self.levels = levels
first = True
self.names = names
self._names = names
self.name = name
for col in self.levels:
# col can be a python int which doesn't have a size attribute
Expand Down Expand Up @@ -1025,6 +1044,13 @@ def __eq__(self, v):

return retval

@property
def names(self):
"""
Return Index or MultiIndex names.
"""
return self._names

@property
def index(self):
return self.levels
Expand All @@ -1039,6 +1065,17 @@ def nlevels(self) -> int:
"""
return len(self.levels)

@property
def ndim(self):
"""
Number of dimensions of the underlying data, by definition 1.
See Also
--------
Index.ndim
"""
return 1

@property
def inferred_type(self) -> str:
return "mixed"
Expand All @@ -1065,6 +1102,13 @@ def get_level_values(self, level: Union[str, int]):
"an integer with absolute value less than the number of levels."
)

@property
def dtype(self) -> npdtype:
"""
Return the dtype object of the underlying data.
"""
return npdtype("O")

def memory_usage(self, unit="B"):
"""
Return the memory usage of the MultiIndex levels.
Expand Down
27 changes: 27 additions & 0 deletions tests/index_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import os
import tempfile


import pandas as pd
from base_test import ArkoudaTest
from context import arkouda as ak
from numpy import dtype as npdtype

from arkouda import io_util
from arkouda.dtypes import dtype
Expand Down Expand Up @@ -61,6 +63,15 @@ def test_multiindex_creation(self):
with self.assertRaises(ValueError):
idx = ak.MultiIndex([ak.arange(5), ak.arange(3)])

def test_name_names(self):
i = ak.Index([1, 2, 3], name="test")
self.assertEqual(i.name, "test")
self.assertListEqual(i.names, ["test"])

size = 10
m = ak.MultiIndex([ak.arange(size), ak.arange(size) * -1], names=["test", "test2"])
self.assertListEqual(m.names, ["test", "test2"])

def test_nlevels(self):
i = ak.Index([1, 2, 3], name="test")
assert i.nlevels == 1
Expand All @@ -69,6 +80,22 @@ def test_nlevels(self):
m = ak.MultiIndex([ak.arange(size), ak.arange(size) * -1])
assert m.nlevels == 2

def test_ndim(self):
i = ak.Index([1, 2, 3], name="test")
assert i.ndim == 1

size = 10
m = ak.MultiIndex([ak.arange(size), ak.arange(size) * -1])
assert m.ndim == 1

def test_dtypes(self):
size = 10
i = ak.Index(ak.arange(size, dtype="float64"))
assert i.dtype == dtype("float64")

m = ak.MultiIndex([ak.arange(size), ak.arange(size) * -1])
assert m.dtype == npdtype("O")

def test_inferred_type(self):
i = ak.Index([1, 2, 3])
self.assertEqual(i.inferred_type, "integer")
Expand Down

0 comments on commit 49847a8

Please sign in to comment.