Updated script that can be controled by Nodejs web app
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
import pandas as pd
|
||||
from pandas import (
|
||||
Index,
|
||||
MultiIndex,
|
||||
Series,
|
||||
)
|
||||
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_cast(self):
|
||||
msg = "could not convert string to float"
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
Index(["a", "b", "c"], dtype=float)
|
||||
|
||||
@pytest.mark.parametrize("tuple_list", [[()], [(), ()]])
|
||||
def test_construct_empty_tuples(self, tuple_list):
|
||||
# GH #45608
|
||||
result = Index(tuple_list)
|
||||
expected = MultiIndex.from_tuples(tuple_list)
|
||||
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
def test_index_string_inference(self):
|
||||
# GH#54430
|
||||
pytest.importorskip("pyarrow")
|
||||
dtype = "string[pyarrow_numpy]"
|
||||
expected = Index(["a", "b"], dtype=dtype)
|
||||
with pd.option_context("future.infer_string", True):
|
||||
ser = Index(["a", "b"])
|
||||
tm.assert_index_equal(ser, expected)
|
||||
|
||||
expected = Index(["a", 1], dtype="object")
|
||||
with pd.option_context("future.infer_string", True):
|
||||
ser = Index(["a", 1])
|
||||
tm.assert_index_equal(ser, expected)
|
||||
|
||||
def test_inference_on_pandas_objects(self):
|
||||
# GH#56012
|
||||
idx = Index([pd.Timestamp("2019-12-31")], dtype=object)
|
||||
with tm.assert_produces_warning(FutureWarning, match="Dtype inference"):
|
||||
result = Index(idx)
|
||||
assert result.dtype != np.object_
|
||||
|
||||
ser = Series([pd.Timestamp("2019-12-31")], dtype=object)
|
||||
|
||||
with tm.assert_produces_warning(FutureWarning, match="Dtype inference"):
|
||||
result = Index(ser)
|
||||
assert result.dtype != np.object_
|
||||
|
||||
def test_constructor_not_read_only(self):
|
||||
# GH#57130
|
||||
ser = Series([1, 2], dtype=object)
|
||||
with pd.option_context("mode.copy_on_write", True):
|
||||
idx = Index(ser)
|
||||
assert idx._values.flags.writeable
|
@@ -0,0 +1,163 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from pandas._config import using_pyarrow_string_dtype
|
||||
import pandas._config.config as cf
|
||||
|
||||
from pandas import Index
|
||||
import pandas._testing as tm
|
||||
|
||||
|
||||
class TestIndexRendering:
|
||||
def test_repr_is_valid_construction_code(self):
|
||||
# for the case of Index, where the repr is traditional rather than
|
||||
# stylized
|
||||
idx = Index(["a", "b"])
|
||||
res = eval(repr(idx))
|
||||
tm.assert_index_equal(res, idx)
|
||||
|
||||
@pytest.mark.xfail(using_pyarrow_string_dtype(), reason="repr different")
|
||||
@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.xfail(using_pyarrow_string_dtype(), reason="repr different")
|
||||
@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)
|
||||
msg = "Index.format is deprecated"
|
||||
with tm.assert_produces_warning(FutureWarning, match=msg):
|
||||
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"]
|
||||
msg = r"Index\.format is deprecated"
|
||||
with tm.assert_produces_warning(FutureWarning, match=msg):
|
||||
assert idx.format() == expected
|
@@ -0,0 +1,104 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from pandas._libs import index as libindex
|
||||
|
||||
import pandas as pd
|
||||
from pandas import (
|
||||
Index,
|
||||
NaT,
|
||||
)
|
||||
import pandas._testing as tm
|
||||
|
||||
|
||||
class TestGetSliceBounds:
|
||||
@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)])
|
||||
def test_get_slice_bounds_within(self, side, expected):
|
||||
index = Index(list("abcdef"))
|
||||
result = index.get_slice_bound("e", side=side)
|
||||
assert result == expected
|
||||
|
||||
@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, side, expected, data, bound):
|
||||
index = Index(data)
|
||||
result = index.get_slice_bound(bound, 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)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"idx_values,idx_non_unique",
|
||||
[
|
||||
([np.nan, 100, 200, 100], [np.nan, 100]),
|
||||
([np.nan, 100.0, 200.0, 100.0], [np.nan, 100.0]),
|
||||
],
|
||||
)
|
||||
def test_get_indexer_non_unique_int_index(self, idx_values, idx_non_unique):
|
||||
indexes, missing = Index(idx_values).get_indexer_non_unique(Index([np.nan]))
|
||||
tm.assert_numpy_array_equal(np.array([0], dtype=np.intp), indexes)
|
||||
tm.assert_numpy_array_equal(np.array([], dtype=np.intp), missing)
|
||||
|
||||
indexes, missing = Index(idx_values).get_indexer_non_unique(
|
||||
Index(idx_non_unique)
|
||||
)
|
||||
tm.assert_numpy_array_equal(np.array([0, 1, 3], dtype=np.intp), indexes)
|
||||
tm.assert_numpy_array_equal(np.array([], 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, monkeypatch):
|
||||
# Go through the libindex path for which using
|
||||
# _bin_search vs ndarray.searchsorted makes a difference
|
||||
|
||||
with monkeypatch.context():
|
||||
monkeypatch.setattr(libindex, "_SIZE_CUTOFF", 100)
|
||||
lev = list("ABCD")
|
||||
dti = pd.date_range("2016-01-01", periods=10)
|
||||
|
||||
mi = pd.MultiIndex.from_product([lev, range(5), 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)
|
||||
|
||||
|
||||
def test_getitem_boolean_ea_indexer():
|
||||
# GH#45806
|
||||
ser = pd.Series([True, False, pd.NA], dtype="boolean")
|
||||
result = ser.index[ser]
|
||||
expected = Index([0])
|
||||
tm.assert_index_equal(result, expected)
|
@@ -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,95 @@
|
||||
"""
|
||||
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"], dtype=object), null_index.insert(0, "a"))
|
||||
|
||||
def test_insert_missing(self, nulls_fixture, using_infer_string):
|
||||
# GH#22295
|
||||
# test there is no mangling of NA values
|
||||
expected = Index(["a", nulls_fixture, "b", "c"], dtype=object)
|
||||
result = Index(list("abc"), dtype=object).insert(
|
||||
1, Index([nulls_fixture], dtype=object)
|
||||
)
|
||||
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)
|
||||
|
||||
def test_insert_none_into_string_numpy(self):
|
||||
# GH#55365
|
||||
pytest.importorskip("pyarrow")
|
||||
index = Index(["a", "b", "c"], dtype="string[pyarrow_numpy]")
|
||||
result = index.insert(-1, None)
|
||||
expected = Index(["a", "b", None, "c"], dtype="string[pyarrow_numpy]")
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
@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,266 @@
|
||||
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
|
||||
|
||||
|
||||
def equal_contents(arr1, arr2) -> bool:
|
||||
"""
|
||||
Checks if the set of unique elements of arr1 and arr2 are equivalent.
|
||||
"""
|
||||
return frozenset(arr1) == frozenset(arr2)
|
||||
|
||||
|
||||
class TestIndexSetOps:
|
||||
@pytest.mark.parametrize(
|
||||
"method", ["union", "intersection", "difference", "symmetric_difference"]
|
||||
)
|
||||
def test_setops_sort_validation(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=2)
|
||||
|
||||
# sort=True is supported as of GH#??
|
||||
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 equal_contents(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)
|
||||
|
||||
def test_union_sort_other_incomparable_true(self):
|
||||
idx = Index([1, pd.Timestamp("2000")])
|
||||
with pytest.raises(TypeError, match=".*"):
|
||||
idx.union(idx[:1], sort=True)
|
||||
|
||||
def test_intersection_equal_sort_true(self):
|
||||
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 equal_contents(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)
|
||||
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", "S1")],
|
||||
),
|
||||
False,
|
||||
),
|
||||
(
|
||||
"intersection",
|
||||
np.array(
|
||||
[(1, "A"), (1, "B"), (2, "A"), (2, "B")],
|
||||
dtype=[("num", int), ("let", "S1")],
|
||||
),
|
||||
None,
|
||||
),
|
||||
(
|
||||
"union",
|
||||
np.array(
|
||||
[(1, "A"), (1, "B"), (1, "C"), (2, "A"), (2, "B"), (2, "C")],
|
||||
dtype=[("num", int), ("let", "S1")],
|
||||
),
|
||||
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", "S1")],
|
||||
)
|
||||
)
|
||||
index2 = Index(
|
||||
np.array(
|
||||
[(1, "A"), (2, "A"), (1, "B"), (2, "B"), (1, "C"), (2, "C")],
|
||||
dtype=[("num", int), ("let", "S1")],
|
||||
)
|
||||
)
|
||||
|
||||
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.assert_index_equal(union.sort_values(), expected.sort_values())
|
||||
|
||||
@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