Esempio n. 1
0
    def test_new_write_with_missing(self):
        writer.write(self.tsdb_file, pd.Series(index = [datetime(2014,1,1), datetime(2014,1,2), datetime(2014,1,3)], data = [1.0, np.nan, 3.0]), 'D')

        data = reader.read(self.tsdb_file)
        self.assertEqual(1.0, data.values[0])
        self.assertTrue(np.isnan(data.values[1]))
        self.assertEqual(3.0, data.values[2])
Esempio n. 2
0
    def test_append_with_large_gap(self):
        writer.write(self.tsdb_file,
                     pd.Series(index=[datetime(2005, 1, 1)], data=[1.0]), 'H')
        start_time = time.time()
        writer.write(self.tsdb_file,
                     pd.Series(index=[datetime(2014, 12, 31)], data=[2.0]),
                     'H')
        end_time = time.time()

        # Prior to the fix committed with this test, on my Macbook Air,
        # the run time here was around 1.3 seconds. After the fix
        # it is down to almost half a second (around 0.56 seconds for
        # the total test run time).
        # The performance improvement is larger with bigger gaps.
        self.assertLessEqual((end_time - start_time), 1)

        # A few spot checks to make sure the test actually ran as expected.
        data = reader.read(self.tsdb_file)
        self.assertEqual(1.0, data.values[0])
        self.assertTrue(np.isnan(data.values[1]))
        self.assertEqual(2.0, data.values[-1])
        self.assertEqual(datetime(2005, 1, 1, 0, 0),
                         data.index[0].to_pydatetime())
        self.assertEqual(datetime(2014, 12, 31),
                         data.index[-1].to_pydatetime())
Esempio n. 3
0
    def test_write_overlapping_hourly(self):
        input_a = pd.Series(index=[
            datetime(2014, 1, 1, 0, 0, 0),
            datetime(2014, 1, 1, 1, 0, 0),
            datetime(2014, 1, 1, 2, 0, 0)
        ],
                            data=[1.0, 2.0, 3.0])
        input_b = pd.Series(index=[
            datetime(2014, 1, 1, 2, 0, 0),
            datetime(2014, 1, 1, 3, 0, 0),
            datetime(2014, 1, 1, 4, 0, 0),
            datetime(2014, 1, 1, 5, 0, 0)
        ],
                            data=[4.0, 5.0, 6.0, 7.0])

        writer.write(self.tsdb_file, input_a, 'H')
        log_entries = writer.write(self.tsdb_file, input_b, 'H')
        modified = log_entries['U']
        self.assertEqual(1, len(modified))
        self.assertEqual([(1388541600, 3.0, 0)], modified)

        data = reader.read(self.tsdb_file)
        self.assertEqual(1.0, data.values[0])
        self.assertEqual(2.0, data.values[1])
        self.assertEqual(4.0, data.values[2])
        self.assertEqual(5.0, data.values[3])
        self.assertEqual(6.0, data.values[4])
        self.assertEqual(7.0, data.values[5])
Esempio n. 4
0
    def test_irregular_update_of_nan(self):
        writer.write(
            self.tsdb_existing_file,
            pd.Series(index=[
                datetime(2014, 1, 2),
                datetime(2014, 1, 3),
                datetime(2014, 1, 5),
                datetime(2014, 1, 7),
                datetime(2014, 1, 8)
            ],
                      data=[2, np.nan, 5.0, 7.0, 8.0]), 'IRR')

        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(index=[
                datetime(2014, 1, 2),
                datetime(2014, 1, 3),
                datetime(2014, 1, 5),
                datetime(2014, 1, 7),
                datetime(2014, 1, 8)
            ],
                      data=[2, 3.5, 5.0, 7.0, 8.0]), 'IRR')
        modified = log_entries['U']

        self.assertEqual(1, len(modified))
        self.assertEqual(1388707200, modified[0][0])
        self.assertTrue(np.isnan(modified[0][1]))
        self.assertEqual(9999, modified[0][2])

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(1.0, data.values[0])
        self.assertEqual(2.0, data.values[1])
        self.assertEqual(3.5, data.values[2])
        self.assertEqual(datetime(2014, 1, 3), data.index[2].to_pydatetime())
Esempio n. 5
0
    def test_log_entries_for_update_nan_multiple_times(self):
        log_entries = writer.write(self.tsdb_existing_file, pd.Series(index = [datetime(2014,1,2),datetime(2014,1,3)], data = [np.nan, 3.5]), 'D')
        new_entries = log_entries['C']
        self.assertEqual(2, len(new_entries))

        log_entries = writer.write(self.tsdb_existing_file, pd.Series(index = [datetime(2014,1,2),datetime(2014,1,3)], data = [np.nan, 3.5]), 'D')
        new_entries = log_entries['C']
        self.assertEqual(1, len(new_entries))
Esempio n. 6
0
    def test_new_write(self):
        writer.write(
            self.tsdb_file,
            pd.Series(index=[
                datetime(2014, 1, 1),
                datetime(2014, 1, 2),
                datetime(2014, 1, 3)
            ],
                      data=[1.0, 2.0, 3.0]), 'D')
        with open(self.tsdb_file, 'rb') as file:
            datafile = file.read()

        self.assertEqual('06606801154cbfdc8e1b8c7b1e3c1956',
                         hashlib.md5(datafile).hexdigest())
