def test_index_year_from_date_range_a(self) -> None:

        index = IndexYear.from_date_range('2014-12-15', '2018-03-15')
        self.assertEqual(len(index), 5)

        index = IndexYear.from_date_range('2014-12-15', '2018-03-15', step=2)
        self.assertEqual([str(d) for d in index.values],
                ['2014', '2016', '2018'])
Пример #2
0
    def test_index_datetime_init_a(self) -> None:

        dates = [
            datetime.date(*x) for x in product((2017, ), (
                4,
                5,
            ), range(1, 4))
        ]
        s1 = Series(range(len(dates)), index=IndexDate(dates))

        with self.assertRaises(ErrorInitIndex):
            index = IndexYearMonth(s1.index)

        with self.assertRaises(ErrorInitIndex):
            index = IndexYear(s1.index)  #type: ignore

        # can reuse the map if going from dt64 index to normal index
        # idx2 = Index(s1.index)
        # self.assertTrue(id(idx2._map) == id(s1.index._map)) #type: ignore

        idx3 = IndexDate(s1.index)
        self.assertTrue(id(idx3._map) == id(s1.index._map))  #type: ignore

        with self.assertRaises(ErrorInitIndex):
            index = IndexYear(idx3)  #type: ignore

        # from a date to a finer resolution has to create a new map
        idx4 = IndexMinute(idx3)
        self.assertTrue(id(idx4._map) != id(s1.index._map))  #type: ignore

        # a GO has to create a new map
        idx5 = IndexDateGO(s1.index)
        self.assertTrue(id(idx4._map) != id(s1.index._map))  #type: ignore

        # supplying a dtype to coerce the labels
        with self.assertRaises(ErrorInitIndex):
            idx6 = Index(s1.index, dtype='datetime64[Y]')

        with self.assertRaises(ErrorInitIndex):
            idx7 = Index(s1.index.values.astype('datetime64[Y]'))

        # final resolution from a normal index
        idx8 = IndexMinute(s1.index)
        self.assertTrue(id(idx8._map) != id(s1.index._map))  #type: ignore
    def test_index_datetime_from_year_month_range(self) -> None:

        date_min, date_max = (np.datetime64('2007-02'), np.datetime64('2020-04'))

        idx1 = IndexYearMonth.from_year_month_range(date_min, date_max)
        self.assertEqual(len(idx1), 159)

        idx2 = IndexYear.from_year_month_range(date_min, date_max)
        self.assertEqual(len(idx2), 14)

        idx3 = IndexDate.from_year_month_range(date_min, date_max)
        self.assertEqual(len(idx3), 4838)
Пример #4
0
    def test_index_pickle_a(self) -> None:
        a = Index([('a', 'b'), ('b', 'c'), ('c', 'd')])
        b = Index([1, 2, 3, 4])
        c = IndexYear.from_date_range('2014-12-15', '2018-03-15')

        for index in (a, b, c):
            pbytes = pickle.dumps(index)
            index_new = pickle.loads(pbytes)
            for v in index:  # iter labels
                # import ipdb; ipdb.set_trace()
                # this compares Index objects
                self.assertFalse(index_new._labels.flags.writeable)
                self.assertEqual(index_new.loc[v], index.loc[v])
    def test_index_year_from_year_range_a(self) -> None:

        index = IndexYear.from_year_range('2010', '2018')
        self.assertEqual(len(index), 9)
    def test_index_year_from_year_month_range_a(self) -> None:

        index = IndexYear.from_year_month_range('2014-12', '2018-03')
        self.assertEqual(len(index), 5)