def test_index_year_month_from_year_range_a(self) -> None: index = IndexYearMonth.from_year_range('2010', '2018') self.assertEqual(len(index), 108) self.assertEqual(str(index.min()), '2010-01') self.assertEqual(str(index.max()), '2018-12') index = IndexYearMonth.from_year_range('2010', '2018', 6) self.assertEqual( [str(d) for d in IndexYearMonth.from_year_range('2010', '2018', 6)], ['2010-01', '2010-07', '2011-01', '2011-07', '2012-01', '2012-07', '2013-01', '2013-07', '2014-01', '2014-07', '2015-01', '2015-07', '2016-01', '2016-07', '2017-01', '2017-07', '2018-01', '2018-07'])
def test_index_datetime_from_date_range(self) -> None: date_min, date_max = (np.datetime64('2007-02'), np.datetime64('2020-04')) # we reject year-mos when calling from_date_range with self.assertRaises(RuntimeError): _ = IndexYearMonth.from_date_range(date_min, date_max)
def test_index_datetime_nanosecond_a(self) -> None: index1 = IndexNanosecond(('2020-01-01', '2020-02-01')) index2 = IndexYearMonth(index1) self.assertEqual( index2.values.tolist(), [datetime.date(2020, 1, 1), datetime.date(2020, 2, 1)] )
def test_index_year_month_from_year_month_range_a(self) -> None: index = IndexYearMonth.from_year_month_range( '2017-12-15', '2018-03-15') self.assertAlmostEqualValues(index.values, np.array(['2017-12', '2018-01', '2018-02', '2018-03'], dtype='datetime64[M]')) index = IndexYearMonth.from_year_month_range('2017-12', '2018-03') self.assertEqual(len(index), 4) self.assertEqual([str(d) for d in index.values], ['2017-12', '2018-01', '2018-02', '2018-03']) index = IndexYearMonth.from_year_month_range('2017-12', '2018-03', step=2) self.assertEqual([str(d) for d in index], ['2017-12', '2018-02'])
def test_index_year_month_a(self) -> None: idx1 = IndexYearMonth(('2018-01', '2018-06')) self.assertEqual( idx1.values.tolist(), [datetime.date(2018, 1, 1), datetime.date(2018, 6, 1)])
def test_index_year_month_from_year_month_range_a(self): with self.assertRaises(Exception): index = IndexYearMonth.from_year_month_range( '2017-12-15', '2018-03-15') index = IndexYearMonth.from_year_month_range('2017-12', '2018-03') self.assertEqual(len(index), 4) self.assertEqual([str(d) for d in index.values], ['2017-12', '2018-01', '2018-02', '2018-03']) index = IndexYearMonth.from_year_month_range('2017-12', '2018-03', step=2) self.assertEqual([str(d) for d in index], ['2017-12', '2018-02'])
def test_index_datetime_microsecond_a(self) -> None: index1 = IndexMicrosecond(('2020-01-01', '2020-02-01')) self.assertEqual(index1.dtype, np.dtype('<M8[us]')) index2 = IndexYearMonth(index1) self.assertEqual( index2.values.tolist(), [datetime.date(2020, 1, 1), datetime.date(2020, 2, 1)] )
def test_index_datetime_hour_b(self) -> None: index1 = IndexHourGO(('2020-01-01', '2020-02-01')) index1.append('2020-03-01') index2 = IndexYearMonth(index1) self.assertEqual( index2.values.tolist(), [datetime.date(2020, 1, 1), datetime.date(2020, 2, 1), datetime.date(2020, 3, 1)] )
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)
def test_index_year_month_b(self) -> None: idx1 = IndexYearMonth(('2017-12', '2018-01', '2018-02', '2018-03', '2018-04')) post1 = idx1.loc[np.datetime64('2018-02'):] self.assertEqual( post1.values.tolist(), #type: ignore [datetime.date(2018, 2, 1), datetime.date(2018, 3, 1), datetime.date(2018, 4, 1)] ) # a year datetime64 post2 = idx1.loc[np.datetime64('2018'):] self.assertEqual( post2.values.tolist(), #type: ignore [datetime.date(2018, 1, 1), datetime.date(2018, 2, 1), datetime.date(2018, 3, 1), datetime.date(2018, 4, 1)] )
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_intersection_a(self) -> None: # NOTE: pending resolution of behavior: should probably raise. index_ym = IndexYearMonth.from_year_month_range('2021-01', '2021-05') index_date = IndexDate(index_ym) with self.assertRaises(RuntimeError): post = index_ym.intersection(index_date)
def test_index_year_month_from_date_range_a(self) -> None: index = IndexYearMonth.from_date_range('2017-12-15', '2018-03-15') self.assertEqual(len(index), 4) index = IndexYearMonth.from_date_range('2017-12-15', '2018-03-15', 2) self.assertEqual(len(index), 2)