Esempio n. 7
0
    def test_new_write_with_missing(self):
        writer.write(
            self.tsdb_file,
            pd.Series(index=[
                datetime(2014, 1, 1),
                datetime(2014, 1, 2),
                datetime(2014, 1, 3)
            ],
                      data=[1.0, np.nan, 3.0]), 'D')

        data = reader.read(self.tsdb_file)
        self.assertEqual(1.0, data.values[0])
        self.assertTrue(np.isnan(data.values[1]))
        self.assertEqual(3.0, data.values[2])
Esempio n. 8
0
    def test_write_irregular_data(self):
        new_data = pd.Series(index=[
            datetime(1900, 1, 1),
            datetime(1900, 3, 1),
            datetime(1900, 4, 1),
            datetime(1900, 6, 1),
        ],
                             data=[1.0, 2.0, 3.0, 4.0])

        log_entries = writer.write(self.tsdb_file, new_data, 'IRR')
        modified = log_entries['U']
        self.assertEqual(0, len(modified))

        data = reader.read(self.tsdb_file)

        self.assertEqual(datetime(1900, 1, 1), data.index[0].to_pydatetime())
        self.assertEqual(datetime(1900, 3, 1), data.index[1].to_pydatetime())
        self.assertEqual(datetime(1900, 4, 1), data.index[2].to_pydatetime())
        self.assertEqual(datetime(1900, 6, 1), data.index[3].to_pydatetime())

        self.assertEqual(4, len(data))

        self.assertEqual(1.0, data.values[0])
        self.assertEqual(2.0, data.values[1])
        self.assertEqual(3.0, data.values[2])
        self.assertEqual(4.0, data.values[3])
Esempio n. 9
0
    def test_irregular_update_and_append(self):
        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(index=[
                datetime(2014, 1, 2),
                datetime(2014, 1, 3),
                datetime(2014, 1, 5),
                datetime(2014, 1, 7),
                datetime(2014, 1, 8)
            ],
                      data=[2.5, 3.0, 5.0, 7.0, 8.0]), 'IRR')
        created = log_entries['C']
        modified = log_entries['U']

        self.assertEqual(1, len(modified))
        self.assertEqual(4, len(created))
        self.assertEqual((1388620800, 2.0, 0), modified[0])
        self.assertEqual((1388620800, 2.5, 0), created[0])

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(1.0, data.values[0])
        self.assertEqual(2.5, data.values[1])
        self.assertEqual(3.0, data.values[2])
        self.assertEqual(5.0, data.values[3])
        self.assertEqual(7.0, data.values[4])
        self.assertEqual(8.0, data.values[5])
        self.assertEqual(datetime(2014, 1, 1), data.index[0].to_pydatetime())
        self.assertEqual(datetime(2014, 1, 2), data.index[1].to_pydatetime())
        self.assertEqual(datetime(2014, 1, 3), data.index[2].to_pydatetime())
        self.assertEqual(datetime(2014, 1, 5), data.index[3].to_pydatetime())
        self.assertEqual(datetime(2014, 1, 7), data.index[4].to_pydatetime())
        self.assertEqual(datetime(2014, 1, 8), data.index[5].to_pydatetime())
Esempio n. 10
0
    def test_log_entries_for_update_nan_multiple_times(self):
        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(index=[datetime(2014, 1, 2),
                             datetime(2014, 1, 3)],
                      data=[np.nan, 3.5]), 'D')
        new_entries = log_entries['C']
        self.assertEqual(2, len(new_entries))

        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(index=[datetime(2014, 1, 2),
                             datetime(2014, 1, 3)],
                      data=[np.nan, 3.5]), 'D')
        new_entries = log_entries['C']
        self.assertEqual(1, len(new_entries))
Esempio n. 11
0
    def test_write_monthly_start_data(self):
        new_data = pd.Series(
                    index = [
                        datetime(2014,6,1),
                        datetime(2014,7,1),
                        datetime(2014,8,1),
                        datetime(2014,9,1)
                    ],
                    data = [
                        6.0,
                        7.3,
                        8.0,
                        9.1
                    ]
                )

        log_entries = writer.write(self.tsdb_file, new_data, 'MS')
        modified = log_entries['U']
        self.assertEqual(0, len(modified))

        data = reader.read(self.tsdb_file)
        self.assertEqual(4, len(data))

        self.assertEqual(6.0, data.values[0])
        self.assertEqual(7.3, data.values[1])
        self.assertEqual(8.0, data.values[2])
        self.assertEqual(9.1, data.values[3])
Esempio n. 12
0
    def test_write_irregular_data(self):
        new_data = pd.Series(
                    index = [
                        datetime(1900,1,1),
                        datetime(1900,3,1),
                        datetime(1900,4,1),
                        datetime(1900,6,1),
                    ],
                    data = [
                        1.0,
                        2.0,
                        3.0,
                        4.0
                    ]
                )

        log_entries = writer.write(self.tsdb_file, new_data, 'IRR')
        modified = log_entries['U']
        self.assertEqual(0, len(modified))

        data = reader.read(self.tsdb_file)

        self.assertEqual(datetime(1900,1,1), data.index[0].to_pydatetime())
        self.assertEqual(datetime(1900,3,1), data.index[1].to_pydatetime())
        self.assertEqual(datetime(1900,4,1), data.index[2].to_pydatetime())
        self.assertEqual(datetime(1900,6,1), data.index[3].to_pydatetime())

        self.assertEqual(4, len(data))

        self.assertEqual(1.0, data.values[0])
        self.assertEqual(2.0, data.values[1])
        self.assertEqual(3.0, data.values[2])
        self.assertEqual(4.0, data.values[3])
