def test_index_date_init_b(self) -> None: i1 = IndexDate(('2017-12-30', '2017-12-31', '2018-01-05'), name='foo') i2 = IndexDate(i1) self.assertEqual(i1.name, i2.name) self.assertEqual(i2.values.tolist(), [ datetime.date(2017, 12, 30), datetime.date(2017, 12, 31), datetime.date(2018, 1, 5) ])
def test_index_date_b(self): with self.assertRaises(Exception): IndexDate([3, 4, 5], dtype=int) idx1 = IndexDate(['2017', '2018']) self.assertTrue(idx1[0].values[0].__class__ == np.datetime64) idx2 = IndexDate(['2017-01', '2018-07']) self.assertTrue(idx2[0].values[0].__class__ == np.datetime64)
def test_index_date_b(self): with self.assertRaises(Exception): index = IndexDate([3, 4, 5]) with self.assertRaises(Exception): index = IndexDate(['2017', '2018']) with self.assertRaises(Exception): index = IndexDate(['2017-01', '2018-07'])
def test_index_many_set_b(self) -> None: idx0 = Index(('1997-01-01', '1997-01-02'), name='foo') idx1 = IndexDate(('2020-01-01', '2020-01-02'), name='foo') idx2 = IndexDate(('2020-02-01', '2020-02-02')) post1 = index_many_set((idx0, idx1), IndexGO, union=True) self.assertEqual(post1.__class__, IndexGO) post2 = index_many_set((idx1, idx2), IndexGO, union=False) self.assertEqual(post2.__class__, IndexDateGO)
def test_index_date_b(self) -> None: with self.assertRaises(Exception): IndexDate([3, 4, 5], dtype=np.int64) #type: ignore #pylint: disable=E1123 idx1 = IndexDate(['2017', '2018']) self.assertTrue(idx1[0].__class__ == np.datetime64) self.assertEqual(idx1.loc_to_iloc('2018-01-01'), 1) idx2 = IndexDate(['2017-01', '2018-07']) self.assertTrue(idx2[0].__class__ == np.datetime64) self.assertEqual(idx2.loc['2017-01-01'], np.datetime64('2017-01-01'))
def test_index_many_concat_a2(self) -> None: # idx0 = Index(('1997-01-01', '1997-01-02'), name='foo') idx1 = IndexDate(('2020-01-01', '2020-01-02'), name='foo') idx2 = IndexDate(('2020-02-01', '2020-02-02')) post2 = index_many_concat((idx1, idx2), Index) assert isinstance(post2, Index) self.assertEqual(post2.__class__, IndexDate) self.assertEqual(post2.values.tolist(), [ datetime.date(2020, 1, 1), datetime.date(2020, 1, 2), datetime.date(2020, 2, 1), datetime.date(2020, 2, 2) ])
def test_index_to_pandas_b(self) -> None: import pandas idx1 = IndexDate(('2018-01-01', '2018-06-01'), name='foo') pdidx = idx1.to_pandas() self.assertEqual(pdidx.name, idx1.name) self.assertTrue((pdidx.values == idx1.values).all()) self.assertTrue(pdidx[1].__class__ == pandas.Timestamp)
def test_index_via_dt_a(self) -> None: idx1 = IndexDate(('2020-01-01', '2021-02-05', '2019-03-17')) self.assertEqual(idx1.via_dt.day.tolist(), [1, 5, 17]) self.assertEqual(idx1.via_dt.weekday().tolist(), [2, 4, 6])
def from_pandas(cls, value, *, is_go: bool = False) -> 'IndexBase': ''' Given a Pandas index, return the appropriate IndexBase derived class. ''' import pandas from static_frame import Index from static_frame import IndexGO from static_frame import IndexDate from static_frame import IndexHierarchy from static_frame import IndexHierarchyGO if isinstance(value, pandas.MultiIndex): # iterating over a hierarchucal index will iterate over labels if is_go: return IndexHierarchyGO.from_labels(value) return IndexHierarchy.from_labels(value) elif isinstance(value, pandas.DatetimeIndex): if is_go: raise NotImplementedError( 'No grow-only version of IndexDate yet exists') return IndexDate(value) if is_go: return IndexGO(value) return Index(value)
def test_index_datetime_astype_a(self) -> None: idx1 = IndexDate(('2020-01-01', '2022-05-10')) self.assertEqual( idx1.astype('datetime64[ns]').__class__, IndexNanosecond) self.assertEqual(idx1.astype('datetime64[Y]').__class__, IndexYear) self.assertEqual(idx1.astype(str).__class__, Index)
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_many_set_c(self) -> None: idx1 = IndexDate(('2020-02-01', '2020-02-02')) post1 = index_many_set((idx1, ), Index, union=True) self.assertEqual(post1.__class__, IndexDate) self.assertTrue(idx1.equals(post1)) # empty iterable returns an empty index post2 = index_many_set((), Index, union=True) self.assertEqual(len(post2), 0) #type: ignore
def test_index_datetime_loc(self) -> None: idx1 = IndexDate(()) idx2 = idx1.iloc[[]] self.assertEqual(idx2.__class__, IndexDate) self.assertEqual(len(idx2), 0) idx3 = idx1.loc[[]] self.assertEqual(idx3.__class__, IndexDate) self.assertEqual(len(idx3), 0)
def test_index_many_set_a(self) -> None: idx0 = Index(('1997-01-01', '1997-01-02'), name='foo') idx1 = IndexDate(('2020-01-01', '2020-01-02'), name='foo') idx2 = IndexDate(('2020-01-02', '2020-01-03')) post1 = index_many_set((idx0, idx1), Index, union=True) assert isinstance(post1, Index) self.assertEqual(post1.name, 'foo') self.assertEqual(post1.__class__, Index) # self.assertEqual(set(post1.values), # {'1997-01-02', # '1997-01-01', # np.datetime64('2020-01-01'), # np.datetime64('2020-01-02')}) # the result of this operation is an unstable ordering values = set(post1.values) self.assertTrue('1997-01-01' in values) self.assertTrue('1997-01-02' in values) self.assertTrue(datetime.date(2020, 1, 1) in values) self.assertTrue(datetime.date(2020, 1, 2) in values) post2 = index_many_set((idx1, idx2), Index, union=True) assert isinstance(post2, Index) self.assertEqual(post2.name, None) self.assertEqual(post2.__class__, IndexDate) self.assertEqual(post2.values.tolist(), [ datetime.date(2020, 1, 1), datetime.date(2020, 1, 2), datetime.date(2020, 1, 3) ]) post3 = index_many_set((idx1, idx2), Index, union=False) assert isinstance(post3, Index) self.assertEqual(post3.name, None) self.assertEqual(post3.__class__, IndexDate) self.assertEqual(post3.values.tolist(), [datetime.date(2020, 1, 2)])
def test_index_many_concat_a1(self) -> None: idx0 = Index(('1997-01-01', '1997-01-02'), name='foo') idx1 = IndexDate(('2020-01-01', '2020-01-02'), name='foo') idx2 = IndexDate(('2020-02-01', '2020-02-02')) post1 = index_many_concat((idx0, idx1), Index) assert isinstance(post1, Index) post1b = index_many_concat((idx0, idx1), Index, IndexDate) assert isinstance(post1b, IndexDate) post1c = index_many_concat((idx0, idx1, idx2), Index, IndexDate) assert isinstance(post1c, IndexDate) self.assertEqual(post1.values.tolist(), [ '1997-01-01', '1997-01-02', datetime.date(2020, 1, 1), datetime.date(2020, 1, 2) ]) self.assertEqual(post1.name, 'foo') self.assertEqual(post1.__class__, Index)
def test_series_to_pandas_b(self): from pandas import Timestamp s1 = Series(range(4), index=IndexDate(('2018-01-02', '2018-01-03', '2018-01-04', '2018-01-05'))) df = s1.to_pandas() self.assertEqual(df.index.tolist(), [ Timestamp('2018-01-02 00:00:00'), Timestamp('2018-01-03 00:00:00'), Timestamp('2018-01-04 00:00:00'), Timestamp('2018-01-05 00:00:00') ]) self.assertEqual(df.values.tolist(), [0, 1, 2, 3])
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_date_q(self) -> None: index = IndexDate(('2017-12-30', '2017-12-31', '2018-01-05')) s1 = Series(range(len(index)), index=index) # a range beyond the observed values cannot determine a match, self.assertEqual(s1[:'2019'].shape, (0,)) # type: ignore self.assertEqual(s1['2016':].shape, (0,)) # type: ignore