-
Notifications
You must be signed in to change notification settings - Fork 4
implement where api #298
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
base: master
Are you sure you want to change the base?
implement where api #298
Changes from 2 commits
aa7301a
834b5a9
fd79955
a564305
fa62e3b
dea5b9c
bd51519
6da082d
0f83acd
213ddac
127511d
c0746fd
78c7b95
86fdeed
0d51c11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| from exetera.core.data_writer import DataWriter | ||
| from exetera.core import operations as ops | ||
| from exetera.core import validation as val | ||
| from exetera.core import utils | ||
|
|
||
|
|
||
| def isin(field:Field, test_elements:Union[list, set, np.ndarray]): | ||
|
|
@@ -39,6 +40,23 @@ def isin(field:Field, test_elements:Union[list, set, np.ndarray]): | |
| return ret | ||
|
|
||
|
|
||
| def where(cond: Union[list, tuple, np.ndarray, Field], a, b): | ||
| if isinstance(cond, (list, tuple, np.ndarray)): | ||
| cond = cond | ||
| elif isinstance(cond, Field): | ||
| if cond.indexed: | ||
| raise NotImplementedError("Where does not support condition on indexed string fields at present") | ||
| cond = cond.data[:] | ||
| elif callable(cond): | ||
| raise NotImplementedError("module method `fields.where` doesn't support callable cond, please use instance mehthod `where` for callable cond.") | ||
|
||
|
|
||
| if isinstance(a, Field): | ||
| a = a.data[:] | ||
| if isinstance(b, Field): | ||
| b = b.data[:] | ||
| return np.where(cond, a, b) | ||
|
||
|
|
||
|
|
||
| class HDF5Field(Field): | ||
| def __init__(self, session, group, dataframe, write_enabled=False): | ||
| super().__init__() | ||
|
|
@@ -143,6 +161,41 @@ def _ensure_valid(self): | |
| if not self._valid_reference: | ||
| raise ValueError("This field no longer refers to a valid underlying field object") | ||
|
|
||
| def where(self, cond:Union[list, tuple, np.ndarray, Field], b, inplace=False): | ||
|
||
| if isinstance(cond, (list, tuple, np.ndarray)): | ||
| cond = cond | ||
| elif isinstance(cond, Field): | ||
| if cond.indexed: | ||
| raise NotImplementedError("Where does not support indexed string fields at present") | ||
| cond = cond.data[:] | ||
| elif callable(cond): | ||
| cond = cond(self.data[:]) | ||
| else: | ||
| raise TypeError("'cond' parameter needs to be either callable lambda function, or array like, or NumericMemField") | ||
|
|
||
|
||
| # if isinstance(b, str): | ||
| # b = b.encode() | ||
| if isinstance(b, Field): | ||
| b = b.data[:] | ||
|
|
||
| result_ndarray = np.where(cond, self.data[:], b) | ||
| result_mem_field = None | ||
| if str(result_ndarray.dtype) in utils.PERMITTED_NUMERIC_TYPES: | ||
| result_mem_field = NumericMemField(self._session, str(result_ndarray.dtype)) | ||
| result_mem_field.data.write(result_ndarray) | ||
|
|
||
| elif isinstance(self, (IndexedStringField, FixedStringField)) or isinstance(b, (IndexedStringField, FixedStringField)): | ||
| result_mem_field = IndexedStringMemField(self._session) | ||
|
||
| result_mem_field.data.write(result_ndarray) | ||
| else: | ||
| raise NotImplementedError(f"instance method where doesn't support the current input type") | ||
|
|
||
| # if inplace: | ||
| # self.data.clear() | ||
| # self.data.write(result) | ||
|
|
||
| return result_mem_field | ||
|
|
||
|
|
||
| class MemoryField(Field): | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: mehthod -> method