Esempio n. 13
0
    def test_irregular_append(self):
        initial_data = reader.read(self.tsdb_existing_file)
        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(
                index = [
                    datetime(2014,1,5),
                    datetime(2014,1,7),
                    datetime(2014,1,8)
                ],
                data = [
                    5.0,
                    7.0,
                    8.0
                ]
            ),
            'IRR'
        )
        created = log_entries['C']
        modified = log_entries['U']

        self.assertEqual(0, len(modified))
        self.assertEqual(3, len(created))
        self.assertEqual((1388880000, 5.0, 0), created[0])

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(len(initial_data) + 3, len(data))
        self.assertEqual(3.0, data.values[2])
        self.assertEqual(5.0, data.values[3])
        self.assertEqual(7.0, data.values[4])
        self.assertEqual(8.0, data.values[5])
Esempio n. 14
0
    def test_irregular_update_of_nan(self):
        writer.write(self.tsdb_existing_file, pd.Series(index = [datetime(2014,1,2), datetime(2014,1,3), datetime(2014,1,5), datetime(2014,1,7), datetime(2014,1,8)], data = [2, np.nan, 5.0, 7.0, 8.0]), 'IRR')

        log_entries = writer.write(self.tsdb_existing_file, pd.Series(index = [datetime(2014,1,2), datetime(2014,1,3), datetime(2014,1,5), datetime(2014,1,7), datetime(2014,1,8)], data = [2, 3.5, 5.0, 7.0, 8.0]), 'IRR')
        modified = log_entries['U']

        self.assertEqual(1, len(modified))
        self.assertEqual(1388707200, modified[0][0])
        self.assertTrue(np.isnan(modified[0][1]))
        self.assertEqual(9999, modified[0][2])

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(1.0, data.values[0])
        self.assertEqual(2.0, data.values[1])
        self.assertEqual(3.5, data.values[2])
        self.assertEqual(datetime(2014,1,3), data.index[2].to_pydatetime())
Esempio n. 15
0
    def test_append_single(self):
        log_entries = writer.write(self.tsdb_existing_file, pd.Series(index = [datetime(2014,1,4)], data = [4.0]), 'D')
        modified = log_entries['U']
        self.assertEqual([], modified)

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(4.0, data.values[-1])
Esempio n. 16
0
    def test_update_over_nan(self):
        # Append a nan value
        log_entries = writer.write(self.tsdb_existing_file, pd.Series(index = [date(2014,1,4)], data = [np.nan]), 'D')

        # Test the nan value was written
        data = reader.read(self.tsdb_existing_file)
        self.assertTrue(np.isnan(data.values[3]))

        # Replace the nan value
        log_entries = writer.write(self.tsdb_existing_file, pd.Series(index = [date(2014,1,4)], data = [4.0]), 'D')

        updated_data = reader.read(self.tsdb_existing_file)
        self.assertEqual(4.0, updated_data.values[3])

        modified = log_entries['U']
        self.assertEqual(1, len(modified))
Esempio n. 17
0
    def test_update_and_append(self):
        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(index=[
                datetime(2014, 1, 2),
                datetime(2014, 1, 3),
                datetime(2014, 1, 4),
                datetime(2014, 1, 5),
                datetime(2014, 1, 6)
            ],
                      data=[2.5, 3.0, 4.0, 5.0, 6.0]), 'D')
        modified = log_entries['U']

        self.assertEqual(1, len(modified))
        self.assertEqual((1388620800, 2.0, 0), modified[0])

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(1.0, data.values[0])
        self.assertEqual(2.5, data.values[1])
        self.assertEqual(3.0, data.values[2])
        self.assertEqual(4.0, data.values[3])
        self.assertEqual(5.0, data.values[4])
        self.assertEqual(6.0, data.values[5])
        self.assertEqual(datetime(2014, 1, 1), data.index[0].to_pydatetime())
        self.assertEqual(datetime(2014, 1, 2), data.index[1].to_pydatetime())
        self.assertEqual(datetime(2014, 1, 3), data.index[2].to_pydatetime())
        self.assertEqual(datetime(2014, 1, 4), data.index[3].to_pydatetime())
        self.assertEqual(datetime(2014, 1, 5), data.index[4].to_pydatetime())
        self.assertEqual(datetime(2014, 1, 6), data.index[5].to_pydatetime())
Esempio n. 18
0
    def test_prepend_with_overlap(self):
        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(
                index = [
                    datetime(2013,12,30),
                    datetime(2013,12,31),
                    datetime(2014,1,1)
                ],
                data = [
                    -2.0,
                    -1.0,
                    0.0
                ]
            ),
            'D'
        )
        created = log_entries['C']
        self.assertEqual(
            [
                (1388361600, -2.0, 0),
                (1388448000, -1.0, 0),
                (1388534400, 0.0, 0)
            ],
            created
        )
        modified = log_entries['U']
        self.assertEqual([(1388534400, 1.0, 0)], modified)

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(-2.0, data.values[0])
        self.assertEqual(-1.0, data.values[1])
        self.assertEqual(0.0, data.values[2])
