blob: bf53091370f141b58fe5c9021038f74fb7877de4 [file] [log] [blame]
Brendan Jackmane81fdcb2017-01-04 17:10:29 +00001# Copyright 2015-2017 ARM Limited
Kapileshwar Singhc5c1e312015-12-09 23:09:29 +00002#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15
16
17import unittest
18from trappy import utils
19import pandas
Javi Merinod3457bf2016-01-14 16:09:36 +000020from pandas.util.testing import assert_series_equal
Kapileshwar Singhc5c1e312015-12-09 23:09:29 +000021
22
23class TestUtils(unittest.TestCase):
24
25 def test_handle_duplicate_index(self):
26 """Test Util Function: handle_duplicate_index
27 """
28
29 # Refer to the example in the function doc string
30 values = [0, 1, 2, 3, 4]
31 index = [0.0, 1.0, 1.0, 6.0, 7.0]
32 series = pandas.Series(values, index=index)
33 new_index = [0.0, 1.0, 2.0, 3.0, 4.0, 6.0, 7.0]
34
35 with self.assertRaises(ValueError):
36 series.reindex(new_index)
37
Javi Merinod3457bf2016-01-14 16:09:36 +000038 max_delta = 0.001
39 expected_index = [0.0, 1.0, 1 + max_delta, 6.0, 7.0]
40 expected_series = pandas.Series(values, index=expected_index)
41 series = utils.handle_duplicate_index(series, max_delta)
42 assert_series_equal(series, expected_series)
43
44 # Make sure that the reindex doesn't raise ValueError any more
Kapileshwar Singhc5c1e312015-12-09 23:09:29 +000045 series.reindex(new_index)
Javi Merino08c8d292016-01-14 16:17:26 +000046
47 def test_handle_duplicate_index_duplicate_end(self):
48 """handle_duplicate_index copes with duplicates at the end of the series"""
49
50 max_delta = 0.001
51 values = [0, 1, 2, 3, 4]
52 index = [0.0, 1.0, 2.0, 6.0, 6.0]
53 expected_index = index[:]
54 expected_index[-1] += max_delta
55 series = pandas.Series(values, index=index)
56 expected_series = pandas.Series(values, index=expected_index)
57
58 series = utils.handle_duplicate_index(series, max_delta)
59 assert_series_equal(series, expected_series)