Brendan Jackman | e81fdcb | 2017-01-04 17:10:29 +0000 | [diff] [blame] | 1 | # Copyright 2015-2017 ARM Limited |
Kapileshwar Singh | c5c1e31 | 2015-12-09 23:09:29 +0000 | [diff] [blame] | 2 | # |
| 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 | |
| 17 | import unittest |
| 18 | from trappy import utils |
| 19 | import pandas |
Javi Merino | d3457bf | 2016-01-14 16:09:36 +0000 | [diff] [blame] | 20 | from pandas.util.testing import assert_series_equal |
Kapileshwar Singh | c5c1e31 | 2015-12-09 23:09:29 +0000 | [diff] [blame] | 21 | |
| 22 | |
| 23 | class 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 Merino | d3457bf | 2016-01-14 16:09:36 +0000 | [diff] [blame] | 38 | 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 Singh | c5c1e31 | 2015-12-09 23:09:29 +0000 | [diff] [blame] | 45 | series.reindex(new_index) |
Javi Merino | 08c8d29 | 2016-01-14 16:17:26 +0000 | [diff] [blame] | 46 | |
| 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) |