Esempio n. 19
0
    def test_update_single(self):
        log_entries = writer.write(self.tsdb_existing_file, pd.Series(index = [datetime(2014,1,2)], data = [2.5]), 'D')
        modified = log_entries['U']
        self.assertEqual(1, len(modified))
        self.assertEqual([(1388620800, 2.0, 0)], modified)

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(2.5, data.values[1])
Esempio n. 20
0
    def test_append_single(self):
        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(index=[datetime(2014, 1, 4)], data=[4.0]), 'D')
        modified = log_entries['U']
        self.assertEqual([], modified)

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(4.0, data.values[-1])
Esempio n. 21
0
    def test_prepend_single(self):
        log_entries = writer.write(self.tsdb_existing_file, pd.Series(index = [datetime(2013,12,31)], data = [-1.0]), 'D')
        created = log_entries['C']
        self.assertEqual([(1388448000, -1.0, 0)], created)
        modified = log_entries['U']
        self.assertEqual([], modified)

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(-1.0, data.values[0])
Esempio n. 22
0
    def test_update_multiple(self):
        log_entries = writer.write(self.tsdb_existing_file, pd.Series(index = [datetime(2014,1,2),datetime(2014,1,3)], data = [2.5, 3.5]), 'D')
        modified = log_entries['U']
        self.assertEqual(2, len(modified))
        self.assertEqual((1388620800, 2.0, 0), modified[0])
        self.assertEqual((1388707200, 3.0, 0), modified[1])

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(2.5, data.values[1])
        self.assertEqual(3.5, data.values[2])
Esempio n. 23
0
    def test_update_single(self):
        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(index=[datetime(2014, 1, 2)], data=[2.5]), 'D')
        modified = log_entries['U']
        self.assertEqual(1, len(modified))
        self.assertEqual([(1388620800, 2.0, 0)], modified)

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(2.5, data.values[1])
Esempio n. 24
0
    def test_prepend_with_gap(self):
        log_entries = writer.write(self.tsdb_existing_file, pd.Series(index = [datetime(2013,12,30)], data = [-2.0]), 'D')
        created = log_entries['C']
        self.assertEqual([(1388361600, -2.0, 0), (1388448000, -9999, 9999)], created)
        modified = log_entries['U']
        self.assertEqual([], modified)

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(-2.0, data.values[0])
        self.assertTrue(np.isnan(data.values[1]))
Esempio n. 25
0
    def test_new_write_and_update_minute_data(self):
        writer.write(
            self.tsdb_file,
            pd.Series(index=[
                datetime(2014, 1, 1, 18, 1, 0),
                datetime(2014, 1, 1, 18, 2, 0),
                datetime(2014, 1, 1, 18, 3, 0)
            ],
                      data=[1.0, np.nan, 3.0]), '1T')

        data = reader.read(self.tsdb_file)
        self.assertEqual(1.0, data.values[0])
        self.assertTrue(np.isnan(data.values[1]))
        self.assertEqual(3.0, data.values[2])

        self.assertEqual(datetime(2014, 1, 1, 18, 1, 0),
                         data.index[0].to_pydatetime())
        self.assertEqual(datetime(2014, 1, 1, 18, 2, 0),
                         data.index[1].to_pydatetime())
        self.assertEqual(datetime(2014, 1, 1, 18, 3, 0),
                         data.index[2].to_pydatetime())

        writer.write(
            self.tsdb_file,
            pd.Series(index=[
                datetime(2014, 1, 1, 18, 3, 0),
                datetime(2014, 1, 1, 18, 4, 0),
                datetime(2014, 1, 1, 18, 5, 0)
            ],
                      data=[3.5, 4.0, 5.0]), '1T')

        data = reader.read(self.tsdb_file)
        self.assertEqual(1.0, data.values[0])
        self.assertTrue(np.isnan(data.values[1]))
        self.assertEqual(3.5, data.values[2])
        self.assertEqual(4.0, data.values[3])
        self.assertEqual(5.0, data.values[4])

        self.assertEqual(datetime(2014, 1, 1, 18, 1, 0),
                         data.index[0].to_pydatetime())
        self.assertEqual(datetime(2014, 1, 1, 18, 5, 0),
                         data.index[-1].to_pydatetime())
Esempio n. 26
0
    def test_append_with_large_gap(self):
        writer.write(self.tsdb_file, pd.Series(index = [datetime(2005,1,1)], data = [1.0]), 'H')
        start_time = time.time()
        writer.write(self.tsdb_file, pd.Series(index = [datetime(2014,12,31)], data = [2.0]), 'H')
        end_time = time.time()

        # Prior to the fix committed with this test, on my Macbook Air,
        # the run time here was around 1.3 seconds. After the fix
        # it is down to almost half a second (around 0.56 seconds for
        # the total test run time).
        # The performance improvement is larger with bigger gaps.
        self.assertLessEqual((end_time - start_time), 1)

        # A few spot checks to make sure the test actually ran as expected.
        data = reader.read(self.tsdb_file)
        self.assertEqual(1.0, data.values[0])
        self.assertTrue(np.isnan(data.values[1]))
        self.assertEqual(2.0, data.values[-1])
        self.assertEqual(datetime(2005,1,1,0,0), data.index[0].to_pydatetime())
        self.assertEqual(datetime(2014,12,31), data.index[-1].to_pydatetime())
Esempio n. 27
0
    def test_prepend_single(self):
        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(index=[datetime(2013, 12, 31)], data=[-1.0]), 'D')
        created = log_entries['C']
        self.assertEqual([(1388448000, -1.0, 0)], created)
        modified = log_entries['U']
        self.assertEqual([], modified)

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(-1.0, data.values[0])
Esempio n. 28
0
    def test_update_over_nan(self):
        # Append a nan value
        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(index=[date(2014, 1, 4)], data=[np.nan]), 'D')

        # Test the nan value was written
        data = reader.read(self.tsdb_existing_file)
        self.assertTrue(np.isnan(data.values[3]))

        # Replace the nan value
        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(index=[date(2014, 1, 4)], data=[4.0]), 'D')

        updated_data = reader.read(self.tsdb_existing_file)
        self.assertEqual(4.0, updated_data.values[3])

        modified = log_entries['U']
        self.assertEqual(1, len(modified))
