mirror of
https://github.com/aykhans/AzSuicideDataVisualization.git
synced 2025-07-04 07:08:05 +00:00
first commit
This commit is contained in:
@ -0,0 +1,42 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from pandas import (
|
||||
Index,
|
||||
MultiIndex,
|
||||
)
|
||||
import pandas._testing as tm
|
||||
|
||||
|
||||
class TestIndexConstructor:
|
||||
# Tests for the Index constructor, specifically for cases that do
|
||||
# not return a subclass
|
||||
|
||||
@pytest.mark.parametrize("value", [1, np.int64(1)])
|
||||
def test_constructor_corner(self, value):
|
||||
# corner case
|
||||
msg = (
|
||||
r"Index\(\.\.\.\) must be called with a collection of some "
|
||||
f"kind, {value} was passed"
|
||||
)
|
||||
with pytest.raises(TypeError, match=msg):
|
||||
Index(value)
|
||||
|
||||
@pytest.mark.parametrize("index_vals", [[("A", 1), "B"], ["B", ("A", 1)]])
|
||||
def test_construction_list_mixed_tuples(self, index_vals):
|
||||
# see gh-10697: if we are constructing from a mixed list of tuples,
|
||||
# make sure that we are independent of the sorting order.
|
||||
index = Index(index_vals)
|
||||
assert isinstance(index, Index)
|
||||
assert not isinstance(index, MultiIndex)
|
||||
|
||||
def test_constructor_wrong_kwargs(self):
|
||||
# GH #19348
|
||||
with pytest.raises(TypeError, match="Unexpected keyword arguments {'foo'}"):
|
||||
with tm.assert_produces_warning(FutureWarning):
|
||||
Index([], foo="bar")
|
||||
|
||||
def test_constructor_cast(self):
|
||||
msg = "could not convert string to float"
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
Index(["a", "b", "c"], dtype=float)
|
@ -0,0 +1,148 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
import pandas._config.config as cf
|
||||
|
||||
from pandas import Index
|
||||
|
||||
|
||||
class TestIndexRendering:
|
||||
@pytest.mark.parametrize(
|
||||
"index,expected",
|
||||
[
|
||||
# ASCII
|
||||
# short
|
||||
(
|
||||
Index(["a", "bb", "ccc"]),
|
||||
"""Index(['a', 'bb', 'ccc'], dtype='object')""",
|
||||
),
|
||||
# multiple lines
|
||||
(
|
||||
Index(["a", "bb", "ccc"] * 10),
|
||||
"Index(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', "
|
||||
"'bb', 'ccc', 'a', 'bb', 'ccc',\n"
|
||||
" 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', "
|
||||
"'bb', 'ccc', 'a', 'bb', 'ccc',\n"
|
||||
" 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],\n"
|
||||
" dtype='object')",
|
||||
),
|
||||
# truncated
|
||||
(
|
||||
Index(["a", "bb", "ccc"] * 100),
|
||||
"Index(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a',\n"
|
||||
" ...\n"
|
||||
" 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],\n"
|
||||
" dtype='object', length=300)",
|
||||
),
|
||||
# Non-ASCII
|
||||
# short
|
||||
(
|
||||
Index(["あ", "いい", "ううう"]),
|
||||
"""Index(['あ', 'いい', 'ううう'], dtype='object')""",
|
||||
),
|
||||
# multiple lines
|
||||
(
|
||||
Index(["あ", "いい", "ううう"] * 10),
|
||||
(
|
||||
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "
|
||||
"'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',\n"
|
||||
" 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "
|
||||
"'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',\n"
|
||||
" 'あ', 'いい', 'ううう', 'あ', 'いい', "
|
||||
"'ううう'],\n"
|
||||
" dtype='object')"
|
||||
),
|
||||
),
|
||||
# truncated
|
||||
(
|
||||
Index(["あ", "いい", "ううう"] * 100),
|
||||
(
|
||||
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "
|
||||
"'あ', 'いい', 'ううう', 'あ',\n"
|
||||
" ...\n"
|
||||
" 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', "
|
||||
"'ううう', 'あ', 'いい', 'ううう'],\n"
|
||||
" dtype='object', length=300)"
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_string_index_repr(self, index, expected):
|
||||
result = repr(index)
|
||||
assert result == expected
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"index,expected",
|
||||
[
|
||||
# short
|
||||
(
|
||||
Index(["あ", "いい", "ううう"]),
|
||||
("Index(['あ', 'いい', 'ううう'], dtype='object')"),
|
||||
),
|
||||
# multiple lines
|
||||
(
|
||||
Index(["あ", "いい", "ううう"] * 10),
|
||||
(
|
||||
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', "
|
||||
"'ううう', 'あ', 'いい', 'ううう',\n"
|
||||
" 'あ', 'いい', 'ううう', 'あ', 'いい', "
|
||||
"'ううう', 'あ', 'いい', 'ううう',\n"
|
||||
" 'あ', 'いい', 'ううう', 'あ', 'いい', "
|
||||
"'ううう', 'あ', 'いい', 'ううう',\n"
|
||||
" 'あ', 'いい', 'ううう'],\n"
|
||||
" dtype='object')"
|
||||
""
|
||||
),
|
||||
),
|
||||
# truncated
|
||||
(
|
||||
Index(["あ", "いい", "ううう"] * 100),
|
||||
(
|
||||
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', "
|
||||
"'ううう', 'あ', 'いい', 'ううう',\n"
|
||||
" 'あ',\n"
|
||||
" ...\n"
|
||||
" 'ううう', 'あ', 'いい', 'ううう', 'あ', "
|
||||
"'いい', 'ううう', 'あ', 'いい',\n"
|
||||
" 'ううう'],\n"
|
||||
" dtype='object', length=300)"
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_string_index_repr_with_unicode_option(self, index, expected):
|
||||
# Enable Unicode option -----------------------------------------
|
||||
with cf.option_context("display.unicode.east_asian_width", True):
|
||||
result = repr(index)
|
||||
assert result == expected
|
||||
|
||||
def test_repr_summary(self):
|
||||
with cf.option_context("display.max_seq_items", 10):
|
||||
result = repr(Index(np.arange(1000)))
|
||||
assert len(result) < 200
|
||||
assert "..." in result
|
||||
|
||||
def test_summary_bug(self):
|
||||
# GH#3869
|
||||
ind = Index(["{other}%s", "~:{range}:0"], name="A")
|
||||
result = ind._summary()
|
||||
# shouldn't be formatted accidentally.
|
||||
assert "~:{range}:0" in result
|
||||
assert "{other}%s" in result
|
||||
|
||||
def test_index_repr_bool_nan(self):
|
||||
# GH32146
|
||||
arr = Index([True, False, np.nan], dtype=object)
|
||||
exp1 = arr.format()
|
||||
out1 = ["True", "False", "NaN"]
|
||||
assert out1 == exp1
|
||||
|
||||
exp2 = repr(arr)
|
||||
out2 = "Index([True, False, nan], dtype='object')"
|
||||
assert out2 == exp2
|
||||
|
||||
def test_format_different_scalar_lengths(self):
|
||||
# GH#35439
|
||||
idx = Index(["aaaaaaaaa", "b"])
|
||||
expected = ["aaaaaaaaa", "b"]
|
||||
assert idx.format() == expected
|
@ -0,0 +1,78 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
import pandas as pd
|
||||
from pandas import (
|
||||
Index,
|
||||
NaT,
|
||||
)
|
||||
import pandas._testing as tm
|
||||
|
||||
|
||||
class TestGetSliceBounds:
|
||||
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
|
||||
@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)])
|
||||
def test_get_slice_bounds_within(self, kind, side, expected):
|
||||
index = Index(list("abcdef"))
|
||||
with tm.assert_produces_warning(FutureWarning, match="'kind' argument"):
|
||||
result = index.get_slice_bound("e", kind=kind, side=side)
|
||||
assert result == expected
|
||||
|
||||
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
|
||||
@pytest.mark.parametrize("side", ["left", "right"])
|
||||
@pytest.mark.parametrize(
|
||||
"data, bound, expected", [(list("abcdef"), "x", 6), (list("bcdefg"), "a", 0)]
|
||||
)
|
||||
def test_get_slice_bounds_outside(self, kind, side, expected, data, bound):
|
||||
index = Index(data)
|
||||
with tm.assert_produces_warning(FutureWarning, match="'kind' argument"):
|
||||
result = index.get_slice_bound(bound, kind=kind, side=side)
|
||||
assert result == expected
|
||||
|
||||
def test_get_slice_bounds_invalid_side(self):
|
||||
with pytest.raises(ValueError, match="Invalid value for side kwarg"):
|
||||
Index([]).get_slice_bound("a", side="middle")
|
||||
|
||||
|
||||
class TestGetIndexerNonUnique:
|
||||
def test_get_indexer_non_unique_dtype_mismatch(self):
|
||||
# GH#25459
|
||||
indexes, missing = Index(["A", "B"]).get_indexer_non_unique(Index([0]))
|
||||
tm.assert_numpy_array_equal(np.array([-1], dtype=np.intp), indexes)
|
||||
tm.assert_numpy_array_equal(np.array([0], dtype=np.intp), missing)
|
||||
|
||||
|
||||
class TestGetLoc:
|
||||
@pytest.mark.slow # to_flat_index takes a while
|
||||
def test_get_loc_tuple_monotonic_above_size_cutoff(self):
|
||||
# Go through the libindex path for which using
|
||||
# _bin_search vs ndarray.searchsorted makes a difference
|
||||
|
||||
lev = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
dti = pd.date_range("2016-01-01", periods=100)
|
||||
|
||||
mi = pd.MultiIndex.from_product([lev, range(10**3), dti])
|
||||
oidx = mi.to_flat_index()
|
||||
|
||||
loc = len(oidx) // 2
|
||||
tup = oidx[loc]
|
||||
|
||||
res = oidx.get_loc(tup)
|
||||
assert res == loc
|
||||
|
||||
def test_get_loc_nan_object_dtype_nonmonotonic_nonunique(self):
|
||||
# case that goes through _maybe_get_bool_indexer
|
||||
idx = Index(["foo", np.nan, None, "foo", 1.0, None], dtype=object)
|
||||
|
||||
# we dont raise KeyError on nan
|
||||
res = idx.get_loc(np.nan)
|
||||
assert res == 1
|
||||
|
||||
# we only match on None, not on np.nan
|
||||
res = idx.get_loc(None)
|
||||
expected = np.array([False, False, True, False, False, True])
|
||||
tm.assert_numpy_array_equal(res, expected)
|
||||
|
||||
# we don't match at all on mismatched NA
|
||||
with pytest.raises(KeyError, match="NaT"):
|
||||
idx.get_loc(NaT)
|
@ -0,0 +1,11 @@
|
||||
from pandas import Index
|
||||
import pandas._testing as tm
|
||||
|
||||
|
||||
def test_pickle_preserves_object_dtype():
|
||||
# GH#43188, GH#43155 don't infer numeric dtype
|
||||
index = Index([1, 2, 3], dtype=object)
|
||||
|
||||
result = tm.round_trip_pickle(index)
|
||||
assert result.dtype == object
|
||||
tm.assert_index_equal(index, result)
|
@ -0,0 +1,86 @@
|
||||
"""
|
||||
Tests for ndarray-like method on the base Index class
|
||||
"""
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from pandas import Index
|
||||
import pandas._testing as tm
|
||||
|
||||
|
||||
class TestReshape:
|
||||
def test_repeat(self):
|
||||
repeats = 2
|
||||
index = Index([1, 2, 3])
|
||||
expected = Index([1, 1, 2, 2, 3, 3])
|
||||
|
||||
result = index.repeat(repeats)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_insert(self):
|
||||
|
||||
# GH 7256
|
||||
# validate neg/pos inserts
|
||||
result = Index(["b", "c", "d"])
|
||||
|
||||
# test 0th element
|
||||
tm.assert_index_equal(Index(["a", "b", "c", "d"]), result.insert(0, "a"))
|
||||
|
||||
# test Nth element that follows Python list behavior
|
||||
tm.assert_index_equal(Index(["b", "c", "e", "d"]), result.insert(-1, "e"))
|
||||
|
||||
# test loc +/- neq (0, -1)
|
||||
tm.assert_index_equal(result.insert(1, "z"), result.insert(-2, "z"))
|
||||
|
||||
# test empty
|
||||
null_index = Index([])
|
||||
tm.assert_index_equal(Index(["a"]), null_index.insert(0, "a"))
|
||||
|
||||
def test_insert_missing(self, nulls_fixture):
|
||||
# GH#22295
|
||||
# test there is no mangling of NA values
|
||||
expected = Index(["a", nulls_fixture, "b", "c"])
|
||||
result = Index(list("abc")).insert(1, nulls_fixture)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"val", [(1, 2), np.datetime64("2019-12-31"), np.timedelta64(1, "D")]
|
||||
)
|
||||
@pytest.mark.parametrize("loc", [-1, 2])
|
||||
def test_insert_datetime_into_object(self, loc, val):
|
||||
# GH#44509
|
||||
idx = Index(["1", "2", "3"])
|
||||
result = idx.insert(loc, val)
|
||||
expected = Index(["1", "2", val, "3"])
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert type(expected[2]) is type(val)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"pos,expected",
|
||||
[
|
||||
(0, Index(["b", "c", "d"], name="index")),
|
||||
(-1, Index(["a", "b", "c"], name="index")),
|
||||
],
|
||||
)
|
||||
def test_delete(self, pos, expected):
|
||||
index = Index(["a", "b", "c", "d"], name="index")
|
||||
result = index.delete(pos)
|
||||
tm.assert_index_equal(result, expected)
|
||||
assert result.name == expected.name
|
||||
|
||||
def test_delete_raises(self):
|
||||
index = Index(["a", "b", "c", "d"], name="index")
|
||||
msg = "index 5 is out of bounds for axis 0 with size 4"
|
||||
with pytest.raises(IndexError, match=msg):
|
||||
index.delete(5)
|
||||
|
||||
def test_append_multiple(self):
|
||||
index = Index(["a", "b", "c", "d", "e", "f"])
|
||||
|
||||
foos = [index[:2], index[2:4], index[4:]]
|
||||
result = foos[0].append(foos[1:])
|
||||
tm.assert_index_equal(result, index)
|
||||
|
||||
# empty
|
||||
result = index.append([])
|
||||
tm.assert_index_equal(result, index)
|
@ -0,0 +1,261 @@
|
||||
from datetime import datetime
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
import pandas as pd
|
||||
from pandas import (
|
||||
Index,
|
||||
Series,
|
||||
)
|
||||
import pandas._testing as tm
|
||||
from pandas.core.algorithms import safe_sort
|
||||
|
||||
|
||||
class TestIndexSetOps:
|
||||
@pytest.mark.parametrize(
|
||||
"method", ["union", "intersection", "difference", "symmetric_difference"]
|
||||
)
|
||||
def test_setops_disallow_true(self, method):
|
||||
idx1 = Index(["a", "b"])
|
||||
idx2 = Index(["b", "c"])
|
||||
|
||||
with pytest.raises(ValueError, match="The 'sort' keyword only takes"):
|
||||
getattr(idx1, method)(idx2, sort=True)
|
||||
|
||||
def test_setops_preserve_object_dtype(self):
|
||||
idx = Index([1, 2, 3], dtype=object)
|
||||
result = idx.intersection(idx[1:])
|
||||
expected = idx[1:]
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# if other is not monotonic increasing, intersection goes through
|
||||
# a different route
|
||||
result = idx.intersection(idx[1:][::-1])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
result = idx._union(idx[1:], sort=None)
|
||||
expected = idx
|
||||
tm.assert_numpy_array_equal(result, expected.values)
|
||||
|
||||
result = idx.union(idx[1:], sort=None)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# if other is not monotonic increasing, _union goes through
|
||||
# a different route
|
||||
result = idx._union(idx[1:][::-1], sort=None)
|
||||
tm.assert_numpy_array_equal(result, expected.values)
|
||||
|
||||
result = idx.union(idx[1:][::-1], sort=None)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_union_base(self):
|
||||
index = Index([0, "a", 1, "b", 2, "c"])
|
||||
first = index[3:]
|
||||
second = index[:5]
|
||||
|
||||
result = first.union(second)
|
||||
|
||||
expected = Index([0, 1, 2, "a", "b", "c"])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize("klass", [np.array, Series, list])
|
||||
def test_union_different_type_base(self, klass):
|
||||
# GH 10149
|
||||
index = Index([0, "a", 1, "b", 2, "c"])
|
||||
first = index[3:]
|
||||
second = index[:5]
|
||||
|
||||
result = first.union(klass(second.values))
|
||||
|
||||
assert tm.equalContents(result, index)
|
||||
|
||||
def test_union_sort_other_incomparable(self):
|
||||
# https://github.com/pandas-dev/pandas/issues/24959
|
||||
idx = Index([1, pd.Timestamp("2000")])
|
||||
# default (sort=None)
|
||||
with tm.assert_produces_warning(RuntimeWarning):
|
||||
result = idx.union(idx[:1])
|
||||
|
||||
tm.assert_index_equal(result, idx)
|
||||
|
||||
# sort=None
|
||||
with tm.assert_produces_warning(RuntimeWarning):
|
||||
result = idx.union(idx[:1], sort=None)
|
||||
tm.assert_index_equal(result, idx)
|
||||
|
||||
# sort=False
|
||||
result = idx.union(idx[:1], sort=False)
|
||||
tm.assert_index_equal(result, idx)
|
||||
|
||||
@pytest.mark.xfail(reason="GH#25151 need to decide on True behavior")
|
||||
def test_union_sort_other_incomparable_true(self):
|
||||
# TODO(GH#25151): decide on True behaviour
|
||||
# sort=True
|
||||
idx = Index([1, pd.Timestamp("2000")])
|
||||
with pytest.raises(TypeError, match=".*"):
|
||||
idx.union(idx[:1], sort=True)
|
||||
|
||||
@pytest.mark.xfail(reason="GH#25151 need to decide on True behavior")
|
||||
def test_intersection_equal_sort_true(self):
|
||||
# TODO(GH#25151): decide on True behaviour
|
||||
idx = Index(["c", "a", "b"])
|
||||
sorted_ = Index(["a", "b", "c"])
|
||||
tm.assert_index_equal(idx.intersection(idx, sort=True), sorted_)
|
||||
|
||||
def test_intersection_base(self, sort):
|
||||
# (same results for py2 and py3 but sortedness not tested elsewhere)
|
||||
index = Index([0, "a", 1, "b", 2, "c"])
|
||||
first = index[:5]
|
||||
second = index[:3]
|
||||
|
||||
expected = Index([0, 1, "a"]) if sort is None else Index([0, "a", 1])
|
||||
result = first.intersection(second, sort=sort)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize("klass", [np.array, Series, list])
|
||||
def test_intersection_different_type_base(self, klass, sort):
|
||||
# GH 10149
|
||||
index = Index([0, "a", 1, "b", 2, "c"])
|
||||
first = index[:5]
|
||||
second = index[:3]
|
||||
|
||||
result = first.intersection(klass(second.values), sort=sort)
|
||||
assert tm.equalContents(result, second)
|
||||
|
||||
def test_intersection_nosort(self):
|
||||
result = Index(["c", "b", "a"]).intersection(["b", "a"])
|
||||
expected = Index(["b", "a"])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_intersection_equal_sort(self):
|
||||
idx = Index(["c", "a", "b"])
|
||||
tm.assert_index_equal(idx.intersection(idx, sort=False), idx)
|
||||
tm.assert_index_equal(idx.intersection(idx, sort=None), idx)
|
||||
|
||||
def test_intersection_str_dates(self, sort):
|
||||
dt_dates = [datetime(2012, 2, 9), datetime(2012, 2, 22)]
|
||||
|
||||
i1 = Index(dt_dates, dtype=object)
|
||||
i2 = Index(["aa"], dtype=object)
|
||||
result = i2.intersection(i1, sort=sort)
|
||||
|
||||
assert len(result) == 0
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"index2,expected_arr",
|
||||
[(Index(["B", "D"]), ["B"]), (Index(["B", "D", "A"]), ["A", "B"])],
|
||||
)
|
||||
def test_intersection_non_monotonic_non_unique(self, index2, expected_arr, sort):
|
||||
# non-monotonic non-unique
|
||||
index1 = Index(["A", "B", "A", "C"])
|
||||
expected = Index(expected_arr, dtype="object")
|
||||
result = index1.intersection(index2, sort=sort)
|
||||
if sort is None:
|
||||
expected = expected.sort_values()
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_difference_base(self, sort):
|
||||
# (same results for py2 and py3 but sortedness not tested elsewhere)
|
||||
index = Index([0, "a", 1, "b", 2, "c"])
|
||||
first = index[:4]
|
||||
second = index[3:]
|
||||
|
||||
result = first.difference(second, sort)
|
||||
expected = Index([0, "a", 1])
|
||||
if sort is None:
|
||||
expected = Index(safe_sort(expected))
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_symmetric_difference(self):
|
||||
# (same results for py2 and py3 but sortedness not tested elsewhere)
|
||||
index = Index([0, "a", 1, "b", 2, "c"])
|
||||
first = index[:4]
|
||||
second = index[3:]
|
||||
|
||||
result = first.symmetric_difference(second)
|
||||
expected = Index([0, 1, 2, "a", "c"])
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"method,expected,sort",
|
||||
[
|
||||
(
|
||||
"intersection",
|
||||
np.array(
|
||||
[(1, "A"), (2, "A"), (1, "B"), (2, "B")],
|
||||
dtype=[("num", int), ("let", "a1")],
|
||||
),
|
||||
False,
|
||||
),
|
||||
(
|
||||
"intersection",
|
||||
np.array(
|
||||
[(1, "A"), (1, "B"), (2, "A"), (2, "B")],
|
||||
dtype=[("num", int), ("let", "a1")],
|
||||
),
|
||||
None,
|
||||
),
|
||||
(
|
||||
"union",
|
||||
np.array(
|
||||
[(1, "A"), (1, "B"), (1, "C"), (2, "A"), (2, "B"), (2, "C")],
|
||||
dtype=[("num", int), ("let", "a1")],
|
||||
),
|
||||
None,
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_tuple_union_bug(self, method, expected, sort):
|
||||
index1 = Index(
|
||||
np.array(
|
||||
[(1, "A"), (2, "A"), (1, "B"), (2, "B")],
|
||||
dtype=[("num", int), ("let", "a1")],
|
||||
)
|
||||
)
|
||||
index2 = Index(
|
||||
np.array(
|
||||
[(1, "A"), (2, "A"), (1, "B"), (2, "B"), (1, "C"), (2, "C")],
|
||||
dtype=[("num", int), ("let", "a1")],
|
||||
)
|
||||
)
|
||||
|
||||
result = getattr(index1, method)(index2, sort=sort)
|
||||
assert result.ndim == 1
|
||||
|
||||
expected = Index(expected)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@pytest.mark.parametrize("first_list", [["b", "a"], []])
|
||||
@pytest.mark.parametrize("second_list", [["a", "b"], []])
|
||||
@pytest.mark.parametrize(
|
||||
"first_name, second_name, expected_name",
|
||||
[("A", "B", None), (None, "B", None), ("A", None, None)],
|
||||
)
|
||||
def test_union_name_preservation(
|
||||
self, first_list, second_list, first_name, second_name, expected_name, sort
|
||||
):
|
||||
first = Index(first_list, name=first_name)
|
||||
second = Index(second_list, name=second_name)
|
||||
union = first.union(second, sort=sort)
|
||||
|
||||
vals = set(first_list).union(second_list)
|
||||
|
||||
if sort is None and len(first_list) > 0 and len(second_list) > 0:
|
||||
expected = Index(sorted(vals), name=expected_name)
|
||||
tm.assert_index_equal(union, expected)
|
||||
else:
|
||||
expected = Index(vals, name=expected_name)
|
||||
tm.equalContents(union, expected)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"diff_type, expected",
|
||||
[["difference", [1, "B"]], ["symmetric_difference", [1, 2, "B", "C"]]],
|
||||
)
|
||||
def test_difference_object_type(self, diff_type, expected):
|
||||
# GH 13432
|
||||
idx1 = Index([0, 1, "A", "B"])
|
||||
idx2 = Index([0, 2, "A", "C"])
|
||||
result = getattr(idx1, diff_type)(idx2)
|
||||
expected = Index(expected)
|
||||
tm.assert_index_equal(result, expected)
|
@ -0,0 +1,13 @@
|
||||
import numpy as np
|
||||
|
||||
from pandas import Index
|
||||
import pandas._testing as tm
|
||||
|
||||
|
||||
class TestWhere:
|
||||
def test_where_intlike_str_doesnt_cast_ints(self):
|
||||
idx = Index(range(3))
|
||||
mask = np.array([True, False, True])
|
||||
res = idx.where(mask, "2")
|
||||
expected = Index([0, "2", 2])
|
||||
tm.assert_index_equal(res, expected)
|
Reference in New Issue
Block a user