Skip to content

Commit

Permalink
feat(frontend): enable handling of non-numerical index values within …
Browse files Browse the repository at this point in the history
…Index class for frontends. in case of string labels, the index_array is like tokenized sequence
  • Loading branch information
Ishticode committed Aug 23, 2023
1 parent 2f4c2c8 commit 4a6abad
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions ivy/functional/frontends/pandas/index.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
import ivy
import ivy.functional.frontends.pandas.series as series

from typing import Iterable

class Index:
def __init__(self, data, dtype=None, copy=False, name=None, tupleize_cols=True):
self.index = data
self.tokens = None
if not isinstance(data, ivy.Array):
self.index_array = ivy.array(data, dtype=dtype)
try:
self.index_array = ivy.array(data, dtype=dtype)
except ivy.utils.exceptions.IvyBackendException:
# labels as strings
if isinstance(data, (list, tuple)):
self.tokens = data
self.index_array = Index._tokenize_1d(data)
else:
# todo: handle other cases
raise NotImplementedError

else:
self.index_array = data

self.tokens_exist = self.tokens is not None
self.dtype = dtype
self.name = name
self.copy = copy
self.tupleize_cols = tupleize_cols

@staticmethod
def _tokenize_1d(x: Iterable):
return ivy.array(list(v for v, _ in enumerate(x)))

def __repr__(self):
return f"Index {self.index_array.to_list()}"
if self.tokens_exist:
return f"Index({list(self.tokens)})"
return f"Index({self.index_array.to_list()})"

def __getitem__(self, item):
if self.tokens_exist:
if isinstance(item, (list, tuple)):
return Index(self.tokens[item])
return self.tokens[item]
elif isinstance(item, (list, tuple)):
return Index(self.index_array[item])
return self.index_array[item]

def __len__(self):
return len(self.index_array)
Expand Down

0 comments on commit 4a6abad

Please sign in to comment.