Esempio n. 29
0
    def test_write_overlapping_hourly(self):
        input_a = pd.Series(
                    index = [
                        datetime(2014,1,1,0,0,0),
                        datetime(2014,1,1,1,0,0),
                        datetime(2014,1,1,2,0,0)
                    ],
                    data = [
                        1.0,
                        2.0,
                        3.0
                    ]
                )
        input_b = pd.Series(
                    index = [
                        datetime(2014,1,1,2,0,0),
                        datetime(2014,1,1,3,0,0),
                        datetime(2014,1,1,4,0,0),
                        datetime(2014,1,1,5,0,0)
                    ],
                    data = [
                        4.0,
                        5.0,
                        6.0,
                        7.0
                    ]
                )

        writer.write(self.tsdb_file, input_a, 'H')
        log_entries = writer.write(self.tsdb_file, input_b, 'H')
        modified = log_entries['U']
        self.assertEqual(1, len(modified))
        self.assertEqual([(1388541600, 3.0, 0)], modified)

        data = reader.read(self.tsdb_file)
        self.assertEqual(1.0, data.values[0])
        self.assertEqual(2.0, data.values[1])
        self.assertEqual(4.0, data.values[2])
        self.assertEqual(5.0, data.values[3])
        self.assertEqual(6.0, data.values[4])
        self.assertEqual(7.0, data.values[5])
Esempio n. 30
0
    def test_append_multiple(self):
        log_entries = writer.write(self.tsdb_existing_file, pd.Series(index = [datetime(2014,1,4), datetime(2014,1,5), datetime(2014,1,6)], data = [4.0, 5.0, 6.0]), 'D')
        modified = log_entries['U']
        self.assertEqual([], modified)

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(1.0, data.values[0])
        self.assertEqual(2.0, data.values[1])
        self.assertEqual(3.0, data.values[2])
        self.assertEqual(4.0, data.values[3])
        self.assertEqual(5.0, data.values[4])
        self.assertEqual(6.0, data.values[5])
Esempio n. 31
0
    def test_prepend_with_gap(self):
        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(index=[datetime(2013, 12, 30)], data=[-2.0]), 'D')
        created = log_entries['C']
        self.assertEqual([(1388361600, -2.0, 0), (1388448000, -9999, 9999)],
                         created)
        modified = log_entries['U']
        self.assertEqual([], modified)

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(-2.0, data.values[0])
        self.assertTrue(np.isnan(data.values[1]))
Esempio n. 32
0
    def test_write_missing_date(self):
        log_entries = writer.write(self.tsdb_existing_file, pd.Series(index = [date(2014,1,3),date(2014,1,5),date(2014,1,6)], data = [3.0, np.nan, 6.5]), 'D')
        modified = log_entries['U']

        self.assertEqual(0, len(modified))

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(1.0, data.values[0])
        self.assertEqual(2.0, data.values[1])
        self.assertEqual(3.0, data.values[2])
        self.assertTrue(np.isnan(data.values[3]))
        self.assertTrue(np.isnan(data.values[4]))
        self.assertEqual(6.5, data.values[5])
Esempio n. 33
0
    def test_update_multiple(self):
        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(index=[datetime(2014, 1, 2),
                             datetime(2014, 1, 3)],
                      data=[2.5, 3.5]), 'D')
        modified = log_entries['U']
        self.assertEqual(2, len(modified))
        self.assertEqual((1388620800, 2.0, 0), modified[0])
        self.assertEqual((1388707200, 3.0, 0), modified[1])

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(2.5, data.values[1])
        self.assertEqual(3.5, data.values[2])
Esempio n. 34
0
    def test_new_write_and_update_minute_data(self):
        writer.write(self.tsdb_file, pd.Series(index = [datetime(2014,1,1,18,1,0), datetime(2014,1,1,18,2,0), datetime(2014,1,1,18,3,0)], data = [1.0, np.nan, 3.0]), '1T')

        data = reader.read(self.tsdb_file)
        self.assertEqual(1.0, data.values[0])
        self.assertTrue(np.isnan(data.values[1]))
        self.assertEqual(3.0, data.values[2])

        self.assertEqual(datetime(2014,1,1,18,1,0), data.index[0].to_pydatetime())
        self.assertEqual(datetime(2014,1,1,18,2,0), data.index[1].to_pydatetime())
        self.assertEqual(datetime(2014,1,1,18,3,0), data.index[2].to_pydatetime())

        writer.write(self.tsdb_file, pd.Series(index = [datetime(2014,1,1,18,3,0), datetime(2014,1,1,18,4,0), datetime(2014,1,1,18,5,0)], data = [3.5, 4.0, 5.0]), '1T')

        data = reader.read(self.tsdb_file)
        self.assertEqual(1.0, data.values[0])
        self.assertTrue(np.isnan(data.values[1]))
        self.assertEqual(3.5, data.values[2])
        self.assertEqual(4.0, data.values[3])
        self.assertEqual(5.0, data.values[4])

        self.assertEqual(datetime(2014,1,1,18,1,0), data.index[0].to_pydatetime())
        self.assertEqual(datetime(2014,1,1,18,5,0), data.index[-1].to_pydatetime())
