Ejemplo n.º 1
0
    def __init__(
        self,
        data: Optional[Union(Series, DataFrame, dict[str, list])] = None,
        index: Optional[PandasIndex] = None,
    ) -> None:
        if data is None:
            self._data = DataFrame(dtype="object")
        elif type(data) is Series:
            self._data = cast(Series, data).to_frame().transpose()
        elif type(data) is DataFrame:
            self._data = DataFrame(data)
        elif type(data) is dict:
            sample_value = next(iter(data.values()))
            if not isinstance(sample_value, Iterable) or isinstance(
                    sample_value, str):
                self._data = Series(data).to_frame().transpose()
            else:
                self._data = DataFrame(data)
        else:
            raise ValueError(
                f"Received unexpected value type {type(data)}: {data}")

        if index is None:
            self._data.index.name = "index"
            self._index = PandasIndex(self._data.index, [])
        else:
            if not isinstance(index, PandasIndex):
                index = PandasIndex(index)
            self._data.index = index._data
            self._index = index
        self._loc = _LocIndexer(self)
        self._iloc = _ILocIndexer(self)
Ejemplo n.º 2
0
    def __init__(
        self: "DatabaseBackend",
        store_class: Type[T],
        database: Database,
        data_token: DataToken,
        index: Optional[DatabaseIndex] = None,
        selected_columns: Optional[list[str]] = None,
        read_only: bool = True,
    ) -> None:
        if not read_only:
            raise NotImplementedError("The current version of Tanuki does not support Store to DB writing")
        self._store_class = store_class
        self._database = database
        self._data_token = data_token
        self._read_only = read_only

        if selected_columns is None:
            self._selected_columns = self._database.table_columns(self._data_token)
        else:
            self._selected_columns = selected_columns

        if index is None:
            pindex = PIndex(np.arange(0, len(self)), name="index")
            self._index = PandasIndex(pindex, [])
        else:
            if not isinstance(index, DatabaseIndex) and not isinstance(
                index, PandasIndex
            ):
                index = PandasIndex(index)
            self._index = index
        self._loc = _LocIndexer(self)
        self._iloc = _ILocIndexer(self)
Ejemplo n.º 3
0
    def test_index(self) -> None:
        expected = PandasIndex(PIndex([0, 1, 2], name="index"), [])
        assert_that(self.data_backend.index.equals(expected), equal_to(True))

        new_frame = self.data_backend.set_index(ExampleStore.ab_index)
        pindex = PIndex([("a", 1), ("b", 2), ("c", 3)])
        pindex.name = "ab_index"
        expected = PandasIndex(pindex, ["a", "b"])
        assert_that(new_frame.index.equals(expected), equal_to(True))
Ejemplo n.º 4
0
    def test_to_pandas(self) -> None:
        assert_that(self.index.to_pandas().equals(self.index), equal_to(True))

        expected = PandasIndex(PIndex(["a", "b", "c"], name="a_index"), ["a"])
        assert_that(self.a_index.to_pandas().equals(expected), equal_to(True))

        multi_index = PIndex([("a", 1), ("b", 2), ("c", 3)])
        multi_index.name = "ab_index"
        expected = PandasIndex(multi_index, ["a", "b"])
        assert_that(self.ab_index.to_pandas().equals(expected), equal_to(True))
Ejemplo n.º 5
0
    def test_equals(self):
        test = PandasIndex(PIndex([0, 1, 2], name="index"), ["a", "b"])
        assert_that(self.index.equals(test), equal_to(True))

        test = PandasIndex(PIndex([0, 1], name="index"), ["a", "b"])
        assert_that(self.index.equals(test), equal_to(False))

        test = PandasIndex(PIndex([0, 1, 2], name="index2"), ["a", "b"])
        assert_that(self.index.equals(test), equal_to(False))

        test = PandasIndex(PIndex([0, 1, 2], name="index"), ["a"])
        assert_that(self.index.equals(test), equal_to(False))
