def test_labels(self, verify): values = [3, 1, 2, 0, 4] expected = np.array([0, 1, 2, 3, 4]) labels = [0, 1, 1, 2, 3, 0, -1, 4] result, result_labels = safe_sort(values, labels, verify=verify) expected_labels = np.array([3, 1, 1, 2, 0, 3, -1, 4], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) tm.assert_numpy_array_equal(result_labels, expected_labels) # na_sentinel labels = [0, 1, 1, 2, 3, 0, 99, 4] result, result_labels = safe_sort(values, labels, na_sentinel=99, verify=verify) expected_labels = np.array([3, 1, 1, 2, 0, 3, 99, 4], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) tm.assert_numpy_array_equal(result_labels, expected_labels) labels = [] result, result_labels = safe_sort(values, labels, verify=verify) expected_labels = np.array([], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) tm.assert_numpy_array_equal(result_labels, expected_labels)
def test_labels(self): values = [3, 1, 2, 0, 4] expected = np.array([0, 1, 2, 3, 4]) labels = [0, 1, 1, 2, 3, 0, -1, 4] result, result_labels = safe_sort(values, labels) expected_labels = np.array([3, 1, 1, 2, 0, 3, -1, 4], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) tm.assert_numpy_array_equal(result_labels, expected_labels) # na_sentinel labels = [0, 1, 1, 2, 3, 0, 99, 4] result, result_labels = safe_sort(values, labels, na_sentinel=99) expected_labels = np.array([3, 1, 1, 2, 0, 3, 99, 4], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) tm.assert_numpy_array_equal(result_labels, expected_labels) # out of bound indices labels = [0, 101, 102, 2, 3, 0, 99, 4] result, result_labels = safe_sort(values, labels) expected_labels = np.array([3, -1, -1, 2, 0, 3, -1, 4], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) tm.assert_numpy_array_equal(result_labels, expected_labels) labels = [] result, result_labels = safe_sort(values, labels) expected_labels = np.array([], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) tm.assert_numpy_array_equal(result_labels, expected_labels)
def test_labels(self): values = [3, 1, 2, 0, 4] expected = np.array([0, 1, 2, 3, 4]) labels = [0, 1, 1, 2, 3, 0, -1, 4] result, result_labels = safe_sort(values, labels) expected_labels = np.array([3, 1, 1, 2, 0, 3, -1, 4], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) tm.assert_numpy_array_equal(result_labels, expected_labels) # na_sentinel labels = [0, 1, 1, 2, 3, 0, 99, 4] result, result_labels = safe_sort(values, labels, na_sentinel=99) expected_labels = np.array([3, 1, 1, 2, 0, 3, 99, 4], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) tm.assert_numpy_array_equal(result_labels, expected_labels) # out of bound indices labels = [0, 101, 102, 2, 3, 0, 99, 4] result, result_labels = safe_sort(values, labels) expected_labels = np.array([3, -1, -1, 2, 0, 3, -1, 4], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) tm.assert_numpy_array_equal(result_labels, expected_labels) labels = [] result, result_labels = safe_sort(values, labels) expected_labels = np.array([], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) tm.assert_numpy_array_equal(result_labels, expected_labels)
def test_unsortable(self): # GH 13714 arr = np.array([1, 2, datetime.now(), 0, 3], dtype=object) msg = ("unorderable types: .* [<>] .*" "|" # the above case happens for numpy < 1.14 "'[<>]' not supported between instances of .*") with pytest.raises(TypeError, match=msg): safe_sort(arr)
def test_unsortable(self): # GH 13714 arr = np.array([1, 2, datetime.now(), 0, 3], dtype=object) msg = ("unorderable types: .* [<>] .*" "|" # the above case happens for numpy < 1.14 "'[<>]' not supported between instances of .*") with pytest.raises(TypeError, match=msg): safe_sort(arr)
def test_unsortable(self): # GH 13714 arr = np.array([1, 2, datetime.now(), 0, 3], dtype=object) msg = (r"'(<|>)' not supported between instances of ('" r"datetime\.datetime' and 'int'|'int' and 'datetime\.datetime" r"')|" r"unorderable types: int\(\) > datetime\.datetime\(\)") with pytest.raises(TypeError, match=msg): safe_sort(arr)
def test_unsortable(self): # GH 13714 arr = np.array([1, 2, datetime.now(), 0, 3], dtype=object) msg = ( r"unorderable types: ({0} [<>] {1}|{1} [<>] {0})".format( r"int\(\)", r"datetime\.datetime\(\)") # noqa: E126 if _np_version_under1p14 else (r"'[<>]' not supported between instances of " r"({0} and {1}|{1} and {0})").format("'int'", r"'datetime\.datetime'")) with pytest.raises(TypeError, match=msg): safe_sort(arr)
def test_pairwise_with_other(self, f): # DataFrame with another DataFrame, pairwise=True results = [f(df, self.df2) for df in self.df1s] for (df, result) in zip(self.df1s, results): tm.assert_index_equal(result.index.levels[0], df.index, check_names=False) tm.assert_numpy_array_equal( safe_sort(result.index.levels[1]), safe_sort(self.df2.columns.unique()) ) for i, result in enumerate(results): if i > 0: self.compare(result, results[0])
def test_mixed_integer(self): values = np.array(['b', 1, 0, 'a', 0, 'b'], dtype=object) result = safe_sort(values) expected = np.array([0, 0, 1, 'a', 'b', 'b'], dtype=object) tm.assert_numpy_array_equal(result, expected) values = np.array(['b', 1, 0, 'a'], dtype=object) labels = [0, 1, 2, 3, 0, -1, 1] result, result_labels = safe_sort(values, labels) expected = np.array([0, 1, 'a', 'b'], dtype=object) expected_labels = np.array([3, 1, 0, 2, 3, -1, 1], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) tm.assert_numpy_array_equal(result_labels, expected_labels)
def test_mixed_integer(self): values = np.array(['b', 1, 0, 'a', 0, 'b'], dtype=object) result = safe_sort(values) expected = np.array([0, 0, 1, 'a', 'b', 'b'], dtype=object) tm.assert_numpy_array_equal(result, expected) values = np.array(['b', 1, 0, 'a'], dtype=object) labels = [0, 1, 2, 3, 0, -1, 1] result, result_labels = safe_sort(values, labels) expected = np.array([0, 1, 'a', 'b'], dtype=object) expected_labels = np.array([3, 1, 0, 2, 3, -1, 1], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) tm.assert_numpy_array_equal(result_labels, expected_labels)
def test_unsortable(self): # GH 13714 arr = np.array([1, 2, datetime.now(), 0, 3], dtype=object) msg = ("'<' not supported between instances of 'datetime.datetime'" r" and 'int'|" r"unorderable types: int\(\) > datetime.datetime\(\)") if compat.PY2: # RuntimeWarning: tp_compare didn't return -1 or -2 for exception with warnings.catch_warnings(): with pytest.raises(TypeError, match=msg): safe_sort(arr) else: with pytest.raises(TypeError, match=msg): safe_sort(arr)
def test_unsortable(self): # GH 13714 arr = np.array([1, 2, datetime.now(), 0, 3], dtype=object) msg = (r"'(<|>)' not supported between instances of ('" r"datetime\.datetime' and 'int'|'int' and 'datetime\.datetime" r"')|" r"unorderable types: int\(\) > datetime\.datetime\(\)") if compat.PY2: # RuntimeWarning: tp_compare didn't return -1 or -2 for exception with warnings.catch_warnings(): with pytest.raises(TypeError, match=msg): safe_sort(arr) else: with pytest.raises(TypeError, match=msg): safe_sort(arr)
def test_basic_sort(self): values = [3, 1, 2, 0, 4] result = safe_sort(values) expected = np.array([0, 1, 2, 3, 4]) tm.assert_numpy_array_equal(result, expected) values = list("baaacb") result = safe_sort(values) expected = np.array(list("aaabbc"), dtype='object') tm.assert_numpy_array_equal(result, expected) values = [] result = safe_sort(values) expected = np.array([]) tm.assert_numpy_array_equal(result, expected)
def test_basic_sort(self): values = [3, 1, 2, 0, 4] result = safe_sort(values) expected = np.array([0, 1, 2, 3, 4]) tm.assert_numpy_array_equal(result, expected) values = list("baaacb") result = safe_sort(values) expected = np.array(list("aaabbc"), dtype='object') tm.assert_numpy_array_equal(result, expected) values = [] result = safe_sort(values) expected = np.array([]) tm.assert_numpy_array_equal(result, expected)
def test_extension_array(self): # a = array([1, 3, np.nan, 2], dtype='Int64') a = array([1, 3, 2], dtype='Int64') result = safe_sort(a) # expected = array([1, 2, 3, np.nan], dtype='Int64') expected = array([1, 2, 3], dtype='Int64') tm.assert_extension_array_equal(result, expected)
def test_extension_array(self): # a = array([1, 3, np.nan, 2], dtype='Int64') a = array([1, 3, 2], dtype='Int64') result = safe_sort(a) # expected = array([1, 2, 3, np.nan], dtype='Int64') expected = array([1, 2, 3], dtype='Int64') tm.assert_extension_array_equal(result, expected)
def test_extension_array_labels(self, verify, na_sentinel): a = array([1, 3, 2], dtype='Int64') result, labels = safe_sort(a, [0, 1, na_sentinel, 2], na_sentinel=na_sentinel, verify=verify) expected_values = array([1, 2, 3], dtype='Int64') expected_labels = np.array([0, 2, na_sentinel, 1], dtype=np.intp) tm.assert_extension_array_equal(result, expected_values) tm.assert_numpy_array_equal(labels, expected_labels)
def test_pairwise_with_self(self, f): # DataFrame with itself, pairwise=True # note that we may construct the 1st level of the MI # in a non-monotonic way, so compare accordingly results = [] for i, df in enumerate(self.df1s): result = f(df) tm.assert_index_equal(result.index.levels[0], df.index, check_names=False) tm.assert_numpy_array_equal( safe_sort(result.index.levels[1]), safe_sort(df.columns.unique()) ) tm.assert_index_equal(result.columns, df.columns) results.append(df) for i, result in enumerate(results): if i > 0: self.compare(result, results[0])
def test_extension_array_labels(self, verify, na_sentinel): a = array([1, 3, 2], dtype='Int64') result, labels = safe_sort(a, [0, 1, na_sentinel, 2], na_sentinel=na_sentinel, verify=verify) expected_values = array([1, 2, 3], dtype='Int64') expected_labels = np.array([0, 2, na_sentinel, 1], dtype=np.intp) tm.assert_extension_array_equal(result, expected_values) tm.assert_numpy_array_equal(labels, expected_labels)
def test_labels_out_of_bound(self, na_sentinel): values = [3, 1, 2, 0, 4] expected = np.array([0, 1, 2, 3, 4]) # out of bound indices labels = [0, 101, 102, 2, 3, 0, 99, 4] result, result_labels = safe_sort(values, labels, na_sentinel=na_sentinel) expected_labels = np.array( [3, na_sentinel, na_sentinel, 2, 0, 3, na_sentinel, 4], dtype=np.intp ) tm.assert_numpy_array_equal(result, expected) tm.assert_numpy_array_equal(result_labels, expected_labels)
def test_labels_out_of_bound(self, na_sentinel): values = [3, 1, 2, 0, 4] expected = np.array([0, 1, 2, 3, 4]) # out of bound indices labels = [0, 101, 102, 2, 3, 0, 99, 4] result, result_labels = safe_sort( values, labels, na_sentinel=na_sentinel) expected_labels = np.array( [3, na_sentinel, na_sentinel, 2, 0, 3, na_sentinel, 4], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) tm.assert_numpy_array_equal(result_labels, expected_labels)
def test_labels(self, verify): values = [3, 1, 2, 0, 4] expected = np.array([0, 1, 2, 3, 4]) labels = [0, 1, 1, 2, 3, 0, -1, 4] result, result_labels = safe_sort(values, labels, verify=verify) expected_labels = np.array([3, 1, 1, 2, 0, 3, -1, 4], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) tm.assert_numpy_array_equal(result_labels, expected_labels) # na_sentinel labels = [0, 1, 1, 2, 3, 0, 99, 4] result, result_labels = safe_sort(values, labels, na_sentinel=99, verify=verify) expected_labels = np.array([3, 1, 1, 2, 0, 3, 99, 4], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) tm.assert_numpy_array_equal(result_labels, expected_labels) labels = [] result, result_labels = safe_sort(values, labels, verify=verify) expected_labels = np.array([], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) tm.assert_numpy_array_equal(result_labels, expected_labels)
def test_exceptions(self): with pytest.raises(TypeError, match="Only list-like objects are allowed"): safe_sort(values=1) with pytest.raises(TypeError, match="Only list-like objects or None"): safe_sort(values=[0, 1, 2], labels=1) with pytest.raises(ValueError, match="values should be unique"): safe_sort(values=[0, 1, 2, 1], labels=[0, 1])
def test_exceptions(self): with tm.assert_raises_regex(TypeError, "Only list-like objects are allowed"): safe_sort(values=1) with tm.assert_raises_regex(TypeError, "Only list-like objects or None"): safe_sort(values=[0, 1, 2], labels=1) with tm.assert_raises_regex(ValueError, "values should be unique"): safe_sort(values=[0, 1, 2, 1], labels=[0, 1])
def test_exceptions(self): with tm.assert_raises_regex(TypeError, "Only list-like objects are allowed"): safe_sort(values=1) with tm.assert_raises_regex(TypeError, "Only list-like objects or None"): safe_sort(values=[0, 1, 2], labels=1) with tm.assert_raises_regex(ValueError, "values should be unique"): safe_sort(values=[0, 1, 2, 1], labels=[0, 1])
def test_exceptions(self): with pytest.raises(TypeError, match="Only list-like objects are allowed"): safe_sort(values=1) with pytest.raises(TypeError, match="Only list-like objects or None"): safe_sort(values=[0, 1, 2], labels=1) with pytest.raises(ValueError, match="values should be unique"): safe_sort(values=[0, 1, 2, 1], labels=[0, 1])
def test_mixed_integer_from_list(self): values = ['b', 1, 0, 'a', 0, 'b'] result = safe_sort(values) expected = np.array([0, 0, 1, 'a', 'b', 'b'], dtype=object) tm.assert_numpy_array_equal(result, expected)
def test_mixed_integer_from_list(self): values = ['b', 1, 0, 'a', 0, 'b'] result = safe_sort(values) expected = np.array([0, 0, 1, 'a', 'b', 'b'], dtype=object) tm.assert_numpy_array_equal(result, expected)