Esempio n. 35
0
    def write(self, identifier, freq, ts, **kwargs):
        """
            Write/update timeseries data for existing timeseries.

            :param identifier: Identifier of the timeseries.
            :type identifier: string
            :param freq: Data frequency (e.g. 'D' for day, as supported by pandas.)
            :type freq: string
            :param ts: Timeseries data to write into the database.
            :type ts: pd.Series
        """
        modified = writer.write(self.get_file_path(identifier, freq, **kwargs), ts, freq)

        log_file = self.get_file_path(identifier, freq, ftype = 'hdf5', **kwargs)

        writer.write_log(log_file, modified, datetime.utcnow())
Esempio n. 36
0
    def test_append_monthly_end_data(self):
        new_data = pd.Series(index=[
            datetime(1901, 1, 31),
            datetime(1901, 2, 28),
        ],
                             data=[31.1, 28.2])

        log_entries = writer.write(self.tsdb_monthly_existing_file, new_data,
                                   'M')
        modified = log_entries['U']
        self.assertEqual(0, len(modified))

        data = reader.read(self.tsdb_monthly_existing_file)
        self.assertEqual(14, len(data))

        self.assertEqual(31.1, data.values[-2])
        self.assertEqual(28.2, data.values[-1])
Esempio n. 37
0
    def test_update_and_append_with_gap(self):
        log_entries = writer.write(self.tsdb_existing_file, pd.Series(index = [datetime(2014,1,5), datetime(2014,1,6)], data = [5.0, 6.0]), 'D')
        modified = log_entries['U']

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(1.0, data.values[0])
        self.assertEqual(2.0, data.values[1])
        self.assertEqual(3.0, data.values[2])
        self.assertTrue(np.isnan(data.values[3]))
        self.assertEqual(5.0, data.values[4])
        self.assertEqual(6.0, data.values[5])
        self.assertEqual(datetime(2014,1,1), data.index[0].to_pydatetime())
        self.assertEqual(datetime(2014,1,2), data.index[1].to_pydatetime())
        self.assertEqual(datetime(2014,1,3), data.index[2].to_pydatetime())
        self.assertEqual(datetime(2014,1,4), data.index[3].to_pydatetime())
        self.assertEqual(datetime(2014,1,5), data.index[4].to_pydatetime())
        self.assertEqual(datetime(2014,1,6), data.index[5].to_pydatetime())
Esempio n. 38
0
    def write(self, identifier, freq, ts, **kwargs):
        """
            Write/update timeseries data for existing timeseries.

            :param identifier: Identifier of the timeseries.
            :type identifier: string
            :param freq: Data frequency (e.g. 'D' for day, as supported by pandas.)
            :type freq: string
            :param ts: Timeseries data to write into the database.
            :type ts: pd.Series
        """
        modified = writer.write(self.get_file_path(identifier, freq, **kwargs),
                                ts, freq)

        log_file = self.get_file_path(identifier, freq, ftype='hdf5', **kwargs)

        writer.write_log(log_file, modified, datetime.utcnow())
Esempio n. 39
0
    def test_append_monthly_start_data(self):
        new_data = pd.Series(index=[
            datetime(1901, 1, 1),
            datetime(1901, 2, 1),
        ],
                             data=[31.1, 28.2])

        log_entries = writer.write(self.tsdb_monthly_start_existing_file,
                                   new_data, 'MS')
        modified = log_entries['U']
        self.assertEqual(0, len(modified))

        data = reader.read(self.tsdb_monthly_start_existing_file)

        self.assertEqual(datetime(1900, 1, 1), data.index[0].to_pydatetime())
        self.assertEqual(datetime(1901, 2, 1), data.index[-1].to_pydatetime())

        self.assertEqual(14, len(data))
Esempio n. 40
0
    def test_append_30min(self):
        new_data = pd.Series(index=[
            datetime(2014, 8, 30, 6, 0, 0),
            datetime(2014, 8, 30, 6, 30, 0)
        ],
                             data=[6.0, 6.30])

        log_entries = writer.write(self.tsdb_30min_existing_file, new_data,
                                   '30min')
        modified = log_entries['U']
        self.assertEqual(0, len(modified))

        data = reader.read(self.tsdb_30min_existing_file)
        self.assertEqual(148, len(data))

        self.assertEqual(17.2, data.values[0])
        self.assertTrue(np.isnan(data.values[-3]))
        self.assertEqual(6.0, data.values[-2])
        self.assertEqual(6.3, data.values[-1])
Esempio n. 41
0
    def test_write_missing_date(self):
        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(
                index=[date(2014, 1, 3),
                       date(2014, 1, 5),
                       date(2014, 1, 6)],
                data=[3.0, np.nan, 6.5]), 'D')
        modified = log_entries['U']

        self.assertEqual(0, len(modified))

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(1.0, data.values[0])
        self.assertEqual(2.0, data.values[1])
        self.assertEqual(3.0, data.values[2])
        self.assertTrue(np.isnan(data.values[3]))
        self.assertTrue(np.isnan(data.values[4]))
        self.assertEqual(6.5, data.values[5])
Esempio n. 42
0
    def test_prepend_with_overlap(self):
        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(index=[
                datetime(2013, 12, 30),
                datetime(2013, 12, 31),
                datetime(2014, 1, 1)
            ],
                      data=[-2.0, -1.0, 0.0]), 'D')
        created = log_entries['C']
        self.assertEqual([(1388361600, -2.0, 0), (1388448000, -1.0, 0),
                          (1388534400, 0.0, 0)], created)
        modified = log_entries['U']
        self.assertEqual([(1388534400, 1.0, 0)], modified)

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(-2.0, data.values[0])
        self.assertEqual(-1.0, data.values[1])
        self.assertEqual(0.0, data.values[2])