Ejemplo n.º 6
0
    def test_getitem(self):
        expected = PandasIndex(PIndex([1], name="index"), [])
        assert_that(self.index[1], equal_to(1))
        assert_that(self.index[[1]].equals(expected), equal_to(True))

        expected = PandasIndex(PIndex(["b"], name="a_index"), ["a"])
        assert_that(self.a_index[1], equal_to("b"))
        assert_that(self.a_index[[1]].equals(expected), equal_to(True))

        multi_index = PIndex([("b", 2)])
        multi_index.name = "ab_index"
        expected = PandasIndex(multi_index, ["a", "b"])
        assert_that(self.ab_index[1], equal_to(("b", 2)))
        assert_that(self.ab_index[[1]].equals(expected), equal_to(True))
Ejemplo n.º 7
0
 def test_drop_indices(self) -> None:
     new_frame = self.data_backend.drop_indices([1])
     expected = PandasBackend(
         {"a": ["a", "c"], "b": [1, 3], "c": [True, True]},
         index=PandasIndex(PIndex([0, 2], name="index"), []),
     )
     assert_that(new_frame.equals(expected), equal_to(True))
Ejemplo n.º 8
0
 def test_query(self) -> None:
     query = (ExampleStore.a == "a") | (ExampleStore.b == 3)
     test = self.data_backend.query(query)
     expected = PandasBackend(
         {"a": ["a", "c"], "b": [1, 3], "c": [True, True]},
         index=PandasIndex(PIndex([0, 2], name="index"), []),
     )
     assert_that(test.equals(expected), equal_to(True))
Ejemplo n.º 9
0
 def test_getmask(self) -> None:
     test = self.data_backend.getmask([True, False, True])
     expected = PandasBackend(
         {"a": ["a", "c"], "b": [1, 3], "c": [True, True]},
         index=PandasIndex(PIndex([0, 2], name="index"), []),
     )
     repr(expected)
     assert_that(test.equals(expected), equal_to(True))
Ejemplo n.º 10
0
    def test_equals(self):
        test = PandasIndex(PIndex([0, 1, 2], name="index"), [])
        assert_that(self.index.equals(test), equal_to(True))

        test = DatabaseIndex("a_index", self.backend["a"])
        assert_that(self.a_index.equals(test), equal_to(True))

        test = DatabaseIndex("ab_index", self.backend.getitems(["a", "b"]))
        assert_that(self.ab_index.equals(test), equal_to(True))
Ejemplo n.º 11
0
 def reset_index(self) -> PandasBackend:
     pindex = PIndex(np.arange(0, len(self)), name="index")
     return DatabaseBackend(
         self._store_class,
         self._database,
         self._data_token,
         index=PandasIndex(pindex, []),
         selected_columns=self._selected_columns,
         read_only=self._read_only,
     )
Ejemplo n.º 12
0
 def reset_index(self: PandasBackend) -> PandasBackend:
     new_data = self._data.reset_index(drop=True)
     new_data.index.name = "index"
     new_index = PandasIndex(new_data.index, [])
     return PandasBackend(new_data, new_index)
Ejemplo n.º 13
0
 def set_index(self, index: Union[Index, IndexAlias]) -> PandasBackend:
     cols = [str(col) for col in index.columns]
     new_data = self._data.set_index(cols)
     new_data.index.name = index.name
     new_index = PandasIndex(new_data.index, cols)
     return PandasBackend(new_data, new_index)
Ejemplo n.º 14
0
 def get_index(self, index_alias: IndexAlias) -> Index:
     cols = [str(col) for col in index_alias.columns]
     new_data = self._data.set_index(cols)
     new_data.index.name = index_alias.name
     return PandasIndex(new_data.index, cols)
Ejemplo n.º 15
0
 def test_getitem(self):
     expected = PandasIndex(PIndex([1], name="index"), ["a", "b"])
     assert_that(self.index[1], equal_to(1))
     assert_that(self.index[[1]].equals(expected), equal_to(True))
Ejemplo n.º 16
0
 def setup_method(self):
     self.index = PandasIndex(PIndex(np.arange(0, 3), name="index"), ["a", "b"])