| 527 | |
| 528 | |
| 529 | class SingleData(IndexData): |
| 530 | def __init__( |
| 531 | self, data: Union[int, float, np.number, list, dict, pd.Series] = [], index: Union[List, pd.Index, Index] = [] |
| 532 | ): |
| 533 | """A data structure of index and numpy data. |
| 534 | It's used to replace pd.Series due to high-speed. |
| 535 | |
| 536 | Parameters |
| 537 | ---------- |
| 538 | data : Union[int, float, np.number, list, dict, pd.Series] |
| 539 | the input data |
| 540 | index : Union[list, pd.Index] |
| 541 | the index of data. |
| 542 | empty list indicates that auto filling the index to the length of data |
| 543 | """ |
| 544 | # for special data type |
| 545 | if isinstance(data, dict): |
| 546 | assert len(index) == 0 |
| 547 | if len(data) > 0: |
| 548 | index, data = zip(*data.items()) |
| 549 | else: |
| 550 | index, data = [], [] |
| 551 | elif isinstance(data, pd.Series): |
| 552 | assert len(index) == 0 |
| 553 | index, data = data.index, data.values |
| 554 | elif isinstance(data, (int, float, np.number)): |
| 555 | data = [data] |
| 556 | super().__init__(data, index) |
| 557 | assert self.ndim == 1 |
| 558 | |
| 559 | def _align_indices(self, other): |
| 560 | if self.index == other.index: |
| 561 | return other |
| 562 | elif set(self.index) == set(other.index): |
| 563 | return other.reindex(self.index) |
| 564 | else: |
| 565 | raise ValueError( |
| 566 | f"The indexes of self and other do not meet the requirements of the four arithmetic operations" |
| 567 | ) |
| 568 | |
| 569 | def reindex(self, index: Index, fill_value=np.nan) -> SingleData: |
| 570 | """reindex data and fill the missing value with np.nan. |
| 571 | |
| 572 | Parameters |
| 573 | ---------- |
| 574 | new_index : list |
| 575 | new index |
| 576 | fill_value: |
| 577 | what value to fill if index is missing |
| 578 | |
| 579 | Returns |
| 580 | ------- |
| 581 | SingleData |
| 582 | reindex data |
| 583 | """ |
| 584 | # TODO: This method can be more general |
| 585 | if self.index == index: |
| 586 | return self |
no outgoing calls
no test coverage detected