Esempio n. 43
0
    def test_append_multiple(self):
        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(index=[
                datetime(2014, 1, 4),
                datetime(2014, 1, 5),
                datetime(2014, 1, 6)
            ],
                      data=[4.0, 5.0, 6.0]), 'D')
        modified = log_entries['U']
        self.assertEqual([], modified)

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(1.0, data.values[0])
        self.assertEqual(2.0, data.values[1])
        self.assertEqual(3.0, data.values[2])
        self.assertEqual(4.0, data.values[3])
        self.assertEqual(5.0, data.values[4])
        self.assertEqual(6.0, data.values[5])
Esempio n. 44
0
    def test_float32_irregular_write(self):
        """
            Test irregular write of float32 data.

            See: https://github.com/amacd31/phildb/issues/16
        """

        sample = pd.Series(pd.np.array([x + 0.1 for x in range(10)],
                                       dtype=pd.np.float32),
                           index=pd.date_range('2017-08-06 06:50:00',
                                               periods=10,
                                               freq='1T'))

        log_entries = writer.write(self.tsdb_existing_file, sample, 'IRR')
        data = reader.read(self.tsdb_existing_file)

        self.assertEqual(datetime(2017, 8, 6, 6, 50, 0, 0),
                         data.index[3].to_pydatetime())
        self.assertEqual(datetime(2017, 8, 6, 6, 51, 0, 0),
                         data.index[4].to_pydatetime())
Esempio n. 45
0
    def test_update_and_append(self):
        log_entries = writer.write(self.tsdb_existing_file, pd.Series(index = [datetime(2014,1,2), datetime(2014,1,3), datetime(2014,1,4), datetime(2014,1,5), datetime(2014,1,6)], data = [2.5, 3.0, 4.0, 5.0, 6.0]), 'D')
        modified = log_entries['U']

        self.assertEqual(1, len(modified))
        self.assertEqual((1388620800, 2.0, 0), modified[0])

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(1.0, data.values[0])
        self.assertEqual(2.5, data.values[1])
        self.assertEqual(3.0, data.values[2])
        self.assertEqual(4.0, data.values[3])
        self.assertEqual(5.0, data.values[4])
        self.assertEqual(6.0, data.values[5])
        self.assertEqual(datetime(2014,1,1), data.index[0].to_pydatetime())
        self.assertEqual(datetime(2014,1,2), data.index[1].to_pydatetime())
        self.assertEqual(datetime(2014,1,3), data.index[2].to_pydatetime())
        self.assertEqual(datetime(2014,1,4), data.index[3].to_pydatetime())
        self.assertEqual(datetime(2014,1,5), data.index[4].to_pydatetime())
        self.assertEqual(datetime(2014,1,6), data.index[5].to_pydatetime())
Esempio n. 46
0
    def test_write_monthly_start_data(self):
        new_data = pd.Series(index=[
            datetime(2014, 6, 1),
            datetime(2014, 7, 1),
            datetime(2014, 8, 1),
            datetime(2014, 9, 1)
        ],
                             data=[6.0, 7.3, 8.0, 9.1])

        log_entries = writer.write(self.tsdb_file, new_data, 'MS')
        modified = log_entries['U']
        self.assertEqual(0, len(modified))

        data = reader.read(self.tsdb_file)
        self.assertEqual(4, len(data))

        self.assertEqual(6.0, data.values[0])
        self.assertEqual(7.3, data.values[1])
        self.assertEqual(8.0, data.values[2])
        self.assertEqual(9.1, data.values[3])
Esempio n. 47
0
    def test_update_and_append_with_gap(self):
        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(index=[datetime(2014, 1, 5),
                             datetime(2014, 1, 6)],
                      data=[5.0, 6.0]), 'D')
        modified = log_entries['U']

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(1.0, data.values[0])
        self.assertEqual(2.0, data.values[1])
        self.assertEqual(3.0, data.values[2])
        self.assertTrue(np.isnan(data.values[3]))
        self.assertEqual(5.0, data.values[4])
        self.assertEqual(6.0, data.values[5])
        self.assertEqual(datetime(2014, 1, 1), data.index[0].to_pydatetime())
        self.assertEqual(datetime(2014, 1, 2), data.index[1].to_pydatetime())
        self.assertEqual(datetime(2014, 1, 3), data.index[2].to_pydatetime())
        self.assertEqual(datetime(2014, 1, 4), data.index[3].to_pydatetime())
        self.assertEqual(datetime(2014, 1, 5), data.index[4].to_pydatetime())
        self.assertEqual(datetime(2014, 1, 6), data.index[5].to_pydatetime())
Esempio n. 48
0
    def test_append_monthly_end_data(self):
        new_data = pd.Series(
                    index = [
                        datetime(1901,1,31),
                        datetime(1901,2,28),
                    ],
                    data = [
                        31.1,
                        28.2
                    ]
                )

        log_entries = writer.write(self.tsdb_monthly_existing_file, new_data, 'M')
        modified = log_entries['U']
        self.assertEqual(0, len(modified))

        data = reader.read(self.tsdb_monthly_existing_file)
        self.assertEqual(14, len(data))

        self.assertEqual(31.1, data.values[-2])
        self.assertEqual(28.2, data.values[-1])
