def test_index_millisecond_series_a(self) -> None: msg = '''2016-04-28 04:22:12.226 2016-04-28 16:29:21.32 2016-04-28 17:36:13.733 2016-04-30 20:21:07.848 2016-05-01 00:00:33.483 2016-05-01 03:02:03.584 2016-05-01 09:26:43.185 2016-05-01 13:45:22.576 2016-05-01 15:25:46.15''' idx = IndexMillisecond(msg.split('\n')) s = Series(range(9), index=idx) self.assertEqual(s['2016-05-01T00:00:33.483'], 4) self.assertEqual(s['2016-05-01T00:00:33.483':].values.tolist(), # type: ignore # https://github.com/python/typeshed/pull/3024 [4, 5, 6, 7, 8]) self.assertEqual(s['2016-05'].to_pairs(), ((np.datetime64('2016-05-01T00:00:33.483'), 4), (np.datetime64('2016-05-01T03:02:03.584'), 5), (np.datetime64('2016-05-01T09:26:43.185'), 6), (np.datetime64('2016-05-01T13:45:22.576'), 7), (np.datetime64('2016-05-01T15:25:46.150'), 8))) self.assertEqual(s['2016-05-01T09'].to_pairs(), ((np.datetime64('2016-05-01T09:26:43.185'), 6),))
def test_index_millisecond_a(self) -> None: msg = '''2016-04-28 04:22:12.226 2016-04-28 16:29:21.32 2016-04-28 17:36:13.733 2016-04-30 20:21:07.848 2016-05-01 00:00:33.483 2016-05-01 03:02:03.584 2016-05-01 09:26:43.185 2016-05-01 13:45:22.576 2016-05-01 15:25:46.15''' idx = IndexMillisecond(msg.split('\n')) self.assertEqual(str(idx.dtype), 'datetime64[ms]') self.assertEqual(idx.loc['2016-04-30T20:21:07.848'], np.datetime64('2016-04-30T20:21:07.848')) self.assertAlmostEqualValues( idx.loc['2016-05-01T09:26:43.185':].values, # type: ignore # https://github.com/python/typeshed/pull/3024 np.array(['2016-05-01T09:26:43.185', '2016-05-01T13:45:22.576', '2016-05-01T15:25:46.150'], dtype='datetime64[ms]')) self.assertAlmostEqualValues(idx.loc['2016-05'].values, #type: ignore np.array(['2016-05-01T00:00:33.483', '2016-05-01T03:02:03.584', '2016-05-01T09:26:43.185', '2016-05-01T13:45:22.576', '2016-05-01T15:25:46.150'], dtype='datetime64[ms]') ) self.assertEqual(idx.loc['2016-05-01T00'].values, #type: ignore np.array(['2016-05-01T00:00:33.483'], dtype='datetime64[ms]'))
def test_index_millisecond_b(self): # integer arguments are interpreted as milliseconds from the epoch idx = IndexMillisecond(range(10)) self.assertAlmostEqualValues( idx.loc['1970-01-01T00:00:00.007':].values, np.array([ '1970-01-01T00:00:00.007', '1970-01-01T00:00:00.008', '1970-01-01T00:00:00.009' ], dtype='datetime64[ms]'))
def test_index_millisecond_b(self) -> None: # integer arguments are interpreted as milliseconds from the epoch idx = IndexMillisecond(range(10)) self.assertAlmostEqualValues( idx.loc['1970-01-01T00:00:00.007':]. values, # type: ignore # https://github.com/python/typeshed/pull/3024 np.array([ '1970-01-01T00:00:00.007', '1970-01-01T00:00:00.008', '1970-01-01T00:00:00.009' ], dtype='datetime64[ms]'))
def test_index_millisecond_frame_a(self) -> None: msg = '''2016-04-28 04:22:12.226 2016-04-28 16:29:21.32 2016-04-28 17:36:13.733 2016-04-30 20:21:07.848 2016-05-01 00:00:33.483 2016-05-01 03:02:03.584 2016-05-01 09:26:43.185 2016-05-01 13:45:22.576 2016-05-01 15:25:46.15''' f = Frame.from_records((x, y) for x, y in enumerate(msg.split('\n'))) idx1 = IndexMillisecond(f[1]) self.assertAlmostEqualValues( idx1.values, np.array([ '2016-04-28T04:22:12.226', '2016-04-28T16:29:21.320', '2016-04-28T17:36:13.733', '2016-04-30T20:21:07.848', '2016-05-01T00:00:33.483', '2016-05-01T03:02:03.584', '2016-05-01T09:26:43.185', '2016-05-01T13:45:22.576', '2016-05-01T15:25:46.150' ], dtype='datetime64[ms]')) idx2 = IndexSecond(f[1]) self.assertAlmostEqualValues( idx2.values, np.array([ '2016-04-28T04:22:12', '2016-04-28T16:29:21', '2016-04-28T17:36:13', '2016-04-30T20:21:07', '2016-05-01T00:00:33', '2016-05-01T03:02:03', '2016-05-01T09:26:43', '2016-05-01T13:45:22', '2016-05-01T15:25:46' ], dtype='datetime64[s]')) f2 = f.set_index(1, index_constructor=IndexMillisecond) self.assertEqual(f2.loc['2016-05', 0].values.tolist(), [4, 5, 6, 7, 8])