Esempio n. 49
0
    def test_irregular_append(self):
        log_entries = writer.write(
            self.tsdb_existing_file,
            pd.Series(index=[
                datetime(2014, 1, 3),
                datetime(2014, 1, 5),
                datetime(2014, 1, 7),
                datetime(2014, 1, 8)
            ],
                      data=[3.0, 5.0, 7.0, 8.0]), 'IRR')
        created = log_entries['C']
        modified = log_entries['U']

        self.assertEqual(0, len(modified))
        self.assertEqual(4, len(created))
        self.assertEqual((1388707200, 3.0, 0), created[0])

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(3.0, data.values[2])
        self.assertEqual(5.0, data.values[3])
        self.assertEqual(7.0, data.values[4])
        self.assertEqual(8.0, data.values[5])
Esempio n. 50
0
    def test_append_monthly_start_data(self):
        new_data = pd.Series(
                    index = [
                        datetime(1901,1,1),
                        datetime(1901,2,1),
                    ],
                    data = [
                        31.1,
                        28.2
                    ]
                )

        log_entries = writer.write(self.tsdb_monthly_start_existing_file, new_data, 'MS')
        modified = log_entries['U']
        self.assertEqual(0, len(modified))

        data = reader.read(self.tsdb_monthly_start_existing_file)

        self.assertEqual(datetime(1900,1,1), data.index[0].to_pydatetime())
        self.assertEqual(datetime(1901,2,1), data.index[-1].to_pydatetime())

        self.assertEqual(14, len(data))
Esempio n. 51
0
    def test_append_30min(self):
        new_data = pd.Series(
                    index = [
                        datetime(2014,8,30,6,0,0),
                        datetime(2014,8,30,6,30,0)
                    ],
                    data = [
                        6.0,
                        6.30
                    ]
                )

        log_entries = writer.write(self.tsdb_30min_existing_file, new_data, '30min')
        modified = log_entries['U']
        self.assertEqual(0, len(modified))

        data = reader.read(self.tsdb_30min_existing_file)
        self.assertEqual(148, len(data))

        self.assertEqual(17.2, data.values[0])
        self.assertTrue(np.isnan(data.values[-3]))
        self.assertEqual(6.0, data.values[-2])
        self.assertEqual(6.3, data.values[-1])
Esempio n. 52
0
    def test_irregular_update_and_append(self):
        log_entries = writer.write(self.tsdb_existing_file, pd.Series(index = [datetime(2014,1,2), datetime(2014,1,3), datetime(2014,1,5), datetime(2014,1,7), datetime(2014,1,8)], data = [2.5, 3.0, 5.0, 7.0, 8.0]), 'IRR')
        created = log_entries['C']
        modified = log_entries['U']

        self.assertEqual(1, len(modified))
        self.assertEqual(4, len(created))
        self.assertEqual((1388620800, 2.0, 0), modified[0])
        self.assertEqual((1388620800, 2.5, 0), created[0])

        data = reader.read(self.tsdb_existing_file)
        self.assertEqual(1.0, data.values[0])
        self.assertEqual(2.5, data.values[1])
        self.assertEqual(3.0, data.values[2])
        self.assertEqual(5.0, data.values[3])
        self.assertEqual(7.0, data.values[4])
        self.assertEqual(8.0, data.values[5])
        self.assertEqual(datetime(2014,1,1), data.index[0].to_pydatetime())
        self.assertEqual(datetime(2014,1,2), data.index[1].to_pydatetime())
        self.assertEqual(datetime(2014,1,3), data.index[2].to_pydatetime())
        self.assertEqual(datetime(2014,1,5), data.index[3].to_pydatetime())
        self.assertEqual(datetime(2014,1,7), data.index[4].to_pydatetime())
        self.assertEqual(datetime(2014,1,8), data.index[5].to_pydatetime())
Esempio n. 53
0
    def test_float32_irregular_write(self):
        """
            Test irregular write of float32 data.

            See: https://github.com/amacd31/phildb/issues/16
        """

        sample = pd.Series(
            pd.np.array(
                [x + 0.1 for x in range(10)],
                dtype=pd.np.float32
            ),
            index=pd.date_range(
                '2017-08-06 06:50:00',
                periods=10,
                freq='1T'
            )
        )

        log_entries = writer.write(self.tsdb_existing_file, sample, 'IRR')
        data = reader.read(self.tsdb_existing_file)

        self.assertEqual(datetime(2017,8,6,6,50,0,0), data.index[3].to_pydatetime())
        self.assertEqual(datetime(2017,8,6,6,51,0,0), data.index[4].to_pydatetime())
Esempio n. 54
0
 def test_empty_series_write(self):
     log_entries = writer.write(self.tsdb_existing_file, pd.Series([]), 'D')
     self.assertEqual(0, len(log_entries['C']))
     self.assertEqual(0, len(log_entries['U']))
Esempio n. 55
0
 def test_empty_series_write(self):
     log_entries = writer.write(self.tsdb_existing_file, pd.Series([]), 'D')
     self.assertEqual(0, len(log_entries['C']))
     self.assertEqual(0, len(log_entries['U']))
Esempio n. 56
0
    def test_new_write(self):
        writer.write(self.tsdb_file, pd.Series(index = [datetime(2014,1,1), datetime(2014,1,2), datetime(2014,1,3)], data = [1.0, 2.0, 3.0]), 'D')
        with open(self.tsdb_file, 'rb') as file:
            datafile = file.read()

        self.assertEqual('06606801154cbfdc8e1b8c7b1e3c1956', hashlib.md5(datafile).hexdigest())