How to use index method in devicefarmer-stf

Best JavaScript code snippet using devicefarmer-stf

test_base.py

Source:test_base.py Github

copy

Full Screen

...57 tuples=MultiIndex.from_tuples(zip(["foo", "bar", "baz"], [1, 2, 3])),58 repeats=Index([0, 0, 1, 1, 2, 2]),59 )60 self.setup_indices()61 def create_index(self):62 return Index(list("abcde"))63 def generate_index_types(self, skip_index_keys=[]):64 """65 Return a generator of the various index types, leaving66 out the ones with a key in skip_index_keys67 """68 for key, index in self.indices.items():69 if key not in skip_index_keys:70 yield key, index71 def test_can_hold_identifiers(self):72 index = self.create_index()73 key = index[0]74 assert index._can_hold_identifiers_and_holds_name(key) is True75 def test_new_axis(self):76 new_index = self.dateIndex[None, :]77 assert new_index.ndim == 278 assert isinstance(new_index, np.ndarray)79 def test_copy_and_deepcopy(self):80 new_copy2 = self.intIndex.copy(dtype=int)81 assert new_copy2.dtype.kind == "i"82 @pytest.mark.parametrize("attr", ["strIndex", "dateIndex"])83 def test_constructor_regular(self, attr):84 # regular instance creation85 index = getattr(self, attr)86 tm.assert_contains_all(index, index)87 def test_constructor_casting(self):88 # casting89 arr = np.array(self.strIndex)90 index = Index(arr)91 tm.assert_contains_all(arr, index)92 tm.assert_index_equal(self.strIndex, index)93 def test_constructor_copy(self):94 # copy95 arr = np.array(self.strIndex)96 index = Index(arr, copy=True, name="name")97 assert isinstance(index, Index)98 assert index.name == "name"99 tm.assert_numpy_array_equal(arr, index.values)100 arr[0] = "SOMEBIGLONGSTRING"101 assert index[0] != "SOMEBIGLONGSTRING"102 # what to do here?103 # arr = np.array(5.)104 # pytest.raises(Exception, arr.view, Index)105 def test_constructor_corner(self):106 # corner case107 msg = (108 r"Index\(\.\.\.\) must be called with a collection of some"109 " kind, 0 was passed"110 )111 with pytest.raises(TypeError, match=msg):112 Index(0)113 @pytest.mark.parametrize("index_vals", [[("A", 1), "B"], ["B", ("A", 1)]])114 def test_construction_list_mixed_tuples(self, index_vals):115 # see gh-10697: if we are constructing from a mixed list of tuples,116 # make sure that we are independent of the sorting order.117 index = Index(index_vals)118 assert isinstance(index, Index)119 assert not isinstance(index, MultiIndex)120 @pytest.mark.parametrize("na_value", [None, np.nan])121 @pytest.mark.parametrize("vtype", [list, tuple, iter])122 def test_construction_list_tuples_nan(self, na_value, vtype):123 # GH 18505 : valid tuples containing NaN124 values = [(1, "two"), (3.0, na_value)]125 result = Index(vtype(values))126 expected = MultiIndex.from_tuples(values)127 tm.assert_index_equal(result, expected)128 @pytest.mark.parametrize("cast_as_obj", [True, False])129 @pytest.mark.parametrize(130 "index",131 [132 pd.date_range(133 "2015-01-01 10:00",134 freq="D",135 periods=3,136 tz="US/Eastern",137 name="Green Eggs & Ham",138 ), # DTI with tz139 pd.date_range("2015-01-01 10:00", freq="D", periods=3), # DTI no tz140 pd.timedelta_range("1 days", freq="D", periods=3), # td141 pd.period_range("2015-01-01", freq="D", periods=3), # period142 ],143 )144 def test_constructor_from_index_dtlike(self, cast_as_obj, index):145 if cast_as_obj:146 result = pd.Index(index.astype(object))147 else:148 result = pd.Index(index)149 tm.assert_index_equal(result, index)150 if isinstance(index, pd.DatetimeIndex):151 assert result.tz == index.tz152 if cast_as_obj:153 # GH#23524 check that Index(dti, dtype=object) does not154 # incorrectly raise ValueError, and that nanoseconds are not155 # dropped156 index += pd.Timedelta(nanoseconds=50)157 result = pd.Index(index, dtype=object)158 assert result.dtype == np.object_159 assert list(result) == list(index)160 @pytest.mark.parametrize(161 "index,has_tz",162 [163 (164 pd.date_range("2015-01-01 10:00", freq="D", periods=3, tz="US/Eastern"),165 True,166 ), # datetimetz167 (pd.timedelta_range("1 days", freq="D", periods=3), False), # td168 (pd.period_range("2015-01-01", freq="D", periods=3), False), # period169 ],170 )171 def test_constructor_from_series_dtlike(self, index, has_tz):172 result = pd.Index(pd.Series(index))173 tm.assert_index_equal(result, index)174 if has_tz:175 assert result.tz == index.tz176 @pytest.mark.parametrize("klass", [Index, DatetimeIndex])177 def test_constructor_from_series(self, klass):178 expected = DatetimeIndex(179 [Timestamp("20110101"), Timestamp("20120101"), Timestamp("20130101")]180 )181 s = Series(182 [Timestamp("20110101"), Timestamp("20120101"), Timestamp("20130101")]183 )184 result = klass(s)185 tm.assert_index_equal(result, expected)186 def test_constructor_from_series_freq(self):187 # GH 6273188 # create from a series, passing a freq189 dts = ["1-1-1990", "2-1-1990", "3-1-1990", "4-1-1990", "5-1-1990"]190 expected = DatetimeIndex(dts, freq="MS")191 s = Series(pd.to_datetime(dts))192 result = DatetimeIndex(s, freq="MS")193 tm.assert_index_equal(result, expected)194 def test_constructor_from_frame_series_freq(self):195 # GH 6273196 # create from a series, passing a freq197 dts = ["1-1-1990", "2-1-1990", "3-1-1990", "4-1-1990", "5-1-1990"]198 expected = DatetimeIndex(dts, freq="MS")199 df = pd.DataFrame(np.random.rand(5, 3))200 df["date"] = dts201 result = DatetimeIndex(df["date"], freq="MS")202 assert df["date"].dtype == object203 expected.name = "date"204 tm.assert_index_equal(result, expected)205 expected = pd.Series(dts, name="date")206 tm.assert_series_equal(df["date"], expected)207 # GH 6274208 # infer freq of same209 freq = pd.infer_freq(df["date"])210 assert freq == "MS"211 @pytest.mark.parametrize(212 "array",213 [214 np.arange(5),215 np.array(["a", "b", "c"]),216 date_range("2000-01-01", periods=3).values,217 ],218 )219 def test_constructor_ndarray_like(self, array):220 # GH 5460#issuecomment-44474502221 # it should be possible to convert any object that satisfies the numpy222 # ndarray interface directly into an Index223 class ArrayLike:224 def __init__(self, array):225 self.array = array226 def __array__(self, dtype=None):227 return self.array228 expected = pd.Index(array)229 result = pd.Index(ArrayLike(array))230 tm.assert_index_equal(result, expected)231 @pytest.mark.parametrize(232 "dtype",233 [int, "int64", "int32", "int16", "int8", "uint64", "uint32", "uint16", "uint8"],234 )235 def test_constructor_int_dtype_float(self, dtype):236 # GH 18400237 if is_unsigned_integer_dtype(dtype):238 index_type = UInt64Index239 else:240 index_type = Int64Index241 expected = index_type([0, 1, 2, 3])242 result = Index([0.0, 1.0, 2.0, 3.0], dtype=dtype)243 tm.assert_index_equal(result, expected)244 def test_constructor_int_dtype_nan(self):245 # see gh-15187246 data = [np.nan]247 expected = Float64Index(data)248 result = Index(data, dtype="float")249 tm.assert_index_equal(result, expected)250 @pytest.mark.parametrize("dtype", ["int64", "uint64"])251 def test_constructor_int_dtype_nan_raises(self, dtype):252 # see gh-15187253 data = [np.nan]254 msg = "cannot convert"255 with pytest.raises(ValueError, match=msg):256 Index(data, dtype=dtype)257 def test_constructor_no_pandas_array(self):258 ser = pd.Series([1, 2, 3])259 result = pd.Index(ser.array)260 expected = pd.Index([1, 2, 3])261 tm.assert_index_equal(result, expected)262 @pytest.mark.parametrize(263 "klass,dtype,na_val",264 [265 (pd.Float64Index, np.float64, np.nan),266 (pd.DatetimeIndex, "datetime64[ns]", pd.NaT),267 ],268 )269 def test_index_ctor_infer_nan_nat(self, klass, dtype, na_val):270 # GH 13467271 na_list = [na_val, na_val]272 expected = klass(na_list)273 assert expected.dtype == dtype274 result = Index(na_list)275 tm.assert_index_equal(result, expected)276 result = Index(np.array(na_list))277 tm.assert_index_equal(result, expected)278 @pytest.mark.parametrize("pos", [0, 1])279 @pytest.mark.parametrize(280 "klass,dtype,ctor",281 [282 (pd.DatetimeIndex, "datetime64[ns]", np.datetime64("nat")),283 (pd.TimedeltaIndex, "timedelta64[ns]", np.timedelta64("nat")),284 ],285 )286 def test_index_ctor_infer_nat_dt_like(self, pos, klass, dtype, ctor, nulls_fixture):287 expected = klass([pd.NaT, pd.NaT])288 assert expected.dtype == dtype289 data = [ctor]290 data.insert(pos, nulls_fixture)291 result = Index(data)292 tm.assert_index_equal(result, expected)293 result = Index(np.array(data, dtype=object))294 tm.assert_index_equal(result, expected)295 @pytest.mark.parametrize("swap_objs", [True, False])296 def test_index_ctor_nat_result(self, swap_objs):297 # mixed np.datetime64/timedelta64 nat results in object298 data = [np.datetime64("nat"), np.timedelta64("nat")]299 if swap_objs:300 data = data[::-1]301 expected = pd.Index(data, dtype=object)302 tm.assert_index_equal(Index(data), expected)303 tm.assert_index_equal(Index(np.array(data, dtype=object)), expected)304 def test_index_ctor_infer_periodindex(self):305 xp = period_range("2012-1-1", freq="M", periods=3)306 rs = Index(xp)307 tm.assert_index_equal(rs, xp)308 assert isinstance(rs, PeriodIndex)309 @pytest.mark.parametrize(310 "vals,dtype",311 [312 ([1, 2, 3, 4, 5], "int"),313 ([1.1, np.nan, 2.2, 3.0], "float"),314 (["A", "B", "C", np.nan], "obj"),315 ],316 )317 def test_constructor_simple_new(self, vals, dtype):318 index = Index(vals, name=dtype)319 result = index._simple_new(index.values, dtype)320 tm.assert_index_equal(result, index)321 @pytest.mark.parametrize(322 "vals",323 [324 [1, 2, 3],325 np.array([1, 2, 3]),326 np.array([1, 2, 3], dtype=int),327 # below should coerce328 [1.0, 2.0, 3.0],329 np.array([1.0, 2.0, 3.0], dtype=float),330 ],331 )332 def test_constructor_dtypes_to_int64(self, vals):333 index = Index(vals, dtype=int)334 assert isinstance(index, Int64Index)335 @pytest.mark.parametrize(336 "vals",337 [338 [1, 2, 3],339 [1.0, 2.0, 3.0],340 np.array([1.0, 2.0, 3.0]),341 np.array([1, 2, 3], dtype=int),342 np.array([1.0, 2.0, 3.0], dtype=float),343 ],344 )345 def test_constructor_dtypes_to_float64(self, vals):346 index = Index(vals, dtype=float)347 assert isinstance(index, Float64Index)348 @pytest.mark.parametrize("cast_index", [True, False])349 @pytest.mark.parametrize(350 "vals", [[True, False, True], np.array([True, False, True], dtype=bool)]351 )352 def test_constructor_dtypes_to_object(self, cast_index, vals):353 if cast_index:354 index = Index(vals, dtype=bool)355 else:356 index = Index(vals)357 assert isinstance(index, Index)358 assert index.dtype == object359 @pytest.mark.parametrize(360 "vals",361 [362 [1, 2, 3],363 np.array([1, 2, 3], dtype=int),364 np.array(365 [np_datetime64_compat("2011-01-01"), np_datetime64_compat("2011-01-02")]366 ),367 [datetime(2011, 1, 1), datetime(2011, 1, 2)],368 ],369 )370 def test_constructor_dtypes_to_categorical(self, vals):371 index = Index(vals, dtype="category")372 assert isinstance(index, CategoricalIndex)373 @pytest.mark.parametrize("cast_index", [True, False])374 @pytest.mark.parametrize(375 "vals",376 [377 Index(378 np.array(379 [380 np_datetime64_compat("2011-01-01"),381 np_datetime64_compat("2011-01-02"),382 ]383 )384 ),385 Index([datetime(2011, 1, 1), datetime(2011, 1, 2)]),386 ],387 )388 def test_constructor_dtypes_to_datetime(self, cast_index, vals):389 if cast_index:390 index = Index(vals, dtype=object)391 assert isinstance(index, Index)392 assert index.dtype == object393 else:394 index = Index(vals)395 assert isinstance(index, DatetimeIndex)396 @pytest.mark.parametrize("cast_index", [True, False])397 @pytest.mark.parametrize(398 "vals",399 [400 np.array([np.timedelta64(1, "D"), np.timedelta64(1, "D")]),401 [timedelta(1), timedelta(1)],402 ],403 )404 def test_constructor_dtypes_to_timedelta(self, cast_index, vals):405 if cast_index:406 index = Index(vals, dtype=object)407 assert isinstance(index, Index)408 assert index.dtype == object409 else:410 index = Index(vals)411 assert isinstance(index, TimedeltaIndex)412 @pytest.mark.parametrize("attr, utc", [["values", False], ["asi8", True]])413 @pytest.mark.parametrize("klass", [pd.Index, pd.DatetimeIndex])414 def test_constructor_dtypes_datetime(self, tz_naive_fixture, attr, utc, klass):415 # Test constructing with a datetimetz dtype416 # .values produces numpy datetimes, so these are considered naive417 # .asi8 produces integers, so these are considered epoch timestamps418 # ^the above will be true in a later version. Right now we `.view`419 # the i8 values as NS_DTYPE, effectively treating them as wall times.420 index = pd.date_range("2011-01-01", periods=5)421 arg = getattr(index, attr)422 index = index.tz_localize(tz_naive_fixture)423 dtype = index.dtype424 if (425 tz_naive_fixture426 and attr == "asi8"427 and str(tz_naive_fixture) not in ("UTC", "tzutc()", "UTC+00:00")428 ):429 ex_warn = FutureWarning430 else:431 ex_warn = None432 # stacklevel is checked elsewhere. We don't do it here since433 # Index will have an frame, throwing off the expected.434 with tm.assert_produces_warning(ex_warn, check_stacklevel=False):435 result = klass(arg, tz=tz_naive_fixture)436 tm.assert_index_equal(result, index)437 with tm.assert_produces_warning(ex_warn, check_stacklevel=False):438 result = klass(arg, dtype=dtype)439 tm.assert_index_equal(result, index)440 with tm.assert_produces_warning(ex_warn, check_stacklevel=False):441 result = klass(list(arg), tz=tz_naive_fixture)442 tm.assert_index_equal(result, index)443 with tm.assert_produces_warning(ex_warn, check_stacklevel=False):444 result = klass(list(arg), dtype=dtype)445 tm.assert_index_equal(result, index)446 @pytest.mark.parametrize("attr", ["values", "asi8"])447 @pytest.mark.parametrize("klass", [pd.Index, pd.TimedeltaIndex])448 def test_constructor_dtypes_timedelta(self, attr, klass):449 index = pd.timedelta_range("1 days", periods=5)450 dtype = index.dtype451 values = getattr(index, attr)452 result = klass(values, dtype=dtype)453 tm.assert_index_equal(result, index)454 result = klass(list(values), dtype=dtype)455 tm.assert_index_equal(result, index)456 @pytest.mark.parametrize("value", [[], iter([]), (x for x in [])])457 @pytest.mark.parametrize(458 "klass",459 [460 Index,461 Float64Index,462 Int64Index,463 UInt64Index,464 CategoricalIndex,465 DatetimeIndex,466 TimedeltaIndex,467 ],468 )469 def test_constructor_empty(self, value, klass):470 empty = klass(value)471 assert isinstance(empty, klass)472 assert not len(empty)473 @pytest.mark.parametrize(474 "empty,klass",475 [476 (PeriodIndex([], freq="B"), PeriodIndex),477 (PeriodIndex(iter([]), freq="B"), PeriodIndex),478 (PeriodIndex((x for x in []), freq="B"), PeriodIndex),479 (RangeIndex(step=1), pd.RangeIndex),480 (MultiIndex(levels=[[1, 2], ["blue", "red"]], codes=[[], []]), MultiIndex),481 ],482 )483 def test_constructor_empty_special(self, empty, klass):484 assert isinstance(empty, klass)485 assert not len(empty)486 def test_constructor_overflow_int64(self):487 # see gh-15832488 msg = (489 "The elements provided in the data cannot "490 "all be casted to the dtype int64"491 )492 with pytest.raises(OverflowError, match=msg):493 Index([np.iinfo(np.uint64).max - 1], dtype="int64")494 @pytest.mark.xfail(reason="see GH#21311: Index doesn't enforce dtype argument")495 def test_constructor_cast(self):496 msg = "could not convert string to float"497 with pytest.raises(ValueError, match=msg):498 Index(["a", "b", "c"], dtype=float)499 def test_view_with_args(self):500 restricted = ["unicodeIndex", "strIndex", "catIndex", "boolIndex", "empty"]501 for i in list(set(self.indices.keys()) - set(restricted)):502 ind = self.indices[i]503 ind.view("i8")504 @pytest.mark.parametrize(505 "index_type",506 [507 "unicodeIndex",508 "strIndex",509 pytest.param("catIndex", marks=pytest.mark.xfail(reason="gh-25464")),510 "boolIndex",511 "empty",512 ],513 )514 def test_view_with_args_object_array_raises(self, index_type):515 ind = self.indices[index_type]516 msg = "Cannot change data-type for object array"517 with pytest.raises(TypeError, match=msg):518 ind.view("i8")519 def test_astype(self):520 casted = self.intIndex.astype("i8")521 # it works!522 casted.get_loc(5)523 # pass on name524 self.intIndex.name = "foobar"525 casted = self.intIndex.astype("i8")526 assert casted.name == "foobar"527 def test_equals_object(self):528 # same529 assert Index(["a", "b", "c"]).equals(Index(["a", "b", "c"]))530 @pytest.mark.parametrize(531 "comp", [Index(["a", "b"]), Index(["a", "b", "d"]), ["a", "b", "c"]]532 )533 def test_not_equals_object(self, comp):534 assert not Index(["a", "b", "c"]).equals(comp)535 def test_insert(self):536 # GH 7256537 # validate neg/pos inserts538 result = Index(["b", "c", "d"])539 # test 0th element540 tm.assert_index_equal(Index(["a", "b", "c", "d"]), result.insert(0, "a"))541 # test Nth element that follows Python list behavior542 tm.assert_index_equal(Index(["b", "c", "e", "d"]), result.insert(-1, "e"))543 # test loc +/- neq (0, -1)544 tm.assert_index_equal(result.insert(1, "z"), result.insert(-2, "z"))545 # test empty546 null_index = Index([])547 tm.assert_index_equal(Index(["a"]), null_index.insert(0, "a"))548 def test_insert_missing(self, nulls_fixture):549 # GH 22295550 # test there is no mangling of NA values551 expected = Index(["a", nulls_fixture, "b", "c"])552 result = Index(list("abc")).insert(1, nulls_fixture)553 tm.assert_index_equal(result, expected)554 @pytest.mark.parametrize(555 "pos,expected",556 [557 (0, Index(["b", "c", "d"], name="index")),558 (-1, Index(["a", "b", "c"], name="index")),559 ],560 )561 def test_delete(self, pos, expected):562 index = Index(["a", "b", "c", "d"], name="index")563 result = index.delete(pos)564 tm.assert_index_equal(result, expected)565 assert result.name == expected.name566 def test_delete_raises(self):567 index = Index(["a", "b", "c", "d"], name="index")568 msg = "index 5 is out of bounds for axis 0 with size 4"569 with pytest.raises(IndexError, match=msg):570 index.delete(5)571 def test_identical(self):572 # index573 i1 = Index(["a", "b", "c"])574 i2 = Index(["a", "b", "c"])575 assert i1.identical(i2)576 i1 = i1.rename("foo")577 assert i1.equals(i2)578 assert not i1.identical(i2)579 i2 = i2.rename("foo")580 assert i1.identical(i2)581 i3 = Index([("a", "a"), ("a", "b"), ("b", "a")])582 i4 = Index([("a", "a"), ("a", "b"), ("b", "a")], tupleize_cols=False)583 assert not i3.identical(i4)584 def test_is_(self):585 ind = Index(range(10))586 assert ind.is_(ind)587 assert ind.is_(ind.view().view().view().view())588 assert not ind.is_(Index(range(10)))589 assert not ind.is_(ind.copy())590 assert not ind.is_(ind.copy(deep=False))591 assert not ind.is_(ind[:])592 assert not ind.is_(np.array(range(10)))593 # quasi-implementation dependent594 assert ind.is_(ind.view())595 ind2 = ind.view()596 ind2.name = "bob"597 assert ind.is_(ind2)598 assert ind2.is_(ind)599 # doesn't matter if Indices are *actually* views of underlying data,600 assert not ind.is_(Index(ind.values))601 arr = np.array(range(1, 11))602 ind1 = Index(arr, copy=False)603 ind2 = Index(arr, copy=False)604 assert not ind1.is_(ind2)605 def test_asof(self):606 d = self.dateIndex[0]607 assert self.dateIndex.asof(d) == d608 assert isna(self.dateIndex.asof(d - timedelta(1)))609 d = self.dateIndex[-1]610 assert self.dateIndex.asof(d + timedelta(1)) == d611 d = self.dateIndex[0].to_pydatetime()612 assert isinstance(self.dateIndex.asof(d), Timestamp)613 def test_asof_datetime_partial(self):614 index = pd.date_range("2010-01-01", periods=2, freq="m")615 expected = Timestamp("2010-02-28")616 result = index.asof("2010-02")617 assert result == expected618 assert not isinstance(result, Index)619 def test_nanosecond_index_access(self):620 s = Series([Timestamp("20130101")]).values.view("i8")[0]621 r = DatetimeIndex([s + 50 + i for i in range(100)])622 x = Series(np.random.randn(100), index=r)623 first_value = x.asof(x.index[0])624 # this does not yet work, as parsing strings is done via dateutil625 # assert first_value == x['2013-01-01 00:00:00.000000050+0000']626 expected_ts = np_datetime64_compat("2013-01-01 00:00:00.000000050+0000", "ns")627 assert first_value == x[Timestamp(expected_ts)]628 def test_booleanindex(self):629 boolIndex = np.repeat(True, len(self.strIndex)).astype(bool)630 boolIndex[5:30:2] = False631 subIndex = self.strIndex[boolIndex]632 for i, val in enumerate(subIndex):633 assert subIndex.get_loc(val) == i634 subIndex = self.strIndex[list(boolIndex)]635 for i, val in enumerate(subIndex):636 assert subIndex.get_loc(val) == i637 def test_fancy(self):638 sl = self.strIndex[[1, 2, 3]]639 for i in sl:640 assert i == sl[sl.get_loc(i)]641 @pytest.mark.parametrize("attr", ["strIndex", "intIndex", "floatIndex"])642 @pytest.mark.parametrize("dtype", [np.int_, np.bool_])643 def test_empty_fancy(self, attr, dtype):644 empty_arr = np.array([], dtype=dtype)645 index = getattr(self, attr)646 empty_index = index.__class__([])647 assert index[[]].identical(empty_index)648 assert index[empty_arr].identical(empty_index)649 @pytest.mark.parametrize("attr", ["strIndex", "intIndex", "floatIndex"])650 def test_empty_fancy_raises(self, attr):651 # pd.DatetimeIndex is excluded, because it overrides getitem and should652 # be tested separately.653 empty_farr = np.array([], dtype=np.float_)654 index = getattr(self, attr)655 empty_index = index.__class__([])656 assert index[[]].identical(empty_index)657 # np.ndarray only accepts ndarray of int & bool dtypes, so should Index658 msg = r"arrays used as indices must be of integer \(or boolean\) type"659 with pytest.raises(IndexError, match=msg):660 index[empty_farr]661 @pytest.mark.parametrize("sort", [None, False])662 def test_intersection(self, sort):663 first = self.strIndex[:20]664 second = self.strIndex[:10]665 intersect = first.intersection(second, sort=sort)666 if sort is None:667 tm.assert_index_equal(intersect, second.sort_values())668 assert tm.equalContents(intersect, second)669 # Corner cases670 inter = first.intersection(first, sort=sort)671 assert inter is first672 @pytest.mark.parametrize(673 "index2,keeps_name",674 [675 (Index([3, 4, 5, 6, 7], name="index"), True), # preserve same name676 (Index([3, 4, 5, 6, 7], name="other"), False), # drop diff names677 (Index([3, 4, 5, 6, 7]), False),678 ],679 )680 @pytest.mark.parametrize("sort", [None, False])681 def test_intersection_name_preservation(self, index2, keeps_name, sort):682 index1 = Index([1, 2, 3, 4, 5], name="index")683 expected = Index([3, 4, 5])684 result = index1.intersection(index2, sort)685 if keeps_name:686 expected.name = "index"687 assert result.name == expected.name688 tm.assert_index_equal(result, expected)689 @pytest.mark.parametrize(690 "first_name,second_name,expected_name",691 [("A", "A", "A"), ("A", "B", None), (None, "B", None)],692 )693 @pytest.mark.parametrize("sort", [None, False])694 def test_intersection_name_preservation2(695 self, first_name, second_name, expected_name, sort696 ):697 first = self.strIndex[5:20]698 second = self.strIndex[:10]699 first.name = first_name700 second.name = second_name701 intersect = first.intersection(second, sort=sort)702 assert intersect.name == expected_name703 @pytest.mark.parametrize(704 "index2,keeps_name",705 [706 (Index([4, 7, 6, 5, 3], name="index"), True),707 (Index([4, 7, 6, 5, 3], name="other"), False),708 ],709 )710 @pytest.mark.parametrize("sort", [None, False])711 def test_intersection_monotonic(self, index2, keeps_name, sort):712 index1 = Index([5, 3, 2, 4, 1], name="index")713 expected = Index([5, 3, 4])714 if keeps_name:715 expected.name = "index"716 result = index1.intersection(index2, sort=sort)717 if sort is None:718 expected = expected.sort_values()719 tm.assert_index_equal(result, expected)720 @pytest.mark.parametrize(721 "index2,expected_arr",722 [(Index(["B", "D"]), ["B"]), (Index(["B", "D", "A"]), ["A", "B", "A"])],723 )724 @pytest.mark.parametrize("sort", [None, False])725 def test_intersection_non_monotonic_non_unique(self, index2, expected_arr, sort):726 # non-monotonic non-unique727 index1 = Index(["A", "B", "A", "C"])728 expected = Index(expected_arr, dtype="object")729 result = index1.intersection(index2, sort=sort)730 if sort is None:731 expected = expected.sort_values()732 tm.assert_index_equal(result, expected)733 @pytest.mark.parametrize("sort", [None, False])734 def test_intersect_str_dates(self, sort):735 dt_dates = [datetime(2012, 2, 9), datetime(2012, 2, 22)]736 i1 = Index(dt_dates, dtype=object)737 i2 = Index(["aa"], dtype=object)738 result = i2.intersection(i1, sort=sort)739 assert len(result) == 0740 def test_intersect_nosort(self):741 result = pd.Index(["c", "b", "a"]).intersection(["b", "a"])742 expected = pd.Index(["b", "a"])743 tm.assert_index_equal(result, expected)744 def test_intersection_equal_sort(self):745 idx = pd.Index(["c", "a", "b"])746 tm.assert_index_equal(idx.intersection(idx, sort=False), idx)747 tm.assert_index_equal(idx.intersection(idx, sort=None), idx)748 @pytest.mark.xfail(reason="Not implemented")749 def test_intersection_equal_sort_true(self):750 # TODO decide on True behaviour751 idx = pd.Index(["c", "a", "b"])752 sorted_ = pd.Index(["a", "b", "c"])753 tm.assert_index_equal(idx.intersection(idx, sort=True), sorted_)754 @pytest.mark.parametrize("sort", [None, False])755 def test_chained_union(self, sort):756 # Chained unions handles names correctly757 i1 = Index([1, 2], name="i1")758 i2 = Index([5, 6], name="i2")759 i3 = Index([3, 4], name="i3")760 union = i1.union(i2.union(i3, sort=sort), sort=sort)761 expected = i1.union(i2, sort=sort).union(i3, sort=sort)762 tm.assert_index_equal(union, expected)763 j1 = Index([1, 2], name="j1")764 j2 = Index([], name="j2")765 j3 = Index([], name="j3")766 union = j1.union(j2.union(j3, sort=sort), sort=sort)767 expected = j1.union(j2, sort=sort).union(j3, sort=sort)768 tm.assert_index_equal(union, expected)769 @pytest.mark.parametrize("sort", [None, False])770 def test_union(self, sort):771 # TODO: Replace with fixturesult772 first = self.strIndex[5:20]773 second = self.strIndex[:10]774 everything = self.strIndex[:20]775 union = first.union(second, sort=sort)776 if sort is None:777 tm.assert_index_equal(union, everything.sort_values())778 assert tm.equalContents(union, everything)779 @pytest.mark.parametrize("slice_", [slice(None), slice(0)])780 def test_union_sort_other_special(self, slice_):781 # https://github.com/pandas-dev/pandas/issues/24959782 idx = pd.Index([1, 0, 2])783 # default, sort=None784 other = idx[slice_]785 tm.assert_index_equal(idx.union(other), idx)786 tm.assert_index_equal(other.union(idx), idx)787 # sort=False788 tm.assert_index_equal(idx.union(other, sort=False), idx)789 @pytest.mark.xfail(reason="Not implemented")790 @pytest.mark.parametrize("slice_", [slice(None), slice(0)])791 def test_union_sort_special_true(self, slice_):792 # TODO decide on True behaviour793 # sort=True794 idx = pd.Index([1, 0, 2])795 # default, sort=None796 other = idx[slice_]797 result = idx.union(other, sort=True)798 expected = pd.Index([0, 1, 2])799 tm.assert_index_equal(result, expected)800 def test_union_sort_other_incomparable(self):801 # https://github.com/pandas-dev/pandas/issues/24959802 idx = pd.Index([1, pd.Timestamp("2000")])803 # default (sort=None)804 with tm.assert_produces_warning(RuntimeWarning):805 result = idx.union(idx[:1])806 tm.assert_index_equal(result, idx)807 # sort=None808 with tm.assert_produces_warning(RuntimeWarning):809 result = idx.union(idx[:1], sort=None)810 tm.assert_index_equal(result, idx)811 # sort=False812 result = idx.union(idx[:1], sort=False)813 tm.assert_index_equal(result, idx)814 @pytest.mark.xfail(reason="Not implemented")815 def test_union_sort_other_incomparable_true(self):816 # TODO decide on True behaviour817 # sort=True818 idx = pd.Index([1, pd.Timestamp("2000")])819 with pytest.raises(TypeError, match=".*"):820 idx.union(idx[:1], sort=True)821 @pytest.mark.parametrize("klass", [np.array, Series, list])822 @pytest.mark.parametrize("sort", [None, False])823 def test_union_from_iterables(self, klass, sort):824 # GH 10149825 # TODO: Replace with fixturesult826 first = self.strIndex[5:20]827 second = self.strIndex[:10]828 everything = self.strIndex[:20]829 case = klass(second.values)830 result = first.union(case, sort=sort)831 if sort is None:832 tm.assert_index_equal(result, everything.sort_values())833 assert tm.equalContents(result, everything)834 @pytest.mark.parametrize("sort", [None, False])835 def test_union_identity(self, sort):836 # TODO: replace with fixturesult837 first = self.strIndex[5:20]838 union = first.union(first, sort=sort)839 # i.e. identity is not preserved when sort is True840 assert (union is first) is (not sort)841 # This should no longer be the same object, since [] is not consistent,842 # both objects will be recast to dtype('O')843 union = first.union([], sort=sort)844 assert (union is first) is (not sort)845 union = Index([]).union(first, sort=sort)846 assert (union is first) is (not sort)847 @pytest.mark.parametrize("first_list", [list("ba"), list()])848 @pytest.mark.parametrize("second_list", [list("ab"), list()])849 @pytest.mark.parametrize(850 "first_name, second_name, expected_name",851 [("A", "B", None), (None, "B", None), ("A", None, None)],852 )853 @pytest.mark.parametrize("sort", [None, False])854 def test_union_name_preservation(855 self, first_list, second_list, first_name, second_name, expected_name, sort856 ):857 first = Index(first_list, name=first_name)858 second = Index(second_list, name=second_name)859 union = first.union(second, sort=sort)860 vals = set(first_list).union(second_list)861 if sort is None and len(first_list) > 0 and len(second_list) > 0:862 expected = Index(sorted(vals), name=expected_name)863 tm.assert_index_equal(union, expected)864 else:865 expected = Index(vals, name=expected_name)866 assert tm.equalContents(union, expected)867 @pytest.mark.parametrize("sort", [None, False])868 def test_union_dt_as_obj(self, sort):869 # TODO: Replace with fixturesult870 firstCat = self.strIndex.union(self.dateIndex)871 secondCat = self.strIndex.union(self.strIndex)872 if self.dateIndex.dtype == np.object_:873 appended = np.append(self.strIndex, self.dateIndex)874 else:875 appended = np.append(self.strIndex, self.dateIndex.astype("O"))876 assert tm.equalContents(firstCat, appended)877 assert tm.equalContents(secondCat, self.strIndex)878 tm.assert_contains_all(self.strIndex, firstCat)879 tm.assert_contains_all(self.strIndex, secondCat)880 tm.assert_contains_all(self.dateIndex, firstCat)881 @pytest.mark.parametrize(882 "method", ["union", "intersection", "difference", "symmetric_difference"]883 )884 def test_setops_disallow_true(self, method):885 idx1 = pd.Index(["a", "b"])886 idx2 = pd.Index(["b", "c"])887 with pytest.raises(ValueError, match="The 'sort' keyword only takes"):888 getattr(idx1, method)(idx2, sort=True)889 def test_map_identity_mapping(self):890 # GH 12766891 # TODO: replace with fixture892 for name, cur_index in self.indices.items():893 tm.assert_index_equal(cur_index, cur_index.map(lambda x: x))894 def test_map_with_tuples(self):895 # GH 12766896 # Test that returning a single tuple from an Index897 # returns an Index.898 index = tm.makeIntIndex(3)899 result = tm.makeIntIndex(3).map(lambda x: (x,))900 expected = Index([(i,) for i in index])901 tm.assert_index_equal(result, expected)902 # Test that returning a tuple from a map of a single index903 # returns a MultiIndex object.904 result = index.map(lambda x: (x, x == 1))905 expected = MultiIndex.from_tuples([(i, i == 1) for i in index])906 tm.assert_index_equal(result, expected)907 def test_map_with_tuples_mi(self):908 # Test that returning a single object from a MultiIndex909 # returns an Index.910 first_level = ["foo", "bar", "baz"]911 multi_index = MultiIndex.from_tuples(zip(first_level, [1, 2, 3]))912 reduced_index = multi_index.map(lambda x: x[0])913 tm.assert_index_equal(reduced_index, Index(first_level))914 @pytest.mark.parametrize(915 "attr", ["makeDateIndex", "makePeriodIndex", "makeTimedeltaIndex"]916 )917 def test_map_tseries_indices_return_index(self, attr):918 index = getattr(tm, attr)(10)919 expected = Index([1] * 10)920 result = index.map(lambda x: 1)921 tm.assert_index_equal(expected, result)922 def test_map_tseries_indices_accsr_return_index(self):923 date_index = tm.makeDateIndex(24, freq="h", name="hourly")924 expected = Index(range(24), name="hourly")925 tm.assert_index_equal(expected, date_index.map(lambda x: x.hour))926 @pytest.mark.parametrize(927 "mapper",928 [929 lambda values, index: {i: e for e, i in zip(values, index)},930 lambda values, index: pd.Series(values, index),931 ],932 )933 def test_map_dictlike(self, mapper):934 # GH 12756935 expected = Index(["foo", "bar", "baz"])936 index = tm.makeIntIndex(3)937 result = index.map(mapper(expected.values, index))938 tm.assert_index_equal(result, expected)939 # TODO: replace with fixture940 for name in self.indices.keys():941 if name == "catIndex":942 # Tested in test_categorical943 continue944 elif name == "repeats":945 # Cannot map duplicated index946 continue947 index = self.indices[name]948 expected = Index(np.arange(len(index), 0, -1))949 # to match proper result coercion for uints950 if name == "empty":951 expected = Index([])952 result = index.map(mapper(expected, index))953 tm.assert_index_equal(result, expected)954 @pytest.mark.parametrize(955 "mapper",956 [Series(["foo", 2.0, "baz"], index=[0, 2, -1]), {0: "foo", 2: 2.0, -1: "baz"}],957 )958 def test_map_with_non_function_missing_values(self, mapper):959 # GH 12756960 expected = Index([2.0, np.nan, "foo"])961 result = Index([2, 1, 0]).map(mapper)962 tm.assert_index_equal(expected, result)963 def test_map_na_exclusion(self):964 index = Index([1.5, np.nan, 3, np.nan, 5])965 result = index.map(lambda x: x * 2, na_action="ignore")966 expected = index * 2967 tm.assert_index_equal(result, expected)968 def test_map_defaultdict(self):969 index = Index([1, 2, 3])970 default_dict = defaultdict(lambda: "blank")971 default_dict[1] = "stuff"972 result = index.map(default_dict)973 expected = Index(["stuff", "blank", "blank"])974 tm.assert_index_equal(result, expected)975 def test_append_multiple(self):976 index = Index(["a", "b", "c", "d", "e", "f"])977 foos = [index[:2], index[2:4], index[4:]]978 result = foos[0].append(foos[1:])979 tm.assert_index_equal(result, index)980 # empty981 result = index.append([])982 tm.assert_index_equal(result, index)983 @pytest.mark.parametrize("name,expected", [("foo", "foo"), ("bar", None)])984 def test_append_empty_preserve_name(self, name, expected):985 left = Index([], name="foo")986 right = Index([1, 2, 3], name=name)987 result = left.append(right)988 assert result.name == expected989 @pytest.mark.parametrize("second_name,expected", [(None, None), ("name", "name")])990 @pytest.mark.parametrize("sort", [None, False])991 def test_difference_name_preservation(self, second_name, expected, sort):992 # TODO: replace with fixturesult993 first = self.strIndex[5:20]994 second = self.strIndex[:10]995 answer = self.strIndex[10:20]996 first.name = "name"997 second.name = second_name998 result = first.difference(second, sort=sort)999 assert tm.equalContents(result, answer)1000 if expected is None:1001 assert result.name is None1002 else:1003 assert result.name == expected1004 @pytest.mark.parametrize("sort", [None, False])1005 def test_difference_empty_arg(self, sort):1006 first = self.strIndex[5:20]1007 first.name == "name"1008 result = first.difference([], sort)1009 assert tm.equalContents(result, first)1010 assert result.name == first.name1011 @pytest.mark.parametrize("sort", [None, False])1012 def test_difference_identity(self, sort):1013 first = self.strIndex[5:20]1014 first.name == "name"1015 result = first.difference(first, sort)1016 assert len(result) == 01017 assert result.name == first.name1018 @pytest.mark.parametrize("sort", [None, False])1019 def test_difference_sort(self, sort):1020 first = self.strIndex[5:20]1021 second = self.strIndex[:10]1022 result = first.difference(second, sort)1023 expected = self.strIndex[10:20]1024 if sort is None:1025 expected = expected.sort_values()1026 tm.assert_index_equal(result, expected)1027 @pytest.mark.parametrize("sort", [None, False])1028 def test_symmetric_difference(self, sort):1029 # smoke1030 index1 = Index([5, 2, 3, 4], name="index1")1031 index2 = Index([2, 3, 4, 1])1032 result = index1.symmetric_difference(index2, sort=sort)1033 expected = Index([5, 1])1034 assert tm.equalContents(result, expected)1035 assert result.name is None1036 if sort is None:1037 expected = expected.sort_values()1038 tm.assert_index_equal(result, expected)1039 # __xor__ syntax1040 expected = index1 ^ index21041 assert tm.equalContents(result, expected)1042 assert result.name is None1043 @pytest.mark.parametrize("opname", ["difference", "symmetric_difference"])1044 def test_difference_incomparable(self, opname):1045 a = pd.Index([3, pd.Timestamp("2000"), 1])1046 b = pd.Index([2, pd.Timestamp("1999"), 1])1047 op = operator.methodcaller(opname, b)1048 # sort=None, the default1049 result = op(a)1050 expected = pd.Index([3, pd.Timestamp("2000"), 2, pd.Timestamp("1999")])1051 if opname == "difference":1052 expected = expected[:2]1053 tm.assert_index_equal(result, expected)1054 # sort=False1055 op = operator.methodcaller(opname, b, sort=False)1056 result = op(a)1057 tm.assert_index_equal(result, expected)1058 @pytest.mark.xfail(reason="Not implemented")1059 @pytest.mark.parametrize("opname", ["difference", "symmetric_difference"])1060 def test_difference_incomparable_true(self, opname):1061 # TODO decide on True behaviour1062 # # sort=True, raises1063 a = pd.Index([3, pd.Timestamp("2000"), 1])1064 b = pd.Index([2, pd.Timestamp("1999"), 1])1065 op = operator.methodcaller(opname, b, sort=True)1066 with pytest.raises(TypeError, match="Cannot compare"):1067 op(a)1068 @pytest.mark.parametrize("sort", [None, False])1069 def test_symmetric_difference_mi(self, sort):1070 index1 = MultiIndex.from_tuples(self.tuples)1071 index2 = MultiIndex.from_tuples([("foo", 1), ("bar", 3)])1072 result = index1.symmetric_difference(index2, sort=sort)1073 expected = MultiIndex.from_tuples([("bar", 2), ("baz", 3), ("bar", 3)])1074 if sort is None:1075 expected = expected.sort_values()1076 tm.assert_index_equal(result, expected)1077 assert tm.equalContents(result, expected)1078 @pytest.mark.parametrize(1079 "index2,expected",1080 [1081 (Index([0, 1, np.nan]), Index([2.0, 3.0, 0.0])),1082 (Index([0, 1]), Index([np.nan, 2.0, 3.0, 0.0])),1083 ],1084 )1085 @pytest.mark.parametrize("sort", [None, False])1086 def test_symmetric_difference_missing(self, index2, expected, sort):1087 # GH 13514 change: {nan} - {nan} == {}1088 # (GH 6444, sorting of nans, is no longer an issue)1089 index1 = Index([1, np.nan, 2, 3])1090 result = index1.symmetric_difference(index2, sort=sort)1091 if sort is None:1092 expected = expected.sort_values()1093 tm.assert_index_equal(result, expected)1094 @pytest.mark.parametrize("sort", [None, False])1095 def test_symmetric_difference_non_index(self, sort):1096 index1 = Index([1, 2, 3, 4], name="index1")1097 index2 = np.array([2, 3, 4, 5])1098 expected = Index([1, 5])1099 result = index1.symmetric_difference(index2, sort=sort)1100 assert tm.equalContents(result, expected)1101 assert result.name == "index1"1102 result = index1.symmetric_difference(index2, result_name="new_name", sort=sort)1103 assert tm.equalContents(result, expected)1104 assert result.name == "new_name"1105 @pytest.mark.parametrize("sort", [None, False])1106 def test_difference_type(self, sort):1107 # GH 200401108 # If taking difference of a set and itself, it1109 # needs to preserve the type of the index1110 skip_index_keys = ["repeats"]1111 for key, index in self.generate_index_types(skip_index_keys):1112 result = index.difference(index, sort=sort)1113 expected = index.drop(index)1114 tm.assert_index_equal(result, expected)1115 @pytest.mark.parametrize("sort", [None, False])1116 def test_intersection_difference(self, sort):1117 # GH 200401118 # Test that the intersection of an index with an1119 # empty index produces the same index as the difference1120 # of an index with itself. Test for all types1121 skip_index_keys = ["repeats"]1122 for key, index in self.generate_index_types(skip_index_keys):1123 inter = index.intersection(index.drop(index))1124 diff = index.difference(index, sort=sort)1125 tm.assert_index_equal(inter, diff)1126 @pytest.mark.parametrize(1127 "attr,expected",1128 [1129 ("strIndex", False),1130 ("boolIndex", False),1131 ("catIndex", False),1132 ("intIndex", True),1133 ("dateIndex", False),1134 ("floatIndex", True),1135 ],1136 )1137 def test_is_numeric(self, attr, expected):1138 assert getattr(self, attr).is_numeric() == expected1139 @pytest.mark.parametrize(1140 "attr,expected",1141 [1142 ("strIndex", True),1143 ("boolIndex", True),1144 ("catIndex", False),1145 ("intIndex", False),1146 ("dateIndex", False),1147 ("floatIndex", False),1148 ],1149 )1150 def test_is_object(self, attr, expected):1151 assert getattr(self, attr).is_object() == expected1152 @pytest.mark.parametrize(1153 "attr,expected",1154 [1155 ("strIndex", False),1156 ("boolIndex", False),1157 ("catIndex", False),1158 ("intIndex", False),1159 ("dateIndex", True),1160 ("floatIndex", False),1161 ],1162 )1163 def test_is_all_dates(self, attr, expected):1164 assert getattr(self, attr).is_all_dates == expected1165 def test_summary(self):1166 self._check_method_works(Index._summary)1167 # GH38691168 ind = Index(["{other}%s", "~:{range}:0"], name="A")1169 result = ind._summary()1170 # shouldn't be formatted accidentally.1171 assert "~:{range}:0" in result1172 assert "{other}%s" in result1173 # GH182171174 def test_summary_deprecated(self):1175 ind = Index(["{other}%s", "~:{range}:0"], name="A")1176 with tm.assert_produces_warning(FutureWarning):1177 ind.summary()1178 def test_format(self):1179 self._check_method_works(Index.format)1180 # GH 146261181 # windows has different precision on datetime.datetime.now (it doesn't1182 # include us since the default for Timestamp shows these but Index1183 # formatting does not we are skipping)1184 now = datetime.now()1185 if not str(now).endswith("000"):1186 index = Index([now])1187 formatted = index.format()1188 expected = [str(index[0])]1189 assert formatted == expected1190 self.strIndex[:0].format()1191 @pytest.mark.parametrize("vals", [[1, 2.0 + 3.0j, 4.0], ["a", "b", "c"]])1192 def test_format_missing(self, vals, nulls_fixture):1193 # 28451194 vals = list(vals) # Copy for each iteration1195 vals.append(nulls_fixture)1196 index = Index(vals)1197 formatted = index.format()1198 expected = [str(index[0]), str(index[1]), str(index[2]), "NaN"]1199 assert formatted == expected1200 assert index[3] is nulls_fixture1201 def test_format_with_name_time_info(self):1202 # bug I fixed 12/20/20111203 inc = timedelta(hours=4)1204 dates = Index([dt + inc for dt in self.dateIndex], name="something")1205 formatted = dates.format(name=True)1206 assert formatted[0] == "something"1207 def test_format_datetime_with_time(self):1208 t = Index([datetime(2012, 2, 7), datetime(2012, 2, 7, 23)])1209 result = t.format()1210 expected = ["2012-02-07 00:00:00", "2012-02-07 23:00:00"]1211 assert len(result) == 21212 assert result == expected1213 @pytest.mark.parametrize("op", ["any", "all"])1214 def test_logical_compat(self, op):1215 index = self.create_index()1216 assert getattr(index, op)() == getattr(index.values, op)()1217 def _check_method_works(self, method):1218 # TODO: make this a dedicated test with parametrized methods1219 method(self.empty)1220 method(self.dateIndex)1221 method(self.unicodeIndex)1222 method(self.strIndex)1223 method(self.intIndex)1224 method(self.tuples)1225 method(self.catIndex)1226 def test_get_indexer(self):1227 index1 = Index([1, 2, 3, 4, 5])1228 index2 = Index([2, 4, 6])1229 r1 = index1.get_indexer(index2)1230 e1 = np.array([1, 3, -1], dtype=np.intp)1231 assert_almost_equal(r1, e1)1232 @pytest.mark.parametrize("reverse", [True, False])1233 @pytest.mark.parametrize(1234 "expected,method",1235 [1236 (np.array([-1, 0, 0, 1, 1], dtype=np.intp), "pad"),1237 (np.array([-1, 0, 0, 1, 1], dtype=np.intp), "ffill"),1238 (np.array([0, 0, 1, 1, 2], dtype=np.intp), "backfill"),1239 (np.array([0, 0, 1, 1, 2], dtype=np.intp), "bfill"),1240 ],1241 )1242 def test_get_indexer_methods(self, reverse, expected, method):1243 index1 = Index([1, 2, 3, 4, 5])1244 index2 = Index([2, 4, 6])1245 if reverse:1246 index1 = index1[::-1]1247 expected = expected[::-1]1248 result = index2.get_indexer(index1, method=method)1249 assert_almost_equal(result, expected)1250 def test_get_indexer_invalid(self):1251 # GH104111252 index = Index(np.arange(10))1253 with pytest.raises(ValueError, match="tolerance argument"):1254 index.get_indexer([1, 0], tolerance=1)1255 with pytest.raises(ValueError, match="limit argument"):1256 index.get_indexer([1, 0], limit=1)1257 @pytest.mark.parametrize(1258 "method, tolerance, indexer, expected",1259 [1260 ("pad", None, [0, 5, 9], [0, 5, 9]),1261 ("backfill", None, [0, 5, 9], [0, 5, 9]),1262 ("nearest", None, [0, 5, 9], [0, 5, 9]),1263 ("pad", 0, [0, 5, 9], [0, 5, 9]),1264 ("backfill", 0, [0, 5, 9], [0, 5, 9]),1265 ("nearest", 0, [0, 5, 9], [0, 5, 9]),1266 ("pad", None, [0.2, 1.8, 8.5], [0, 1, 8]),1267 ("backfill", None, [0.2, 1.8, 8.5], [1, 2, 9]),1268 ("nearest", None, [0.2, 1.8, 8.5], [0, 2, 9]),1269 ("pad", 1, [0.2, 1.8, 8.5], [0, 1, 8]),1270 ("backfill", 1, [0.2, 1.8, 8.5], [1, 2, 9]),1271 ("nearest", 1, [0.2, 1.8, 8.5], [0, 2, 9]),1272 ("pad", 0.2, [0.2, 1.8, 8.5], [0, -1, -1]),1273 ("backfill", 0.2, [0.2, 1.8, 8.5], [-1, 2, -1]),1274 ("nearest", 0.2, [0.2, 1.8, 8.5], [0, 2, -1]),1275 ],1276 )1277 def test_get_indexer_nearest(self, method, tolerance, indexer, expected):1278 index = Index(np.arange(10))1279 actual = index.get_indexer(indexer, method=method, tolerance=tolerance)1280 tm.assert_numpy_array_equal(actual, np.array(expected, dtype=np.intp))1281 @pytest.mark.parametrize("listtype", [list, tuple, Series, np.array])1282 @pytest.mark.parametrize(1283 "tolerance, expected",1284 list(1285 zip(1286 [[0.3, 0.3, 0.1], [0.2, 0.1, 0.1], [0.1, 0.5, 0.5]],1287 [[0, 2, -1], [0, -1, -1], [-1, 2, 9]],1288 )1289 ),1290 )1291 def test_get_indexer_nearest_listlike_tolerance(1292 self, tolerance, expected, listtype1293 ):1294 index = Index(np.arange(10))1295 actual = index.get_indexer(1296 [0.2, 1.8, 8.5], method="nearest", tolerance=listtype(tolerance)1297 )1298 tm.assert_numpy_array_equal(actual, np.array(expected, dtype=np.intp))1299 def test_get_indexer_nearest_error(self):1300 index = Index(np.arange(10))1301 with pytest.raises(ValueError, match="limit argument"):1302 index.get_indexer([1, 0], method="nearest", limit=1)1303 with pytest.raises(ValueError, match="tolerance size must match"):1304 index.get_indexer([1, 0], method="nearest", tolerance=[1, 2, 3])1305 @pytest.mark.parametrize(1306 "method,expected",1307 [("pad", [8, 7, 0]), ("backfill", [9, 8, 1]), ("nearest", [9, 7, 0])],1308 )1309 def test_get_indexer_nearest_decreasing(self, method, expected):1310 index = Index(np.arange(10))[::-1]1311 actual = index.get_indexer([0, 5, 9], method=method)1312 tm.assert_numpy_array_equal(actual, np.array([9, 4, 0], dtype=np.intp))1313 actual = index.get_indexer([0.2, 1.8, 8.5], method=method)1314 tm.assert_numpy_array_equal(actual, np.array(expected, dtype=np.intp))1315 @pytest.mark.parametrize(1316 "method,expected",1317 [1318 ("pad", np.array([-1, 0, 1, 1], dtype=np.intp)),1319 ("backfill", np.array([0, 0, 1, -1], dtype=np.intp)),1320 ],1321 )1322 def test_get_indexer_strings(self, method, expected):1323 index = pd.Index(["b", "c"])1324 actual = index.get_indexer(["a", "b", "c", "d"], method=method)1325 tm.assert_numpy_array_equal(actual, expected)1326 def test_get_indexer_strings_raises(self):1327 index = pd.Index(["b", "c"])1328 msg = r"unsupported operand type\(s\) for -: 'str' and 'str'"1329 with pytest.raises(TypeError, match=msg):1330 index.get_indexer(["a", "b", "c", "d"], method="nearest")1331 with pytest.raises(TypeError, match=msg):1332 index.get_indexer(["a", "b", "c", "d"], method="pad", tolerance=2)1333 with pytest.raises(TypeError, match=msg):1334 index.get_indexer(1335 ["a", "b", "c", "d"], method="pad", tolerance=[2, 2, 2, 2]1336 )1337 @pytest.mark.parametrize("idx_class", [Int64Index, RangeIndex, Float64Index])1338 def test_get_indexer_numeric_index_boolean_target(self, idx_class):1339 # GH 168771340 numeric_index = idx_class(RangeIndex((4)))1341 result = numeric_index.get_indexer([True, False, True])1342 expected = np.array([-1, -1, -1], dtype=np.intp)1343 tm.assert_numpy_array_equal(result, expected)1344 def test_get_indexer_with_NA_values(1345 self, unique_nulls_fixture, unique_nulls_fixture21346 ):1347 # GH 223321348 # check pairwise, that no pair of na values1349 # is mangled1350 if unique_nulls_fixture is unique_nulls_fixture2:1351 return # skip it, values are not unique1352 arr = np.array([unique_nulls_fixture, unique_nulls_fixture2], dtype=np.object)1353 index = pd.Index(arr, dtype=np.object)1354 result = index.get_indexer(1355 [unique_nulls_fixture, unique_nulls_fixture2, "Unknown"]1356 )1357 expected = np.array([0, 1, -1], dtype=np.intp)1358 tm.assert_numpy_array_equal(result, expected)1359 @pytest.mark.parametrize("method", [None, "pad", "backfill", "nearest"])1360 def test_get_loc(self, method):1361 index = pd.Index([0, 1, 2])1362 assert index.get_loc(1, method=method) == 11363 if method:1364 assert index.get_loc(1, method=method, tolerance=0) == 11365 @pytest.mark.parametrize("method", [None, "pad", "backfill", "nearest"])1366 def test_get_loc_raises_bad_label(self, method):1367 index = pd.Index([0, 1, 2])1368 if method:1369 # Messages vary across versions1370 if PY36:1371 msg = "not supported between"1372 else:1373 msg = "unorderable types"1374 else:1375 msg = "invalid key"1376 with pytest.raises(TypeError, match=msg):1377 index.get_loc([1, 2], method=method)1378 @pytest.mark.parametrize(1379 "method,loc", [("pad", 1), ("backfill", 2), ("nearest", 1)]1380 )1381 def test_get_loc_tolerance(self, method, loc):1382 index = pd.Index([0, 1, 2])1383 assert index.get_loc(1.1, method) == loc1384 assert index.get_loc(1.1, method, tolerance=1) == loc1385 @pytest.mark.parametrize("method", ["pad", "backfill", "nearest"])1386 def test_get_loc_outside_tolerance_raises(self, method):1387 index = pd.Index([0, 1, 2])1388 with pytest.raises(KeyError, match="1.1"):1389 index.get_loc(1.1, method, tolerance=0.05)1390 def test_get_loc_bad_tolerance_raises(self):1391 index = pd.Index([0, 1, 2])1392 with pytest.raises(ValueError, match="must be numeric"):1393 index.get_loc(1.1, "nearest", tolerance="invalid")1394 def test_get_loc_tolerance_no_method_raises(self):1395 index = pd.Index([0, 1, 2])1396 with pytest.raises(ValueError, match="tolerance .* valid if"):1397 index.get_loc(1.1, tolerance=1)1398 def test_get_loc_raises_missized_tolerance(self):1399 index = pd.Index([0, 1, 2])1400 with pytest.raises(ValueError, match="tolerance size must match"):1401 index.get_loc(1.1, "nearest", tolerance=[1, 1])1402 def test_get_loc_raises_object_nearest(self):1403 index = pd.Index(["a", "c"])1404 with pytest.raises(TypeError, match="unsupported operand type"):1405 index.get_loc("a", method="nearest")1406 def test_get_loc_raises_object_tolerance(self):1407 index = pd.Index(["a", "c"])1408 with pytest.raises(TypeError, match="unsupported operand type"):1409 index.get_loc("a", method="pad", tolerance="invalid")1410 @pytest.mark.parametrize("dtype", [int, float])1411 def test_slice_locs(self, dtype):1412 index = Index(np.array([0, 1, 2, 5, 6, 7, 9, 10], dtype=dtype))1413 n = len(index)1414 assert index.slice_locs(start=2) == (2, n)1415 assert index.slice_locs(start=3) == (3, n)1416 assert index.slice_locs(3, 8) == (3, 6)1417 assert index.slice_locs(5, 10) == (3, n)1418 assert index.slice_locs(end=8) == (0, 6)1419 assert index.slice_locs(end=9) == (0, 7)1420 # reversed1421 index2 = index[::-1]1422 assert index2.slice_locs(8, 2) == (2, 6)1423 assert index2.slice_locs(7, 3) == (2, 5)1424 @pytest.mark.parametrize("dtype", [int, float])1425 def test_slice_float_locs(self, dtype):1426 index = Index(np.array([0, 1, 2, 5, 6, 7, 9, 10], dtype=dtype))1427 n = len(index)1428 assert index.slice_locs(5.0, 10.0) == (3, n)1429 assert index.slice_locs(4.5, 10.5) == (3, 8)1430 index2 = index[::-1]1431 assert index2.slice_locs(8.5, 1.5) == (2, 6)1432 assert index2.slice_locs(10.5, -1) == (0, n)1433 def test_slice_locs_dup(self):1434 index = Index(["a", "a", "b", "c", "d", "d"])1435 assert index.slice_locs("a", "d") == (0, 6)1436 assert index.slice_locs(end="d") == (0, 6)1437 assert index.slice_locs("a", "c") == (0, 4)1438 assert index.slice_locs("b", "d") == (2, 6)1439 index2 = index[::-1]1440 assert index2.slice_locs("d", "a") == (0, 6)1441 assert index2.slice_locs(end="a") == (0, 6)1442 assert index2.slice_locs("d", "b") == (0, 4)1443 assert index2.slice_locs("c", "a") == (2, 6)1444 @pytest.mark.parametrize("dtype", [int, float])1445 def test_slice_locs_dup_numeric(self, dtype):1446 index = Index(np.array([10, 12, 12, 14], dtype=dtype))1447 assert index.slice_locs(12, 12) == (1, 3)1448 assert index.slice_locs(11, 13) == (1, 3)1449 index2 = index[::-1]1450 assert index2.slice_locs(12, 12) == (1, 3)1451 assert index2.slice_locs(13, 11) == (1, 3)1452 def test_slice_locs_na(self):1453 index = Index([np.nan, 1, 2])1454 assert index.slice_locs(1) == (1, 3)1455 assert index.slice_locs(np.nan) == (0, 3)1456 index = Index([0, np.nan, np.nan, 1, 2])1457 assert index.slice_locs(np.nan) == (1, 5)1458 def test_slice_locs_na_raises(self):1459 index = Index([np.nan, 1, 2])1460 with pytest.raises(KeyError, match=""):1461 index.slice_locs(start=1.5)1462 with pytest.raises(KeyError, match=""):1463 index.slice_locs(end=1.5)1464 @pytest.mark.parametrize(1465 "in_slice,expected",1466 [1467 (pd.IndexSlice[::-1], "yxdcb"),1468 (pd.IndexSlice["b":"y":-1], ""),1469 (pd.IndexSlice["b"::-1], "b"),1470 (pd.IndexSlice[:"b":-1], "yxdcb"),1471 (pd.IndexSlice[:"y":-1], "y"),1472 (pd.IndexSlice["y"::-1], "yxdcb"),1473 (pd.IndexSlice["y"::-4], "yb"),1474 # absent labels1475 (pd.IndexSlice[:"a":-1], "yxdcb"),1476 (pd.IndexSlice[:"a":-2], "ydb"),1477 (pd.IndexSlice["z"::-1], "yxdcb"),1478 (pd.IndexSlice["z"::-3], "yc"),1479 (pd.IndexSlice["m"::-1], "dcb"),1480 (pd.IndexSlice[:"m":-1], "yx"),1481 (pd.IndexSlice["a":"a":-1], ""),1482 (pd.IndexSlice["z":"z":-1], ""),1483 (pd.IndexSlice["m":"m":-1], ""),1484 ],1485 )1486 def test_slice_locs_negative_step(self, in_slice, expected):1487 index = Index(list("bcdxy"))1488 s_start, s_stop = index.slice_locs(in_slice.start, in_slice.stop, in_slice.step)1489 result = index[s_start : s_stop : in_slice.step]1490 expected = pd.Index(list(expected))1491 tm.assert_index_equal(result, expected)1492 def test_drop_by_str_label(self):1493 # TODO: Parametrize these after replacing self.strIndex with fixture1494 n = len(self.strIndex)1495 drop = self.strIndex[list(range(5, 10))]1496 dropped = self.strIndex.drop(drop)1497 expected = self.strIndex[list(range(5)) + list(range(10, n))]1498 tm.assert_index_equal(dropped, expected)1499 dropped = self.strIndex.drop(self.strIndex[0])1500 expected = self.strIndex[1:]1501 tm.assert_index_equal(dropped, expected)1502 @pytest.mark.parametrize("keys", [["foo", "bar"], ["1", "bar"]])1503 def test_drop_by_str_label_raises_missing_keys(self, keys):1504 with pytest.raises(KeyError, match=""):1505 self.strIndex.drop(keys)1506 def test_drop_by_str_label_errors_ignore(self):1507 # TODO: Parametrize these after replacing self.strIndex with fixture1508 # errors='ignore'1509 n = len(self.strIndex)1510 drop = self.strIndex[list(range(5, 10))]1511 mixed = drop.tolist() + ["foo"]1512 dropped = self.strIndex.drop(mixed, errors="ignore")1513 expected = self.strIndex[list(range(5)) + list(range(10, n))]1514 tm.assert_index_equal(dropped, expected)1515 dropped = self.strIndex.drop(["foo", "bar"], errors="ignore")1516 expected = self.strIndex[list(range(n))]1517 tm.assert_index_equal(dropped, expected)1518 def test_drop_by_numeric_label_loc(self):1519 # TODO: Parametrize numeric and str tests after self.strIndex fixture1520 index = Index([1, 2, 3])1521 dropped = index.drop(1)1522 expected = Index([2, 3])1523 tm.assert_index_equal(dropped, expected)1524 def test_drop_by_numeric_label_raises_missing_keys(self):1525 index = Index([1, 2, 3])1526 with pytest.raises(KeyError, match=""):1527 index.drop([3, 4])1528 @pytest.mark.parametrize(1529 "key,expected", [(4, Index([1, 2, 3])), ([3, 4, 5], Index([1, 2]))]1530 )1531 def test_drop_by_numeric_label_errors_ignore(self, key, expected):1532 index = Index([1, 2, 3])1533 dropped = index.drop(key, errors="ignore")1534 tm.assert_index_equal(dropped, expected)1535 @pytest.mark.parametrize(1536 "values",1537 [["a", "b", ("c", "d")], ["a", ("c", "d"), "b"], [("c", "d"), "a", "b"]],1538 )1539 @pytest.mark.parametrize("to_drop", [[("c", "d"), "a"], ["a", ("c", "d")]])1540 def test_drop_tuple(self, values, to_drop):1541 # GH 183041542 index = pd.Index(values)1543 expected = pd.Index(["b"])1544 result = index.drop(to_drop)1545 tm.assert_index_equal(result, expected)1546 removed = index.drop(to_drop[0])1547 for drop_me in to_drop[1], [to_drop[1]]:1548 result = removed.drop(drop_me)1549 tm.assert_index_equal(result, expected)1550 removed = index.drop(to_drop[1])1551 msg = r"\"\[{}\] not found in axis\"".format(re.escape(to_drop[1].__repr__()))1552 for drop_me in to_drop[1], [to_drop[1]]:1553 with pytest.raises(KeyError, match=msg):1554 removed.drop(drop_me)1555 @pytest.mark.parametrize(1556 "method,expected,sort",1557 [1558 (1559 "intersection",1560 np.array(1561 [(1, "A"), (2, "A"), (1, "B"), (2, "B")],1562 dtype=[("num", int), ("let", "a1")],1563 ),1564 False,1565 ),1566 (1567 "intersection",1568 np.array(1569 [(1, "A"), (1, "B"), (2, "A"), (2, "B")],1570 dtype=[("num", int), ("let", "a1")],1571 ),1572 None,1573 ),1574 (1575 "union",1576 np.array(1577 [(1, "A"), (1, "B"), (1, "C"), (2, "A"), (2, "B"), (2, "C")],1578 dtype=[("num", int), ("let", "a1")],1579 ),1580 None,1581 ),1582 ],1583 )1584 def test_tuple_union_bug(self, method, expected, sort):1585 index1 = Index(1586 np.array(1587 [(1, "A"), (2, "A"), (1, "B"), (2, "B")],1588 dtype=[("num", int), ("let", "a1")],1589 )1590 )1591 index2 = Index(1592 np.array(1593 [(1, "A"), (2, "A"), (1, "B"), (2, "B"), (1, "C"), (2, "C")],1594 dtype=[("num", int), ("let", "a1")],1595 )1596 )1597 result = getattr(index1, method)(index2, sort=sort)1598 assert result.ndim == 11599 expected = Index(expected)1600 tm.assert_index_equal(result, expected)1601 @pytest.mark.parametrize(1602 "attr",1603 [1604 "is_monotonic_increasing",1605 "is_monotonic_decreasing",1606 "_is_strictly_monotonic_increasing",1607 "_is_strictly_monotonic_decreasing",1608 ],1609 )1610 def test_is_monotonic_incomparable(self, attr):1611 index = Index([5, datetime.now(), 7])1612 assert not getattr(index, attr)1613 def test_get_set_value(self):1614 # TODO: Remove function? GH 197281615 values = np.random.randn(100)1616 date = self.dateIndex[67]1617 assert_almost_equal(self.dateIndex.get_value(values, date), values[67])1618 self.dateIndex.set_value(values, date, 10)1619 assert values[67] == 101620 @pytest.mark.parametrize("values", [["foo", "bar", "quux"], {"foo", "bar", "quux"}])1621 @pytest.mark.parametrize(1622 "index,expected",1623 [1624 (Index(["qux", "baz", "foo", "bar"]), np.array([False, False, True, True])),1625 (Index([]), np.array([], dtype=bool)), # empty1626 ],1627 )1628 def test_isin(self, values, index, expected):1629 result = index.isin(values)1630 tm.assert_numpy_array_equal(result, expected)1631 def test_isin_nan_common_object(self, nulls_fixture, nulls_fixture2):1632 # Test cartesian product of null fixtures and ensure that we don't1633 # mangle the various types (save a corner case with PyPy)1634 # all nans are the same1635 if (1636 isinstance(nulls_fixture, float)1637 and isinstance(nulls_fixture2, float)1638 and math.isnan(nulls_fixture)1639 and math.isnan(nulls_fixture2)1640 ):1641 tm.assert_numpy_array_equal(1642 Index(["a", nulls_fixture]).isin([nulls_fixture2]),1643 np.array([False, True]),1644 )1645 elif nulls_fixture is nulls_fixture2: # should preserve NA type1646 tm.assert_numpy_array_equal(1647 Index(["a", nulls_fixture]).isin([nulls_fixture2]),1648 np.array([False, True]),1649 )1650 else:1651 tm.assert_numpy_array_equal(1652 Index(["a", nulls_fixture]).isin([nulls_fixture2]),1653 np.array([False, False]),1654 )1655 def test_isin_nan_common_float64(self, nulls_fixture):1656 if nulls_fixture is pd.NaT:1657 pytest.skip("pd.NaT not compatible with Float64Index")1658 # Float64Index overrides isin, so must be checked separately1659 tm.assert_numpy_array_equal(1660 Float64Index([1.0, nulls_fixture]).isin([np.nan]), np.array([False, True])1661 )1662 # we cannot compare NaT with NaN1663 tm.assert_numpy_array_equal(1664 Float64Index([1.0, nulls_fixture]).isin([pd.NaT]), np.array([False, False])1665 )1666 @pytest.mark.parametrize("level", [0, -1])1667 @pytest.mark.parametrize(1668 "index",1669 [1670 Index(["qux", "baz", "foo", "bar"]),1671 # Float64Index overrides isin, so must be checked separately1672 Float64Index([1.0, 2.0, 3.0, 4.0]),1673 ],1674 )1675 def test_isin_level_kwarg(self, level, index):1676 values = index.tolist()[-2:] + ["nonexisting"]1677 expected = np.array([False, False, True, True])1678 tm.assert_numpy_array_equal(expected, index.isin(values, level=level))1679 index.name = "foobar"1680 tm.assert_numpy_array_equal(expected, index.isin(values, level="foobar"))1681 @pytest.mark.parametrize("level", [2, 10, -3])1682 def test_isin_level_kwarg_bad_level_raises(self, level, indices):1683 index = indices1684 with pytest.raises(IndexError, match="Too many levels"):1685 index.isin([], level=level)1686 @pytest.mark.parametrize("label", [1.0, "foobar", "xyzzy", np.nan])1687 def test_isin_level_kwarg_bad_label_raises(self, label, indices):1688 index = indices1689 if isinstance(index, MultiIndex):1690 index = index.rename(["foo", "bar"])1691 msg = "'Level {} not found'"1692 else:1693 index = index.rename("foo")1694 msg = r"'Level {} must be same as name \(foo\)'"1695 with pytest.raises(KeyError, match=msg.format(label)):1696 index.isin([], level=label)1697 @pytest.mark.parametrize("empty", [[], Series(), np.array([])])1698 def test_isin_empty(self, empty):1699 # see gh-169911700 index = Index(["a", "b"])1701 expected = np.array([False, False])1702 result = index.isin(empty)1703 tm.assert_numpy_array_equal(expected, result)1704 @pytest.mark.parametrize(1705 "values",1706 [1707 [1, 2, 3, 4],1708 [1.0, 2.0, 3.0, 4.0],1709 [True, True, True, True],1710 ["foo", "bar", "baz", "qux"],1711 pd.date_range("2018-01-01", freq="D", periods=4),1712 ],1713 )1714 def test_boolean_cmp(self, values):1715 index = Index(values)1716 result = index == values1717 expected = np.array([True, True, True, True], dtype=bool)1718 tm.assert_numpy_array_equal(result, expected)1719 @pytest.mark.parametrize("name,level", [(None, 0), ("a", "a")])1720 def test_get_level_values(self, name, level):1721 expected = self.strIndex.copy()1722 if name:1723 expected.name = name1724 result = expected.get_level_values(level)1725 tm.assert_index_equal(result, expected)1726 def test_slice_keep_name(self):1727 index = Index(["a", "b"], name="asdf")1728 assert index.name == index[1:].name1729 # instance attributes of the form self.<name>Index1730 @pytest.mark.parametrize("index_kind", ["unicode", "str", "date", "int", "float"])1731 def test_join_self(self, join_type, index_kind):1732 res = getattr(self, "{0}Index".format(index_kind))1733 joined = res.join(res, how=join_type)1734 assert res is joined1735 @pytest.mark.parametrize("method", ["strip", "rstrip", "lstrip"])1736 def test_str_attribute(self, method):1737 # GH90681738 index = Index([" jack", "jill ", " jesse ", "frank"])1739 expected = Index([getattr(str, method)(x) for x in index.values])1740 result = getattr(index.str, method)()1741 tm.assert_index_equal(result, expected)1742 @pytest.mark.parametrize(1743 "index",1744 [1745 Index(range(5)),1746 tm.makeDateIndex(10),1747 MultiIndex.from_tuples([("foo", "1"), ("bar", "3")]),1748 period_range(start="2000", end="2010", freq="A"),1749 ],1750 )1751 def test_str_attribute_raises(self, index):1752 with pytest.raises(AttributeError, match="only use .str accessor"):1753 index.str.repeat(2)1754 @pytest.mark.parametrize(1755 "expand,expected",1756 [1757 (None, Index([["a", "b", "c"], ["d", "e"], ["f"]])),1758 (False, Index([["a", "b", "c"], ["d", "e"], ["f"]])),1759 (1760 True,1761 MultiIndex.from_tuples(1762 [("a", "b", "c"), ("d", "e", np.nan), ("f", np.nan, np.nan)]1763 ),1764 ),1765 ],1766 )1767 def test_str_split(self, expand, expected):1768 index = Index(["a b c", "d e", "f"])1769 if expand is not None:1770 result = index.str.split(expand=expand)1771 else:1772 result = index.str.split()1773 tm.assert_index_equal(result, expected)1774 def test_str_bool_return(self):1775 # test boolean case, should return np.array instead of boolean Index1776 index = Index(["a1", "a2", "b1", "b2"])1777 result = index.str.startswith("a")1778 expected = np.array([True, True, False, False])1779 tm.assert_numpy_array_equal(result, expected)1780 assert isinstance(result, np.ndarray)1781 def test_str_bool_series_indexing(self):1782 index = Index(["a1", "a2", "b1", "b2"])1783 s = Series(range(4), index=index)1784 result = s[s.index.str.startswith("a")]1785 expected = Series(range(2), index=["a1", "a2"])1786 tm.assert_series_equal(result, expected)1787 @pytest.mark.parametrize(1788 "index,expected", [(Index(list("abcd")), True), (Index(range(4)), False)]1789 )1790 def test_tab_completion(self, index, expected):1791 # GH 99101792 result = "str" in dir(index)1793 assert result == expected1794 def test_indexing_doesnt_change_class(self):1795 index = Index([1, 2, 3, "a", "b", "c"])1796 assert index[1:3].identical(pd.Index([2, 3], dtype=np.object_))1797 assert index[[0, 1]].identical(pd.Index([1, 2], dtype=np.object_))1798 def test_outer_join_sort(self):1799 left_index = Index(np.random.permutation(15))1800 right_index = tm.makeDateIndex(10)1801 with tm.assert_produces_warning(RuntimeWarning):1802 result = left_index.join(right_index, how="outer")1803 # right_index in this case because DatetimeIndex has join precedence1804 # over Int64Index1805 with tm.assert_produces_warning(RuntimeWarning):1806 expected = right_index.astype(object).union(left_index.astype(object))1807 tm.assert_index_equal(result, expected)1808 def test_nan_first_take_datetime(self):1809 index = Index([pd.NaT, Timestamp("20130101"), Timestamp("20130102")])1810 result = index.take([-1, 0, 1])1811 expected = Index([index[-1], index[0], index[1]])1812 tm.assert_index_equal(result, expected)1813 def test_take_fill_value(self):1814 # GH 126311815 index = pd.Index(list("ABC"), name="xxx")1816 result = index.take(np.array([1, 0, -1]))1817 expected = pd.Index(list("BAC"), name="xxx")1818 tm.assert_index_equal(result, expected)1819 # fill_value1820 result = index.take(np.array([1, 0, -1]), fill_value=True)1821 expected = pd.Index(["B", "A", np.nan], name="xxx")1822 tm.assert_index_equal(result, expected)1823 # allow_fill=False1824 result = index.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)1825 expected = pd.Index(["B", "A", "C"], name="xxx")1826 tm.assert_index_equal(result, expected)1827 def test_take_fill_value_none_raises(self):1828 index = pd.Index(list("ABC"), name="xxx")1829 msg = (1830 "When allow_fill=True and fill_value is not None, "1831 "all indices must be >= -1"1832 )1833 with pytest.raises(ValueError, match=msg):1834 index.take(np.array([1, 0, -2]), fill_value=True)1835 with pytest.raises(ValueError, match=msg):1836 index.take(np.array([1, 0, -5]), fill_value=True)1837 def test_take_bad_bounds_raises(self):1838 index = pd.Index(list("ABC"), name="xxx")1839 with pytest.raises(IndexError, match="out of bounds"):1840 index.take(np.array([1, -5]))1841 @pytest.mark.parametrize("name", [None, "foobar"])1842 @pytest.mark.parametrize(1843 "labels",1844 [1845 [],1846 np.array([]),1847 ["A", "B", "C"],1848 ["C", "B", "A"],1849 np.array(["A", "B", "C"]),1850 np.array(["C", "B", "A"]),1851 # Must preserve name even if dtype changes1852 pd.date_range("20130101", periods=3).values,1853 pd.date_range("20130101", periods=3).tolist(),1854 ],1855 )1856 def test_reindex_preserves_name_if_target_is_list_or_ndarray(self, name, labels):1857 # GH65521858 index = pd.Index([0, 1, 2])1859 index.name = name1860 assert index.reindex(labels)[0].name == name1861 @pytest.mark.parametrize("labels", [[], np.array([]), np.array([], dtype=np.int64)])1862 def test_reindex_preserves_type_if_target_is_empty_list_or_array(self, labels):1863 # GH77741864 index = pd.Index(list("abc"))1865 assert index.reindex(labels)[0].dtype.type == np.object_1866 @pytest.mark.parametrize(1867 "labels,dtype",1868 [1869 (pd.Int64Index([]), np.int64),1870 (pd.Float64Index([]), np.float64),1871 (pd.DatetimeIndex([]), np.datetime64),1872 ],1873 )1874 def test_reindex_doesnt_preserve_type_if_target_is_empty_index(self, labels, dtype):1875 # GH77741876 index = pd.Index(list("abc"))1877 assert index.reindex(labels)[0].dtype.type == dtype1878 def test_reindex_no_type_preserve_target_empty_mi(self):1879 index = pd.Index(list("abc"))1880 result = index.reindex(1881 pd.MultiIndex([pd.Int64Index([]), pd.Float64Index([])], [[], []])1882 )[0]1883 assert result.levels[0].dtype.type == np.int641884 assert result.levels[1].dtype.type == np.float641885 def test_groupby(self):1886 index = Index(range(5))1887 result = index.groupby(np.array([1, 1, 2, 2, 2]))1888 expected = {1: pd.Index([0, 1]), 2: pd.Index([2, 3, 4])}1889 tm.assert_dict_equal(result, expected)1890 @pytest.mark.parametrize(1891 "mi,expected",1892 [1893 (MultiIndex.from_tuples([(1, 2), (4, 5)]), np.array([True, True])),1894 (MultiIndex.from_tuples([(1, 2), (4, 6)]), np.array([True, False])),1895 ],1896 )1897 def test_equals_op_multiindex(self, mi, expected):1898 # GH97851899 # test comparisons of multiindex1900 df = pd.read_csv(StringIO("a,b,c\n1,2,3\n4,5,6"), index_col=[0, 1])1901 result = df.index == mi1902 tm.assert_numpy_array_equal(result, expected)1903 def test_equals_op_multiindex_identify(self):1904 df = pd.read_csv(StringIO("a,b,c\n1,2,3\n4,5,6"), index_col=[0, 1])1905 result = df.index == df.index1906 expected = np.array([True, True])1907 tm.assert_numpy_array_equal(result, expected)1908 @pytest.mark.parametrize(1909 "index",1910 [1911 MultiIndex.from_tuples([(1, 2), (4, 5), (8, 9)]),1912 Index(["foo", "bar", "baz"]),1913 ],1914 )1915 def test_equals_op_mismatched_multiindex_raises(self, index):1916 df = pd.read_csv(StringIO("a,b,c\n1,2,3\n4,5,6"), index_col=[0, 1])1917 with pytest.raises(ValueError, match="Lengths must match"):1918 df.index == index1919 def test_equals_op_index_vs_mi_same_length(self):1920 mi = MultiIndex.from_tuples([(1, 2), (4, 5), (8, 9)])1921 index = Index(["foo", "bar", "baz"])1922 result = mi == index1923 expected = np.array([False, False, False])1924 tm.assert_numpy_array_equal(result, expected)1925 @pytest.mark.parametrize("dt_conv", [pd.to_datetime, pd.to_timedelta])1926 def test_dt_conversion_preserves_name(self, dt_conv):1927 # GH 108751928 index = pd.Index(["01:02:03", "01:02:04"], name="label")1929 assert index.name == dt_conv(index).name1930 @pytest.mark.parametrize(1931 "index,expected",1932 [1933 # ASCII1934 # short1935 (1936 pd.Index(["a", "bb", "ccc"]),1937 """Index(['a', 'bb', 'ccc'], dtype='object')""",1938 ),1939 # multiple lines1940 (1941 pd.Index(["a", "bb", "ccc"] * 10),1942 """\1943Index(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc',1944 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc',1945 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],1946 dtype='object')""",1947 ),1948 # truncated1949 (1950 pd.Index(["a", "bb", "ccc"] * 100),1951 """\1952Index(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a',1953 ...1954 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],1955 dtype='object', length=300)""",1956 ),1957 # Non-ASCII1958 # short1959 (1960 pd.Index(["あ", "いい", "ううう"]),1961 """Index(['あ', 'いい', 'ううう'], dtype='object')""",1962 ),1963 # multiple lines1964 (1965 pd.Index(["あ", "いい", "ううう"] * 10),1966 (1967 "Index(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "1968 "'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',\n"1969 " 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "1970 "'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',\n"1971 " 'あ', 'いい', 'ううう', 'あ', 'いい', "1972 "'ううう'],\n"1973 " dtype='object')"1974 ),1975 ),1976 # truncated1977 (1978 pd.Index(["あ", "いい", "ううう"] * 100),1979 (1980 "Index(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "1981 "'あ', 'いい', 'ううう', 'あ',\n"1982 " ...\n"1983 " 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', "1984 "'ううう', 'あ', 'いい', 'ううう'],\n"1985 " dtype='object', length=300)"1986 ),1987 ),1988 ],1989 )1990 def test_string_index_repr(self, index, expected):1991 result = repr(index)1992 assert result == expected1993 @pytest.mark.parametrize(1994 "index,expected",1995 [1996 # short1997 (1998 pd.Index(["あ", "いい", "ううう"]),1999 ("Index(['あ', 'いい', 'ううう'], dtype='object')"),2000 ),2001 # multiple lines2002 (2003 pd.Index(["あ", "いい", "ううう"] * 10),2004 (2005 "Index(['あ', 'いい', 'ううう', 'あ', 'いい', "2006 "'ううう', 'あ', 'いい', 'ううう',\n"2007 " 'あ', 'いい', 'ううう', 'あ', 'いい', "2008 "'ううう', 'あ', 'いい', 'ううう',\n"2009 " 'あ', 'いい', 'ううう', 'あ', 'いい', "2010 "'ううう', 'あ', 'いい', 'ううう',\n"2011 " 'あ', 'いい', 'ううう'],\n"2012 " dtype='object')"2013 ""2014 ),2015 ),2016 # truncated2017 (2018 pd.Index(["あ", "いい", "ううう"] * 100),2019 (2020 "Index(['あ', 'いい', 'ううう', 'あ', 'いい', "2021 "'ううう', 'あ', 'いい', 'ううう',\n"2022 " 'あ',\n"2023 " ...\n"2024 " 'ううう', 'あ', 'いい', 'ううう', 'あ', "2025 "'いい', 'ううう', 'あ', 'いい',\n"2026 " 'ううう'],\n"2027 " dtype='object', length=300)"2028 ),2029 ),2030 ],2031 )2032 def test_string_index_repr_with_unicode_option(self, index, expected):2033 # Enable Unicode option -----------------------------------------2034 with cf.option_context("display.unicode.east_asian_width", True):2035 result = repr(index)2036 assert result == expected2037 def test_cached_properties_not_settable(self):2038 index = pd.Index([1, 2, 3])2039 with pytest.raises(AttributeError, match="Can't set attribute"):2040 index.is_unique = False2041 def test_get_duplicates_deprecated(self):2042 index = pd.Index([1, 2, 3])2043 with tm.assert_produces_warning(FutureWarning):2044 index.get_duplicates()2045 def test_tab_complete_warning(self, ip):2046 # https://github.com/pandas-dev/pandas/issues/164092047 pytest.importorskip("IPython", minversion="6.0.0")2048 from IPython.core.completer import provisionalcompleter2049 code = "import pandas as pd; idx = pd.Index([1, 2])"2050 ip.run_code(code)2051 with tm.assert_produces_warning(None):2052 with provisionalcompleter("ignore"):2053 list(ip.Completer.completions("idx.", 4))2054 def test_deprecated_contains(self):2055 for index in self.indices.values():2056 with tm.assert_produces_warning(FutureWarning):2057 index.contains(1)2058class TestMixedIntIndex(Base):2059 # Mostly the tests from common.py for which the results differ2060 # in py2 and py3 because ints and strings are uncomparable in py32061 # (GH 13514)2062 _holder = Index2063 def setup_method(self, method):2064 self.indices = dict(mixedIndex=Index([0, "a", 1, "b", 2, "c"]))2065 self.setup_indices()2066 def create_index(self):2067 return self.mixedIndex2068 def test_argsort(self):2069 index = self.create_index()2070 if PY36:2071 with pytest.raises(TypeError, match="'>|<' not supported"):2072 index.argsort()2073 else:2074 with pytest.raises(TypeError, match="unorderable types"):2075 index.argsort()2076 def test_numpy_argsort(self):2077 index = self.create_index()2078 if PY36:2079 with pytest.raises(TypeError, match="'>|<' not supported"):2080 np.argsort(index)2081 else:2082 with pytest.raises(TypeError, match="unorderable types"):2083 np.argsort(index)2084 def test_copy_name(self):2085 # Check that "name" argument passed at initialization is honoured2086 # GH123092087 index = self.create_index()2088 first = index.__class__(index, copy=True, name="mario")2089 second = first.__class__(first, copy=False)2090 # Even though "copy=False", we want a new object.2091 assert first is not second2092 tm.assert_index_equal(first, second)2093 assert first.name == "mario"2094 assert second.name == "mario"2095 s1 = Series(2, index=first)2096 s2 = Series(3, index=second[:-1])2097 s3 = s1 * s22098 assert s3.index.name == "mario"2099 def test_copy_name2(self):2100 # Check that adding a "name" parameter to the copy is honored2101 # GH143022102 index = pd.Index([1, 2], name="MyName")2103 index1 = index.copy()2104 tm.assert_index_equal(index, index1)2105 index2 = index.copy(name="NewName")2106 tm.assert_index_equal(index, index2, check_names=False)2107 assert index.name == "MyName"2108 assert index2.name == "NewName"2109 index3 = index.copy(names=["NewName"])2110 tm.assert_index_equal(index, index3, check_names=False)2111 assert index.name == "MyName"2112 assert index.names == ["MyName"]2113 assert index3.name == "NewName"2114 assert index3.names == ["NewName"]2115 def test_union_base(self):2116 index = self.create_index()2117 first = index[3:]2118 second = index[:5]2119 result = first.union(second)2120 expected = Index([0, 1, 2, "a", "b", "c"])2121 tm.assert_index_equal(result, expected)2122 @pytest.mark.parametrize("klass", [np.array, Series, list])2123 def test_union_different_type_base(self, klass):2124 # GH 101492125 index = self.create_index()2126 first = index[3:]2127 second = index[:5]2128 result = first.union(klass(second.values))2129 assert tm.equalContents(result, index)2130 def test_unique_na(self):2131 idx = pd.Index([2, np.nan, 2, 1], name="my_index")2132 expected = pd.Index([2, np.nan, 1], name="my_index")2133 result = idx.unique()2134 tm.assert_index_equal(result, expected)2135 @pytest.mark.parametrize("sort", [None, False])2136 def test_intersection_base(self, sort):2137 # (same results for py2 and py3 but sortedness not tested elsewhere)2138 index = self.create_index()2139 first = index[:5]2140 second = index[:3]2141 expected = Index([0, 1, "a"]) if sort is None else Index([0, "a", 1])2142 result = first.intersection(second, sort=sort)2143 tm.assert_index_equal(result, expected)2144 @pytest.mark.parametrize("klass", [np.array, Series, list])2145 @pytest.mark.parametrize("sort", [None, False])2146 def test_intersection_different_type_base(self, klass, sort):2147 # GH 101492148 index = self.create_index()2149 first = index[:5]2150 second = index[:3]2151 result = first.intersection(klass(second.values), sort=sort)2152 assert tm.equalContents(result, second)2153 @pytest.mark.parametrize("sort", [None, False])2154 def test_difference_base(self, sort):2155 # (same results for py2 and py3 but sortedness not tested elsewhere)2156 index = self.create_index()2157 first = index[:4]2158 second = index[3:]2159 result = first.difference(second, sort)2160 expected = Index([0, "a", 1])2161 if sort is None:2162 expected = Index(safe_sort(expected))2163 tm.assert_index_equal(result, expected)2164 def test_symmetric_difference(self):2165 # (same results for py2 and py3 but sortedness not tested elsewhere)2166 index = self.create_index()2167 first = index[:4]2168 second = index[3:]2169 result = first.symmetric_difference(second)2170 expected = Index([0, 1, 2, "a", "c"])2171 tm.assert_index_equal(result, expected)2172 def test_logical_compat(self):2173 index = self.create_index()2174 assert index.all() == index.values.all()2175 assert index.any() == index.values.any()2176 @pytest.mark.parametrize("how", ["any", "all"])2177 @pytest.mark.parametrize("dtype", [None, object, "category"])2178 @pytest.mark.parametrize(2179 "vals,expected",2180 [2181 ([1, 2, 3], [1, 2, 3]),2182 ([1.0, 2.0, 3.0], [1.0, 2.0, 3.0]),2183 ([1.0, 2.0, np.nan, 3.0], [1.0, 2.0, 3.0]),2184 (["A", "B", "C"], ["A", "B", "C"]),2185 (["A", np.nan, "B", "C"], ["A", "B", "C"]),2186 ],2187 )2188 def test_dropna(self, how, dtype, vals, expected):2189 # GH 61942190 index = pd.Index(vals, dtype=dtype)2191 result = index.dropna(how=how)2192 expected = pd.Index(expected, dtype=dtype)2193 tm.assert_index_equal(result, expected)2194 @pytest.mark.parametrize("how", ["any", "all"])2195 @pytest.mark.parametrize(2196 "index,expected",2197 [2198 (2199 pd.DatetimeIndex(["2011-01-01", "2011-01-02", "2011-01-03"]),2200 pd.DatetimeIndex(["2011-01-01", "2011-01-02", "2011-01-03"]),2201 ),2202 (2203 pd.DatetimeIndex(["2011-01-01", "2011-01-02", "2011-01-03", pd.NaT]),2204 pd.DatetimeIndex(["2011-01-01", "2011-01-02", "2011-01-03"]),2205 ),2206 (2207 pd.TimedeltaIndex(["1 days", "2 days", "3 days"]),2208 pd.TimedeltaIndex(["1 days", "2 days", "3 days"]),2209 ),2210 (2211 pd.TimedeltaIndex([pd.NaT, "1 days", "2 days", "3 days", pd.NaT]),2212 pd.TimedeltaIndex(["1 days", "2 days", "3 days"]),2213 ),2214 (2215 pd.PeriodIndex(["2012-02", "2012-04", "2012-05"], freq="M"),2216 pd.PeriodIndex(["2012-02", "2012-04", "2012-05"], freq="M"),2217 ),2218 (2219 pd.PeriodIndex(["2012-02", "2012-04", "NaT", "2012-05"], freq="M"),2220 pd.PeriodIndex(["2012-02", "2012-04", "2012-05"], freq="M"),2221 ),2222 ],2223 )2224 def test_dropna_dt_like(self, how, index, expected):2225 result = index.dropna(how=how)2226 tm.assert_index_equal(result, expected)2227 def test_dropna_invalid_how_raises(self):2228 msg = "invalid how option: xxx"2229 with pytest.raises(ValueError, match=msg):2230 pd.Index([1, 2, 3]).dropna(how="xxx")2231 def test_get_combined_index(self):2232 result = _get_combined_index([])2233 expected = Index([])2234 tm.assert_index_equal(result, expected)2235 def test_repeat(self):2236 repeats = 22237 index = pd.Index([1, 2, 3])2238 expected = pd.Index([1, 1, 2, 2, 3, 3])2239 result = index.repeat(repeats)2240 tm.assert_index_equal(result, expected)2241 @pytest.mark.parametrize(2242 "index",2243 [2244 pd.Index([np.nan]),2245 pd.Index([np.nan, 1]),2246 pd.Index([1, 2, np.nan]),2247 pd.Index(["a", "b", np.nan]),2248 pd.to_datetime(["NaT"]),2249 pd.to_datetime(["NaT", "2000-01-01"]),2250 pd.to_datetime(["2000-01-01", "NaT", "2000-01-02"]),2251 pd.to_timedelta(["1 day", "NaT"]),2252 ],2253 )2254 def test_is_monotonic_na(self, index):2255 assert index.is_monotonic_increasing is False2256 assert index.is_monotonic_decreasing is False2257 assert index._is_strictly_monotonic_increasing is False2258 assert index._is_strictly_monotonic_decreasing is False2259 def test_repr_summary(self):2260 with cf.option_context("display.max_seq_items", 10):2261 result = repr(pd.Index(np.arange(1000)))2262 assert len(result) < 2002263 assert "..." in result2264 @pytest.mark.parametrize("klass", [Series, DataFrame])2265 def test_int_name_format(self, klass):2266 index = Index(["a", "b", "c"], name=0)2267 result = klass(list(range(3)), index=index)2268 assert "0" in repr(result)2269 def test_print_unicode_columns(self):2270 df = pd.DataFrame({"\u05d0": [1, 2, 3], "\u05d1": [4, 5, 6], "c": [7, 8, 9]})2271 repr(df.columns) # should not raise UnicodeDecodeError2272 def test_str_to_bytes_raises(self):2273 # GH 264472274 index = Index([str(x) for x in range(10)])2275 msg = "^'str' object cannot be interpreted as an integer$"2276 with pytest.raises(TypeError, match=msg):2277 bytes(index)2278 def test_intersect_str_dates(self):2279 dt_dates = [datetime(2012, 2, 9), datetime(2012, 2, 22)]2280 index1 = Index(dt_dates, dtype=object)2281 index2 = Index(["aa"], dtype=object)2282 result = index2.intersection(index1)2283 expected = Index([], dtype=object)2284 tm.assert_index_equal(result, expected)2285class TestIndexUtils:2286 @pytest.mark.parametrize(2287 "data, names, expected",2288 [2289 ([[1, 2, 3]], None, Index([1, 2, 3])),2290 ([[1, 2, 3]], ["name"], Index([1, 2, 3], name="name")),2291 (2292 [["a", "a"], ["c", "d"]],2293 None,2294 MultiIndex([["a"], ["c", "d"]], [[0, 0], [0, 1]]),2295 ),2296 (2297 [["a", "a"], ["c", "d"]],2298 ["L1", "L2"],2299 MultiIndex([["a"], ["c", "d"]], [[0, 0], [0, 1]], names=["L1", "L2"]),2300 ),2301 ],2302 )2303 def test_ensure_index_from_sequences(self, data, names, expected):2304 result = ensure_index_from_sequences(data, names)2305 tm.assert_index_equal(result, expected)2306 def test_ensure_index_mixed_closed_intervals(self):2307 # GH271722308 intervals = [2309 pd.Interval(0, 1, closed="left"),2310 pd.Interval(1, 2, closed="right"),2311 pd.Interval(2, 3, closed="neither"),2312 pd.Interval(3, 4, closed="both"),2313 ]2314 result = ensure_index(intervals)2315 expected = Index(intervals, dtype=object)2316 tm.assert_index_equal(result, expected)2317@pytest.mark.parametrize(2318 "opname",2319 [2320 "eq",2321 "ne",2322 "le",2323 "lt",2324 "ge",2325 "gt",2326 "add",2327 "radd",2328 "sub",...

Full Screen

Full Screen

test_alter_axes.py

Source:test_alter_axes.py Github

copy

Full Screen

...30 df.index = idx31 tm.assert_index_equal(df.index, idx)32 with pytest.raises(ValueError, match="Length mismatch"):33 df.index = idx[::2]34 def test_set_index(self, float_string_frame):35 df = float_string_frame36 idx = Index(np.arange(len(df))[::-1])37 df = df.set_index(idx)38 tm.assert_index_equal(df.index, idx)39 with pytest.raises(ValueError, match="Length mismatch"):40 df.set_index(idx[::2])41 def test_set_index_cast(self):42 # issue casting an index then set_index43 df = DataFrame(44 {"A": [1.1, 2.2, 3.3], "B": [5.0, 6.1, 7.2]}, index=[2010, 2011, 2012]45 )46 df2 = df.set_index(df.index.astype(np.int32))47 tm.assert_frame_equal(df, df2)48 # A has duplicate values, C does not49 @pytest.mark.parametrize("keys", ["A", "C", ["A", "B"], ("tuple", "as", "label")])50 @pytest.mark.parametrize("inplace", [True, False])51 @pytest.mark.parametrize("drop", [True, False])52 def test_set_index_drop_inplace(self, frame_of_index_cols, drop, inplace, keys):53 df = frame_of_index_cols54 if isinstance(keys, list):55 idx = MultiIndex.from_arrays([df[x] for x in keys], names=keys)56 else:57 idx = Index(df[keys], name=keys)58 expected = df.drop(keys, axis=1) if drop else df59 expected.index = idx60 if inplace:61 result = df.copy()62 result.set_index(keys, drop=drop, inplace=True)63 else:64 result = df.set_index(keys, drop=drop)65 tm.assert_frame_equal(result, expected)66 # A has duplicate values, C does not67 @pytest.mark.parametrize("keys", ["A", "C", ["A", "B"], ("tuple", "as", "label")])68 @pytest.mark.parametrize("drop", [True, False])69 def test_set_index_append(self, frame_of_index_cols, drop, keys):70 df = frame_of_index_cols71 keys = keys if isinstance(keys, list) else [keys]72 idx = MultiIndex.from_arrays(73 [df.index] + [df[x] for x in keys], names=[None] + keys74 )75 expected = df.drop(keys, axis=1) if drop else df.copy()76 expected.index = idx77 result = df.set_index(keys, drop=drop, append=True)78 tm.assert_frame_equal(result, expected)79 # A has duplicate values, C does not80 @pytest.mark.parametrize("keys", ["A", "C", ["A", "B"], ("tuple", "as", "label")])81 @pytest.mark.parametrize("drop", [True, False])82 def test_set_index_append_to_multiindex(self, frame_of_index_cols, drop, keys):83 # append to existing multiindex84 df = frame_of_index_cols.set_index(["D"], drop=drop, append=True)85 keys = keys if isinstance(keys, list) else [keys]86 expected = frame_of_index_cols.set_index(["D"] + keys, drop=drop, append=True)87 result = df.set_index(keys, drop=drop, append=True)88 tm.assert_frame_equal(result, expected)89 def test_set_index_after_mutation(self):90 # GH159091 df = DataFrame({"val": [0, 1, 2], "key": ["a", "b", "c"]})92 expected = DataFrame({"val": [1, 2]}, Index(["b", "c"], name="key"))93 df2 = df.loc[df.index.map(lambda indx: indx >= 1)]94 result = df2.set_index("key")95 tm.assert_frame_equal(result, expected)96 # MultiIndex constructor does not work directly on Series -> lambda97 # Add list-of-list constructor because list is ambiguous -> lambda98 # also test index name if append=True (name is duplicate here for B)99 @pytest.mark.parametrize(100 "box",101 [102 Series,103 Index,104 np.array,105 list,106 lambda x: [list(x)],107 lambda x: MultiIndex.from_arrays([x]),108 ],109 )110 @pytest.mark.parametrize(111 "append, index_name", [(True, None), (True, "B"), (True, "test"), (False, None)]112 )113 @pytest.mark.parametrize("drop", [True, False])114 def test_set_index_pass_single_array(115 self, frame_of_index_cols, drop, append, index_name, box116 ):117 df = frame_of_index_cols118 df.index.name = index_name119 key = box(df["B"])120 if box == list:121 # list of strings gets interpreted as list of keys122 msg = "['one', 'two', 'three', 'one', 'two']"123 with pytest.raises(KeyError, match=msg):124 df.set_index(key, drop=drop, append=append)125 else:126 # np.array/list-of-list "forget" the name of B127 name_mi = getattr(key, "names", None)128 name = [getattr(key, "name", None)] if name_mi is None else name_mi129 result = df.set_index(key, drop=drop, append=append)130 # only valid column keys are dropped131 # since B is always passed as array above, nothing is dropped132 expected = df.set_index(["B"], drop=False, append=append)133 expected.index.names = [index_name] + name if append else name134 tm.assert_frame_equal(result, expected)135 # MultiIndex constructor does not work directly on Series -> lambda136 # also test index name if append=True (name is duplicate here for A & B)137 @pytest.mark.parametrize(138 "box", [Series, Index, np.array, list, lambda x: MultiIndex.from_arrays([x])]139 )140 @pytest.mark.parametrize(141 "append, index_name",142 [(True, None), (True, "A"), (True, "B"), (True, "test"), (False, None)],143 )144 @pytest.mark.parametrize("drop", [True, False])145 def test_set_index_pass_arrays(146 self, frame_of_index_cols, drop, append, index_name, box147 ):148 df = frame_of_index_cols149 df.index.name = index_name150 keys = ["A", box(df["B"])]151 # np.array/list "forget" the name of B152 names = ["A", None if box in [np.array, list, tuple, iter] else "B"]153 result = df.set_index(keys, drop=drop, append=append)154 # only valid column keys are dropped155 # since B is always passed as array above, only A is dropped, if at all156 expected = df.set_index(["A", "B"], drop=False, append=append)157 expected = expected.drop("A", axis=1) if drop else expected158 expected.index.names = [index_name] + names if append else names159 tm.assert_frame_equal(result, expected)160 # MultiIndex constructor does not work directly on Series -> lambda161 # We also emulate a "constructor" for the label -> lambda162 # also test index name if append=True (name is duplicate here for A)163 @pytest.mark.parametrize(164 "box2",165 [166 Series,167 Index,168 np.array,169 list,170 iter,171 lambda x: MultiIndex.from_arrays([x]),172 lambda x: x.name,173 ],174 )175 @pytest.mark.parametrize(176 "box1",177 [178 Series,179 Index,180 np.array,181 list,182 iter,183 lambda x: MultiIndex.from_arrays([x]),184 lambda x: x.name,185 ],186 )187 @pytest.mark.parametrize(188 "append, index_name", [(True, None), (True, "A"), (True, "test"), (False, None)]189 )190 @pytest.mark.parametrize("drop", [True, False])191 def test_set_index_pass_arrays_duplicate(192 self, frame_of_index_cols, drop, append, index_name, box1, box2193 ):194 df = frame_of_index_cols195 df.index.name = index_name196 keys = [box1(df["A"]), box2(df["A"])]197 result = df.set_index(keys, drop=drop, append=append)198 # if either box is iter, it has been consumed; re-read199 keys = [box1(df["A"]), box2(df["A"])]200 # need to adapt first drop for case that both keys are 'A' --201 # cannot drop the same column twice;202 # use "is" because == would give ambiguous Boolean error for containers203 first_drop = (204 False if (keys[0] is "A" and keys[1] is "A") else drop # noqa: F632205 )206 # to test against already-tested behaviour, we add sequentially,207 # hence second append always True; must wrap keys in list, otherwise208 # box = list would be interpreted as keys209 expected = df.set_index([keys[0]], drop=first_drop, append=append)210 expected = expected.set_index([keys[1]], drop=drop, append=True)211 tm.assert_frame_equal(result, expected)212 @pytest.mark.parametrize("append", [True, False])213 @pytest.mark.parametrize("drop", [True, False])214 def test_set_index_pass_multiindex(self, frame_of_index_cols, drop, append):215 df = frame_of_index_cols216 keys = MultiIndex.from_arrays([df["A"], df["B"]], names=["A", "B"])217 result = df.set_index(keys, drop=drop, append=append)218 # setting with a MultiIndex will never drop columns219 expected = df.set_index(["A", "B"], drop=False, append=append)220 tm.assert_frame_equal(result, expected)221 def test_set_index_verify_integrity(self, frame_of_index_cols):222 df = frame_of_index_cols223 with pytest.raises(ValueError, match="Index has duplicate keys"):224 df.set_index("A", verify_integrity=True)225 # with MultiIndex226 with pytest.raises(ValueError, match="Index has duplicate keys"):227 df.set_index([df["A"], df["A"]], verify_integrity=True)228 @pytest.mark.parametrize("append", [True, False])229 @pytest.mark.parametrize("drop", [True, False])230 def test_set_index_raise_keys(self, frame_of_index_cols, drop, append):231 df = frame_of_index_cols232 with pytest.raises(KeyError, match="['foo', 'bar', 'baz']"):233 # column names are A-E, as well as one tuple234 df.set_index(["foo", "bar", "baz"], drop=drop, append=append)235 # non-existent key in list with arrays236 with pytest.raises(KeyError, match="X"):237 df.set_index([df["A"], df["B"], "X"], drop=drop, append=append)238 msg = "[('foo', 'foo', 'foo', 'bar', 'bar')]"239 # tuples always raise KeyError240 with pytest.raises(KeyError, match=msg):241 df.set_index(tuple(df["A"]), drop=drop, append=append)242 # also within a list243 with pytest.raises(KeyError, match=msg):244 df.set_index(["A", df["A"], tuple(df["A"])], drop=drop, append=append)245 @pytest.mark.parametrize("append", [True, False])246 @pytest.mark.parametrize("drop", [True, False])247 @pytest.mark.parametrize("box", [set], ids=["set"])248 def test_set_index_raise_on_type(self, frame_of_index_cols, box, drop, append):249 df = frame_of_index_cols250 msg = 'The parameter "keys" may be a column key, .*'251 # forbidden type, e.g. set252 with pytest.raises(TypeError, match=msg):253 df.set_index(box(df["A"]), drop=drop, append=append)254 # forbidden type in list, e.g. set255 with pytest.raises(TypeError, match=msg):256 df.set_index(["A", df["A"], box(df["A"])], drop=drop, append=append)257 # MultiIndex constructor does not work directly on Series -> lambda258 @pytest.mark.parametrize(259 "box",260 [Series, Index, np.array, iter, lambda x: MultiIndex.from_arrays([x])],261 ids=["Series", "Index", "np.array", "iter", "MultiIndex"],262 )263 @pytest.mark.parametrize("length", [4, 6], ids=["too_short", "too_long"])264 @pytest.mark.parametrize("append", [True, False])265 @pytest.mark.parametrize("drop", [True, False])266 def test_set_index_raise_on_len(267 self, frame_of_index_cols, box, length, drop, append268 ):269 # GH 24984270 df = frame_of_index_cols # has length 5271 values = np.random.randint(0, 10, (length,))272 msg = "Length mismatch: Expected 5 rows, received array of length.*"273 # wrong length directly274 with pytest.raises(ValueError, match=msg):275 df.set_index(box(values), drop=drop, append=append)276 # wrong length in list277 with pytest.raises(ValueError, match=msg):278 df.set_index(["A", df.A, box(values)], drop=drop, append=append)279 def test_set_index_custom_label_type(self):280 # GH 24969281 class Thing:282 def __init__(self, name, color):283 self.name = name284 self.color = color285 def __str__(self):286 return "<Thing {self.name!r}>".format(self=self)287 # necessary for pretty KeyError288 __repr__ = __str__289 thing1 = Thing("One", "red")290 thing2 = Thing("Two", "blue")291 df = DataFrame({thing1: [0, 1], thing2: [2, 3]})292 expected = DataFrame({thing1: [0, 1]}, index=Index([2, 3], name=thing2))293 # use custom label directly294 result = df.set_index(thing2)295 tm.assert_frame_equal(result, expected)296 # custom label wrapped in list297 result = df.set_index([thing2])298 tm.assert_frame_equal(result, expected)299 # missing key300 thing3 = Thing("Three", "pink")301 msg = "<Thing 'Three'>"302 with pytest.raises(KeyError, match=msg):303 # missing label directly304 df.set_index(thing3)305 with pytest.raises(KeyError, match=msg):306 # missing label in list307 df.set_index([thing3])308 def test_set_index_custom_label_hashable_iterable(self):309 # GH 24969310 # actual example discussed in GH 24984 was e.g. for shapely.geometry311 # objects (e.g. a collection of Points) that can be both hashable and312 # iterable; using frozenset as a stand-in for testing here313 class Thing(frozenset):314 # need to stabilize repr for KeyError (due to random order in sets)315 def __repr__(self):316 tmp = sorted(list(self))317 # double curly brace prints one brace in format string318 return "frozenset({{{}}})".format(", ".join(map(repr, tmp)))319 thing1 = Thing(["One", "red"])320 thing2 = Thing(["Two", "blue"])321 df = DataFrame({thing1: [0, 1], thing2: [2, 3]})322 expected = DataFrame({thing1: [0, 1]}, index=Index([2, 3], name=thing2))323 # use custom label directly324 result = df.set_index(thing2)325 tm.assert_frame_equal(result, expected)326 # custom label wrapped in list327 result = df.set_index([thing2])328 tm.assert_frame_equal(result, expected)329 # missing key330 thing3 = Thing(["Three", "pink"])331 msg = r"frozenset\(\{'Three', 'pink'\}\)"332 with pytest.raises(KeyError, match=msg):333 # missing label directly334 df.set_index(thing3)335 with pytest.raises(KeyError, match=msg):336 # missing label in list337 df.set_index([thing3])338 def test_set_index_custom_label_type_raises(self):339 # GH 24969340 # purposefully inherit from something unhashable341 class Thing(set):342 def __init__(self, name, color):343 self.name = name344 self.color = color345 def __str__(self):346 return "<Thing {self.name!r}>".format(self=self)347 thing1 = Thing("One", "red")348 thing2 = Thing("Two", "blue")349 df = DataFrame([[0, 2], [1, 3]], columns=[thing1, thing2])350 msg = 'The parameter "keys" may be a column key, .*'351 with pytest.raises(TypeError, match=msg):352 # use custom label directly353 df.set_index(thing2)354 with pytest.raises(TypeError, match=msg):355 # custom label wrapped in list356 df.set_index([thing2])357 def test_construction_with_categorical_index(self):358 ci = tm.makeCategoricalIndex(10)359 ci.name = "B"360 # with Categorical361 df = DataFrame({"A": np.random.randn(10), "B": ci.values})362 idf = df.set_index("B")363 tm.assert_index_equal(idf.index, ci)364 # from a CategoricalIndex365 df = DataFrame({"A": np.random.randn(10), "B": ci})366 idf = df.set_index("B")367 tm.assert_index_equal(idf.index, ci)368 # round-trip369 idf = idf.reset_index().set_index("B")370 tm.assert_index_equal(idf.index, ci)371 def test_set_index_cast_datetimeindex(self):372 df = DataFrame(373 {374 "A": [datetime(2000, 1, 1) + timedelta(i) for i in range(1000)],375 "B": np.random.randn(1000),376 }377 )378 idf = df.set_index("A")379 assert isinstance(idf.index, DatetimeIndex)380 def test_convert_dti_to_series(self):381 # don't cast a DatetimeIndex WITH a tz, leave as object382 # GH 6032383 idx = DatetimeIndex(384 to_datetime(["2013-1-1 13:00", "2013-1-2 14:00"]), name="B"385 ).tz_localize("US/Pacific")386 df = DataFrame(np.random.randn(2, 1), columns=["A"])387 expected = Series(388 np.array(389 [390 Timestamp("2013-01-01 13:00:00-0800", tz="US/Pacific"),391 Timestamp("2013-01-02 14:00:00-0800", tz="US/Pacific"),392 ],393 dtype="object",394 ),395 name="B",396 )397 # convert index to series398 result = Series(idx)399 tm.assert_series_equal(result, expected)400 # assign to frame401 df["B"] = idx402 result = df["B"]403 tm.assert_series_equal(result, expected)404 # convert to series while keeping the timezone405 result = idx.to_series(keep_tz=True, index=[0, 1])406 tm.assert_series_equal(result, expected)407 # convert to utc408 with tm.assert_produces_warning(FutureWarning):409 df["B"] = idx.to_series(keep_tz=False, index=[0, 1])410 result = df["B"]411 comp = Series(DatetimeIndex(expected.values).tz_localize(None), name="B")412 tm.assert_series_equal(result, comp)413 with tm.assert_produces_warning(FutureWarning) as m:414 result = idx.to_series(index=[0, 1])415 tm.assert_series_equal(result, expected.dt.tz_convert(None))416 msg = (417 "The default of the 'keep_tz' keyword in "418 "DatetimeIndex.to_series will change to True in a future "419 "release."420 )421 assert msg in str(m[0].message)422 with tm.assert_produces_warning(FutureWarning):423 result = idx.to_series(keep_tz=False, index=[0, 1])424 tm.assert_series_equal(result, expected.dt.tz_convert(None))425 # list of datetimes with a tz426 df["B"] = idx.to_pydatetime()427 result = df["B"]428 tm.assert_series_equal(result, expected)429 # GH 6785430 # set the index manually431 import pytz432 df = DataFrame([{"ts": datetime(2014, 4, 1, tzinfo=pytz.utc), "foo": 1}])433 expected = df.set_index("ts")434 df.index = df["ts"]435 df.pop("ts")436 tm.assert_frame_equal(df, expected)437 def test_reset_index_tz(self, tz_aware_fixture):438 # GH 3950439 # reset_index with single level440 tz = tz_aware_fixture441 idx = date_range("1/1/2011", periods=5, freq="D", tz=tz, name="idx")442 df = DataFrame({"a": range(5), "b": ["A", "B", "C", "D", "E"]}, index=idx)443 expected = DataFrame(444 {445 "idx": [446 datetime(2011, 1, 1),447 datetime(2011, 1, 2),448 datetime(2011, 1, 3),449 datetime(2011, 1, 4),450 datetime(2011, 1, 5),451 ],452 "a": range(5),453 "b": ["A", "B", "C", "D", "E"],454 },455 columns=["idx", "a", "b"],456 )457 expected["idx"] = expected["idx"].apply(lambda d: Timestamp(d, tz=tz))458 tm.assert_frame_equal(df.reset_index(), expected)459 def test_set_index_timezone(self):460 # GH 12358461 # tz-aware Series should retain the tz462 idx = to_datetime(["2014-01-01 10:10:10"], utc=True).tz_convert("Europe/Rome")463 df = DataFrame({"A": idx})464 assert df.set_index(idx).index[0].hour == 11465 assert DatetimeIndex(Series(df.A))[0].hour == 11466 assert df.set_index(df.A).index[0].hour == 11467 def test_set_index_dst(self):468 di = date_range("2006-10-29 00:00:00", periods=3, freq="H", tz="US/Pacific")469 df = DataFrame(data={"a": [0, 1, 2], "b": [3, 4, 5]}, index=di).reset_index()470 # single level471 res = df.set_index("index")472 exp = DataFrame(473 data={"a": [0, 1, 2], "b": [3, 4, 5]}, index=Index(di, name="index")474 )475 tm.assert_frame_equal(res, exp)476 # GH 12920477 res = df.set_index(["index", "a"])478 exp_index = MultiIndex.from_arrays([di, [0, 1, 2]], names=["index", "a"])479 exp = DataFrame({"b": [3, 4, 5]}, index=exp_index)480 tm.assert_frame_equal(res, exp)481 def test_reset_index_with_intervals(self):482 idx = IntervalIndex.from_breaks(np.arange(11), name="x")483 original = DataFrame({"x": idx, "y": np.arange(10)})[["x", "y"]]484 result = original.set_index("x")485 expected = DataFrame({"y": np.arange(10)}, index=idx)486 tm.assert_frame_equal(result, expected)487 result2 = result.reset_index()488 tm.assert_frame_equal(result2, original)489 def test_set_index_multiindexcolumns(self):490 columns = MultiIndex.from_tuples([("foo", 1), ("foo", 2), ("bar", 1)])491 df = DataFrame(np.random.randn(3, 3), columns=columns)492 result = df.set_index(df.columns[0])493 expected = df.iloc[:, 1:]494 expected.index = df.iloc[:, 0].values495 expected.index.names = [df.columns[0]]496 tm.assert_frame_equal(result, expected)497 def test_set_index_empty_column(self):498 # GH 1971499 df = DataFrame(500 [501 {"a": 1, "p": 0},502 {"a": 2, "m": 10},503 {"a": 3, "m": 11, "p": 20},504 {"a": 4, "m": 12, "p": 21},505 ],506 columns=("a", "m", "p", "x"),507 )508 result = df.set_index(["a", "x"])509 expected = df[["m", "p"]]510 expected.index = MultiIndex.from_arrays([df["a"], df["x"]], names=["a", "x"])511 tm.assert_frame_equal(result, expected)512 def test_set_columns(self, float_string_frame):513 cols = Index(np.arange(len(float_string_frame.columns)))514 float_string_frame.columns = cols515 with pytest.raises(ValueError, match="Length mismatch"):516 float_string_frame.columns = cols[::2]517 def test_dti_set_index_reindex(self):518 # GH 6631519 df = DataFrame(np.random.random(6))520 idx1 = date_range("2011/01/01", periods=6, freq="M", tz="US/Eastern")521 idx2 = date_range("2013", periods=6, freq="A", tz="Asia/Tokyo")522 df = df.set_index(idx1)523 tm.assert_index_equal(df.index, idx1)524 df = df.reindex(idx2)525 tm.assert_index_equal(df.index, idx2)526 # GH 11314527 # with tz528 index = date_range(529 datetime(2015, 10, 1), datetime(2015, 10, 1, 23), freq="H", tz="US/Eastern"530 )531 df = DataFrame(np.random.randn(24, 1), columns=["a"], index=index)532 new_index = date_range(533 datetime(2015, 10, 2), datetime(2015, 10, 2, 23), freq="H", tz="US/Eastern"534 )535 result = df.set_index(new_index)536 assert result.index.freq == index.freq537 # Renaming538 def test_rename(self, float_frame):539 mapping = {"A": "a", "B": "b", "C": "c", "D": "d"}540 renamed = float_frame.rename(columns=mapping)541 renamed2 = float_frame.rename(columns=str.lower)542 tm.assert_frame_equal(renamed, renamed2)543 tm.assert_frame_equal(544 renamed2.rename(columns=str.upper), float_frame, check_names=False545 )546 # index547 data = {"A": {"foo": 0, "bar": 1}}548 # gets sorted alphabetical549 df = DataFrame(data)550 renamed = df.rename(index={"foo": "bar", "bar": "foo"})551 tm.assert_index_equal(renamed.index, Index(["foo", "bar"]))552 renamed = df.rename(index=str.upper)553 tm.assert_index_equal(renamed.index, Index(["BAR", "FOO"]))554 # have to pass something555 with pytest.raises(TypeError, match="must pass an index to rename"):556 float_frame.rename()557 # partial columns558 renamed = float_frame.rename(columns={"C": "foo", "D": "bar"})559 tm.assert_index_equal(renamed.columns, Index(["A", "B", "foo", "bar"]))560 # other axis561 renamed = float_frame.T.rename(index={"C": "foo", "D": "bar"})562 tm.assert_index_equal(renamed.index, Index(["A", "B", "foo", "bar"]))563 # index with name564 index = Index(["foo", "bar"], name="name")565 renamer = DataFrame(data, index=index)566 renamed = renamer.rename(index={"foo": "bar", "bar": "foo"})567 tm.assert_index_equal(renamed.index, Index(["bar", "foo"], name="name"))568 assert renamed.index.name == renamer.index.name569 def test_rename_axis_inplace(self, float_frame):570 # GH 15704571 expected = float_frame.rename_axis("foo")572 result = float_frame.copy()573 no_return = result.rename_axis("foo", inplace=True)574 assert no_return is None575 tm.assert_frame_equal(result, expected)576 expected = float_frame.rename_axis("bar", axis=1)577 result = float_frame.copy()578 no_return = result.rename_axis("bar", axis=1, inplace=True)579 assert no_return is None580 tm.assert_frame_equal(result, expected)581 def test_rename_axis_raises(self):582 # https://github.com/pandas-dev/pandas/issues/17833583 df = DataFrame({"A": [1, 2], "B": [1, 2]})584 with pytest.raises(ValueError, match="Use `.rename`"):585 df.rename_axis(id, axis=0)586 with pytest.raises(ValueError, match="Use `.rename`"):587 df.rename_axis({0: 10, 1: 20}, axis=0)588 with pytest.raises(ValueError, match="Use `.rename`"):589 df.rename_axis(id, axis=1)590 with pytest.raises(ValueError, match="Use `.rename`"):591 df["A"].rename_axis(id)592 def test_rename_axis_mapper(self):593 # GH 19978594 mi = MultiIndex.from_product([["a", "b", "c"], [1, 2]], names=["ll", "nn"])595 df = DataFrame(596 {"x": [i for i in range(len(mi))], "y": [i * 10 for i in range(len(mi))]},597 index=mi,598 )599 # Test for rename of the Index object of columns600 result = df.rename_axis("cols", axis=1)601 tm.assert_index_equal(result.columns, Index(["x", "y"], name="cols"))602 # Test for rename of the Index object of columns using dict603 result = result.rename_axis(columns={"cols": "new"}, axis=1)604 tm.assert_index_equal(result.columns, Index(["x", "y"], name="new"))605 # Test for renaming index using dict606 result = df.rename_axis(index={"ll": "foo"})607 assert result.index.names == ["foo", "nn"]608 # Test for renaming index using a function609 result = df.rename_axis(index=str.upper, axis=0)610 assert result.index.names == ["LL", "NN"]611 # Test for renaming index providing complete list612 result = df.rename_axis(index=["foo", "goo"])613 assert result.index.names == ["foo", "goo"]614 # Test for changing index and columns at same time615 sdf = df.reset_index().set_index("nn").drop(columns=["ll", "y"])616 result = sdf.rename_axis(index="foo", columns="meh")617 assert result.index.name == "foo"618 assert result.columns.name == "meh"619 # Test different error cases620 with pytest.raises(TypeError, match="Must pass"):621 df.rename_axis(index="wrong")622 with pytest.raises(ValueError, match="Length of names"):623 df.rename_axis(index=["wrong"])624 with pytest.raises(TypeError, match="bogus"):625 df.rename_axis(bogus=None)626 @pytest.mark.parametrize(627 "kwargs, rename_index, rename_columns",628 [629 ({"mapper": None, "axis": 0}, True, False),630 ({"mapper": None, "axis": 1}, False, True),631 ({"index": None}, True, False),632 ({"columns": None}, False, True),633 ({"index": None, "columns": None}, True, True),634 ({}, False, False),635 ],636 )637 def test_rename_axis_none(self, kwargs, rename_index, rename_columns):638 # GH 25034639 index = Index(list("abc"), name="foo")640 columns = Index(["col1", "col2"], name="bar")641 data = np.arange(6).reshape(3, 2)642 df = DataFrame(data, index, columns)643 result = df.rename_axis(**kwargs)644 expected_index = index.rename(None) if rename_index else index645 expected_columns = columns.rename(None) if rename_columns else columns646 expected = DataFrame(data, expected_index, expected_columns)647 tm.assert_frame_equal(result, expected)648 def test_rename_multiindex(self):649 tuples_index = [("foo1", "bar1"), ("foo2", "bar2")]650 tuples_columns = [("fizz1", "buzz1"), ("fizz2", "buzz2")]651 index = MultiIndex.from_tuples(tuples_index, names=["foo", "bar"])652 columns = MultiIndex.from_tuples(tuples_columns, names=["fizz", "buzz"])653 df = DataFrame([(0, 0), (1, 1)], index=index, columns=columns)654 #655 # without specifying level -> across all levels656 renamed = df.rename(657 index={"foo1": "foo3", "bar2": "bar3"},658 columns={"fizz1": "fizz3", "buzz2": "buzz3"},659 )660 new_index = MultiIndex.from_tuples(661 [("foo3", "bar1"), ("foo2", "bar3")], names=["foo", "bar"]662 )663 new_columns = MultiIndex.from_tuples(664 [("fizz3", "buzz1"), ("fizz2", "buzz3")], names=["fizz", "buzz"]665 )666 tm.assert_index_equal(renamed.index, new_index)667 tm.assert_index_equal(renamed.columns, new_columns)668 assert renamed.index.names == df.index.names669 assert renamed.columns.names == df.columns.names670 #671 # with specifying a level (GH13766)672 # dict673 new_columns = MultiIndex.from_tuples(674 [("fizz3", "buzz1"), ("fizz2", "buzz2")], names=["fizz", "buzz"]675 )676 renamed = df.rename(columns={"fizz1": "fizz3", "buzz2": "buzz3"}, level=0)677 tm.assert_index_equal(renamed.columns, new_columns)678 renamed = df.rename(columns={"fizz1": "fizz3", "buzz2": "buzz3"}, level="fizz")679 tm.assert_index_equal(renamed.columns, new_columns)680 new_columns = MultiIndex.from_tuples(681 [("fizz1", "buzz1"), ("fizz2", "buzz3")], names=["fizz", "buzz"]682 )683 renamed = df.rename(columns={"fizz1": "fizz3", "buzz2": "buzz3"}, level=1)684 tm.assert_index_equal(renamed.columns, new_columns)685 renamed = df.rename(columns={"fizz1": "fizz3", "buzz2": "buzz3"}, level="buzz")686 tm.assert_index_equal(renamed.columns, new_columns)687 # function688 func = str.upper689 new_columns = MultiIndex.from_tuples(690 [("FIZZ1", "buzz1"), ("FIZZ2", "buzz2")], names=["fizz", "buzz"]691 )692 renamed = df.rename(columns=func, level=0)693 tm.assert_index_equal(renamed.columns, new_columns)694 renamed = df.rename(columns=func, level="fizz")695 tm.assert_index_equal(renamed.columns, new_columns)696 new_columns = MultiIndex.from_tuples(697 [("fizz1", "BUZZ1"), ("fizz2", "BUZZ2")], names=["fizz", "buzz"]698 )699 renamed = df.rename(columns=func, level=1)700 tm.assert_index_equal(renamed.columns, new_columns)701 renamed = df.rename(columns=func, level="buzz")702 tm.assert_index_equal(renamed.columns, new_columns)703 # index704 new_index = MultiIndex.from_tuples(705 [("foo3", "bar1"), ("foo2", "bar2")], names=["foo", "bar"]706 )707 renamed = df.rename(index={"foo1": "foo3", "bar2": "bar3"}, level=0)708 tm.assert_index_equal(renamed.index, new_index)709 def test_rename_nocopy(self, float_frame):710 renamed = float_frame.rename(columns={"C": "foo"}, copy=False)711 renamed["foo"] = 1.0712 assert (float_frame["C"] == 1.0).all()713 def test_rename_inplace(self, float_frame):714 float_frame.rename(columns={"C": "foo"})715 assert "C" in float_frame716 assert "foo" not in float_frame717 c_id = id(float_frame["C"])718 float_frame = float_frame.copy()719 float_frame.rename(columns={"C": "foo"}, inplace=True)720 assert "C" not in float_frame721 assert "foo" in float_frame722 assert id(float_frame["foo"]) != c_id723 def test_rename_bug(self):724 # GH 5344725 # rename set ref_locs, and set_index was not resetting726 df = DataFrame({0: ["foo", "bar"], 1: ["bah", "bas"], 2: [1, 2]})727 df = df.rename(columns={0: "a"})728 df = df.rename(columns={1: "b"})729 df = df.set_index(["a", "b"])730 df.columns = ["2001-01-01"]731 expected = DataFrame(732 [[1], [2]],733 index=MultiIndex.from_tuples(734 [("foo", "bah"), ("bar", "bas")], names=["a", "b"]735 ),736 columns=["2001-01-01"],737 )738 tm.assert_frame_equal(df, expected)739 def test_rename_bug2(self):740 # GH 19497741 # rename was changing Index to MultiIndex if Index contained tuples742 df = DataFrame(data=np.arange(3), index=[(0, 0), (1, 1), (2, 2)], columns=["a"])743 df = df.rename({(1, 1): (5, 4)}, axis="index")744 expected = DataFrame(745 data=np.arange(3), index=[(0, 0), (5, 4), (2, 2)], columns=["a"]746 )747 tm.assert_frame_equal(df, expected)748 def test_rename_errors_raises(self):749 df = DataFrame(columns=["A", "B", "C", "D"])750 with pytest.raises(KeyError, match="'E'] not found in axis"):751 df.rename(columns={"A": "a", "E": "e"}, errors="raise")752 @pytest.mark.parametrize(753 "mapper, errors, expected_columns",754 [755 ({"A": "a", "E": "e"}, "ignore", ["a", "B", "C", "D"]),756 ({"A": "a"}, "raise", ["a", "B", "C", "D"]),757 (str.lower, "raise", ["a", "b", "c", "d"]),758 ],759 )760 def test_rename_errors(self, mapper, errors, expected_columns):761 # GH 13473762 # rename now works with errors parameter763 df = DataFrame(columns=["A", "B", "C", "D"])764 result = df.rename(columns=mapper, errors=errors)765 expected = DataFrame(columns=expected_columns)766 tm.assert_frame_equal(result, expected)767 def test_reorder_levels(self):768 index = MultiIndex(769 levels=[["bar"], ["one", "two", "three"], [0, 1]],770 codes=[[0, 0, 0, 0, 0, 0], [0, 1, 2, 0, 1, 2], [0, 1, 0, 1, 0, 1]],771 names=["L0", "L1", "L2"],772 )773 df = DataFrame({"A": np.arange(6), "B": np.arange(6)}, index=index)774 # no change, position775 result = df.reorder_levels([0, 1, 2])776 tm.assert_frame_equal(df, result)777 # no change, labels778 result = df.reorder_levels(["L0", "L1", "L2"])779 tm.assert_frame_equal(df, result)780 # rotate, position781 result = df.reorder_levels([1, 2, 0])782 e_idx = MultiIndex(783 levels=[["one", "two", "three"], [0, 1], ["bar"]],784 codes=[[0, 1, 2, 0, 1, 2], [0, 1, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0]],785 names=["L1", "L2", "L0"],786 )787 expected = DataFrame({"A": np.arange(6), "B": np.arange(6)}, index=e_idx)788 tm.assert_frame_equal(result, expected)789 result = df.reorder_levels([0, 0, 0])790 e_idx = MultiIndex(791 levels=[["bar"], ["bar"], ["bar"]],792 codes=[[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]],793 names=["L0", "L0", "L0"],794 )795 expected = DataFrame({"A": np.arange(6), "B": np.arange(6)}, index=e_idx)796 tm.assert_frame_equal(result, expected)797 result = df.reorder_levels(["L0", "L0", "L0"])798 tm.assert_frame_equal(result, expected)799 def test_reset_index(self, float_frame):800 stacked = float_frame.stack()[::2]801 stacked = DataFrame({"foo": stacked, "bar": stacked})802 names = ["first", "second"]803 stacked.index.names = names804 deleveled = stacked.reset_index()805 for i, (lev, level_codes) in enumerate(806 zip(stacked.index.levels, stacked.index.codes)807 ):808 values = lev.take(level_codes)809 name = names[i]810 tm.assert_index_equal(values, Index(deleveled[name]))811 stacked.index.names = [None, None]812 deleveled2 = stacked.reset_index()813 tm.assert_series_equal(814 deleveled["first"], deleveled2["level_0"], check_names=False815 )816 tm.assert_series_equal(817 deleveled["second"], deleveled2["level_1"], check_names=False818 )819 # default name assigned820 rdf = float_frame.reset_index()821 exp = Series(float_frame.index.values, name="index")822 tm.assert_series_equal(rdf["index"], exp)823 # default name assigned, corner case824 df = float_frame.copy()825 df["index"] = "foo"826 rdf = df.reset_index()827 exp = Series(float_frame.index.values, name="level_0")828 tm.assert_series_equal(rdf["level_0"], exp)829 # but this is ok830 float_frame.index.name = "index"831 deleveled = float_frame.reset_index()832 tm.assert_series_equal(deleveled["index"], Series(float_frame.index))833 tm.assert_index_equal(deleveled.index, Index(np.arange(len(deleveled))))834 # preserve column names835 float_frame.columns.name = "columns"836 resetted = float_frame.reset_index()837 assert resetted.columns.name == "columns"838 # only remove certain columns839 df = float_frame.reset_index().set_index(["index", "A", "B"])840 rs = df.reset_index(["A", "B"])841 # TODO should reset_index check_names ?842 tm.assert_frame_equal(rs, float_frame, check_names=False)843 rs = df.reset_index(["index", "A", "B"])844 tm.assert_frame_equal(rs, float_frame.reset_index(), check_names=False)845 rs = df.reset_index(["index", "A", "B"])846 tm.assert_frame_equal(rs, float_frame.reset_index(), check_names=False)847 rs = df.reset_index("A")848 xp = float_frame.reset_index().set_index(["index", "B"])849 tm.assert_frame_equal(rs, xp, check_names=False)850 # test resetting in place851 df = float_frame.copy()852 resetted = float_frame.reset_index()853 df.reset_index(inplace=True)854 tm.assert_frame_equal(df, resetted, check_names=False)855 df = float_frame.reset_index().set_index(["index", "A", "B"])856 rs = df.reset_index("A", drop=True)857 xp = float_frame.copy()858 del xp["A"]859 xp = xp.set_index(["B"], append=True)860 tm.assert_frame_equal(rs, xp, check_names=False)861 def test_reset_index_name(self):862 df = DataFrame(863 [[1, 2, 3, 4], [5, 6, 7, 8]],864 columns=["A", "B", "C", "D"],865 index=Index(range(2), name="x"),866 )867 assert df.reset_index().index.name is None868 assert df.reset_index(drop=True).index.name is None869 df.reset_index(inplace=True)870 assert df.index.name is None871 def test_reset_index_level(self):872 df = DataFrame([[1, 2, 3, 4], [5, 6, 7, 8]], columns=["A", "B", "C", "D"])873 for levels in ["A", "B"], [0, 1]:874 # With MultiIndex875 result = df.set_index(["A", "B"]).reset_index(level=levels[0])876 tm.assert_frame_equal(result, df.set_index("B"))877 result = df.set_index(["A", "B"]).reset_index(level=levels[:1])878 tm.assert_frame_equal(result, df.set_index("B"))879 result = df.set_index(["A", "B"]).reset_index(level=levels)880 tm.assert_frame_equal(result, df)881 result = df.set_index(["A", "B"]).reset_index(level=levels, drop=True)882 tm.assert_frame_equal(result, df[["C", "D"]])883 # With single-level Index (GH 16263)884 result = df.set_index("A").reset_index(level=levels[0])885 tm.assert_frame_equal(result, df)886 result = df.set_index("A").reset_index(level=levels[:1])887 tm.assert_frame_equal(result, df)888 result = df.set_index(["A"]).reset_index(level=levels[0], drop=True)889 tm.assert_frame_equal(result, df[["B", "C", "D"]])890 # Missing levels - for both MultiIndex and single-level Index:891 for idx_lev in ["A", "B"], ["A"]:892 with pytest.raises(KeyError, match="Level E "):893 df.set_index(idx_lev).reset_index(level=["A", "E"])894 with pytest.raises(IndexError, match="Too many levels"):895 df.set_index(idx_lev).reset_index(level=[0, 1, 2])896 def test_reset_index_right_dtype(self):897 time = np.arange(0.0, 10, np.sqrt(2) / 2)898 s1 = Series(899 (9.81 * time ** 2) / 2, index=Index(time, name="time"), name="speed"900 )901 df = DataFrame(s1)902 resetted = s1.reset_index()903 assert resetted["time"].dtype == np.float64904 resetted = df.reset_index()905 assert resetted["time"].dtype == np.float64906 def test_reset_index_multiindex_col(self):907 vals = np.random.randn(3, 3).astype(object)908 idx = ["x", "y", "z"]909 full = np.hstack(([[x] for x in idx], vals))910 df = DataFrame(911 vals,912 Index(idx, name="a"),913 columns=[["b", "b", "c"], ["mean", "median", "mean"]],914 )915 rs = df.reset_index()916 xp = DataFrame(917 full, columns=[["a", "b", "b", "c"], ["", "mean", "median", "mean"]]918 )919 tm.assert_frame_equal(rs, xp)920 rs = df.reset_index(col_fill=None)921 xp = DataFrame(922 full, columns=[["a", "b", "b", "c"], ["a", "mean", "median", "mean"]]923 )924 tm.assert_frame_equal(rs, xp)925 rs = df.reset_index(col_level=1, col_fill="blah")926 xp = DataFrame(927 full, columns=[["blah", "b", "b", "c"], ["a", "mean", "median", "mean"]]928 )929 tm.assert_frame_equal(rs, xp)930 df = DataFrame(931 vals,932 MultiIndex.from_arrays([[0, 1, 2], ["x", "y", "z"]], names=["d", "a"]),933 columns=[["b", "b", "c"], ["mean", "median", "mean"]],934 )935 rs = df.reset_index("a")936 xp = DataFrame(937 full,938 Index([0, 1, 2], name="d"),939 columns=[["a", "b", "b", "c"], ["", "mean", "median", "mean"]],940 )941 tm.assert_frame_equal(rs, xp)942 rs = df.reset_index("a", col_fill=None)943 xp = DataFrame(944 full,945 Index(range(3), name="d"),946 columns=[["a", "b", "b", "c"], ["a", "mean", "median", "mean"]],947 )948 tm.assert_frame_equal(rs, xp)949 rs = df.reset_index("a", col_fill="blah", col_level=1)950 xp = DataFrame(951 full,952 Index(range(3), name="d"),953 columns=[["blah", "b", "b", "c"], ["a", "mean", "median", "mean"]],954 )955 tm.assert_frame_equal(rs, xp)956 def test_reset_index_multiindex_nan(self):957 # GH6322, testing reset_index on MultiIndexes958 # when we have a nan or all nan959 df = DataFrame(960 {"A": ["a", "b", "c"], "B": [0, 1, np.nan], "C": np.random.rand(3)}961 )962 rs = df.set_index(["A", "B"]).reset_index()963 tm.assert_frame_equal(rs, df)964 df = DataFrame(965 {"A": [np.nan, "b", "c"], "B": [0, 1, 2], "C": np.random.rand(3)}966 )967 rs = df.set_index(["A", "B"]).reset_index()968 tm.assert_frame_equal(rs, df)969 df = DataFrame({"A": ["a", "b", "c"], "B": [0, 1, 2], "C": [np.nan, 1.1, 2.2]})970 rs = df.set_index(["A", "B"]).reset_index()971 tm.assert_frame_equal(rs, df)972 df = DataFrame(973 {974 "A": ["a", "b", "c"],975 "B": [np.nan, np.nan, np.nan],976 "C": np.random.rand(3),977 }978 )979 rs = df.set_index(["A", "B"]).reset_index()980 tm.assert_frame_equal(rs, df)981 def test_reset_index_with_datetimeindex_cols(self):982 # GH5818983 #984 df = DataFrame(985 [[1, 2], [3, 4]],986 columns=date_range("1/1/2013", "1/2/2013"),987 index=["A", "B"],988 )989 result = df.reset_index()990 expected = DataFrame(991 [["A", 1, 2], ["B", 3, 4]],992 columns=["index", datetime(2013, 1, 1), datetime(2013, 1, 2)],993 )994 tm.assert_frame_equal(result, expected)995 def test_reset_index_range(self):996 # GH 12071997 df = DataFrame([[0, 0], [1, 1]], columns=["A", "B"], index=RangeIndex(stop=2))998 result = df.reset_index()999 assert isinstance(result.index, RangeIndex)1000 expected = DataFrame(1001 [[0, 0, 0], [1, 1, 1]],1002 columns=["index", "A", "B"],1003 index=RangeIndex(stop=2),1004 )1005 tm.assert_frame_equal(result, expected)1006 def test_set_index_names(self):1007 df = tm.makeDataFrame()1008 df.index.name = "name"1009 assert df.set_index(df.index).index.names == ["name"]1010 mi = MultiIndex.from_arrays(df[["A", "B"]].T.values, names=["A", "B"])1011 mi2 = MultiIndex.from_arrays(1012 df[["A", "B", "A", "B"]].T.values, names=["A", "B", "C", "D"]1013 )1014 df = df.set_index(["A", "B"])1015 assert df.set_index(df.index).index.names == ["A", "B"]1016 # Check that set_index isn't converting a MultiIndex into an Index1017 assert isinstance(df.set_index(df.index).index, MultiIndex)1018 # Check actual equality1019 tm.assert_index_equal(df.set_index(df.index).index, mi)1020 idx2 = df.index.rename(["C", "D"])1021 # Check that [MultiIndex, MultiIndex] yields a MultiIndex rather1022 # than a pair of tuples1023 assert isinstance(df.set_index([df.index, idx2]).index, MultiIndex)1024 # Check equality1025 tm.assert_index_equal(df.set_index([df.index, idx2]).index, mi2)1026 def test_rename_objects(self, float_string_frame):1027 renamed = float_string_frame.rename(columns=str.upper)1028 assert "FOO" in renamed1029 assert "foo" not in renamed1030 def test_rename_axis_style(self):1031 # https://github.com/pandas-dev/pandas/issues/123921032 df = DataFrame({"A": [1, 2], "B": [1, 2]}, index=["X", "Y"])1033 expected = DataFrame({"a": [1, 2], "b": [1, 2]}, index=["X", "Y"])1034 result = df.rename(str.lower, axis=1)1035 tm.assert_frame_equal(result, expected)1036 result = df.rename(str.lower, axis="columns")1037 tm.assert_frame_equal(result, expected)1038 result = df.rename({"A": "a", "B": "b"}, axis=1)1039 tm.assert_frame_equal(result, expected)1040 result = df.rename({"A": "a", "B": "b"}, axis="columns")1041 tm.assert_frame_equal(result, expected)1042 # Index1043 expected = DataFrame({"A": [1, 2], "B": [1, 2]}, index=["x", "y"])1044 result = df.rename(str.lower, axis=0)1045 tm.assert_frame_equal(result, expected)1046 result = df.rename(str.lower, axis="index")1047 tm.assert_frame_equal(result, expected)1048 result = df.rename({"X": "x", "Y": "y"}, axis=0)1049 tm.assert_frame_equal(result, expected)1050 result = df.rename({"X": "x", "Y": "y"}, axis="index")1051 tm.assert_frame_equal(result, expected)1052 result = df.rename(mapper=str.lower, axis="index")1053 tm.assert_frame_equal(result, expected)1054 def test_rename_mapper_multi(self):1055 df = DataFrame({"A": ["a", "b"], "B": ["c", "d"], "C": [1, 2]}).set_index(1056 ["A", "B"]1057 )1058 result = df.rename(str.upper)1059 expected = df.rename(index=str.upper)1060 tm.assert_frame_equal(result, expected)1061 def test_rename_positional_named(self):1062 # https://github.com/pandas-dev/pandas/issues/123921063 df = DataFrame({"a": [1, 2], "b": [1, 2]}, index=["X", "Y"])1064 result = df.rename(str.lower, columns=str.upper)1065 expected = DataFrame({"A": [1, 2], "B": [1, 2]}, index=["x", "y"])1066 tm.assert_frame_equal(result, expected)1067 def test_rename_axis_style_raises(self):1068 # see gh-123921069 df = DataFrame({"A": [1, 2], "B": [1, 2]}, index=["0", "1"])1070 # Named target and axis1071 over_spec_msg = "Cannot specify both 'axis' and any of 'index' or 'columns'"1072 with pytest.raises(TypeError, match=over_spec_msg):1073 df.rename(index=str.lower, axis=1)1074 with pytest.raises(TypeError, match=over_spec_msg):1075 df.rename(index=str.lower, axis="columns")1076 with pytest.raises(TypeError, match=over_spec_msg):1077 df.rename(columns=str.lower, axis="columns")1078 with pytest.raises(TypeError, match=over_spec_msg):1079 df.rename(index=str.lower, axis=0)1080 # Multiple targets and axis1081 with pytest.raises(TypeError, match=over_spec_msg):1082 df.rename(str.lower, str.lower, axis="columns")1083 # Too many targets1084 over_spec_msg = "Cannot specify all of 'mapper', 'index', 'columns'."1085 with pytest.raises(TypeError, match=over_spec_msg):1086 df.rename(str.lower, str.lower, str.lower)1087 # Duplicates1088 with pytest.raises(TypeError, match="multiple values"):1089 df.rename(id, mapper=id)1090 def test_reindex_api_equivalence(self):1091 # equivalence of the labels/axis and index/columns API's1092 df = DataFrame(1093 [[1, 2, 3], [3, 4, 5], [5, 6, 7]],1094 index=["a", "b", "c"],1095 columns=["d", "e", "f"],1096 )1097 res1 = df.reindex(["b", "a"])1098 res2 = df.reindex(index=["b", "a"])1099 res3 = df.reindex(labels=["b", "a"])1100 res4 = df.reindex(labels=["b", "a"], axis=0)1101 res5 = df.reindex(["b", "a"], axis=0)1102 for res in [res2, res3, res4, res5]:1103 tm.assert_frame_equal(res1, res)1104 res1 = df.reindex(columns=["e", "d"])1105 res2 = df.reindex(["e", "d"], axis=1)1106 res3 = df.reindex(labels=["e", "d"], axis=1)1107 for res in [res2, res3]:1108 tm.assert_frame_equal(res1, res)1109 res1 = df.reindex(index=["b", "a"], columns=["e", "d"])1110 res2 = df.reindex(columns=["e", "d"], index=["b", "a"])1111 res3 = df.reindex(labels=["b", "a"], axis=0).reindex(labels=["e", "d"], axis=1)1112 for res in [res2, res3]:1113 tm.assert_frame_equal(res1, res)1114 def test_rename_positional(self):1115 df = DataFrame(columns=["A", "B"])1116 with tm.assert_produces_warning(FutureWarning) as rec:1117 result = df.rename(None, str.lower)1118 expected = DataFrame(columns=["a", "b"])1119 tm.assert_frame_equal(result, expected)1120 assert len(rec) == 11121 message = str(rec[0].message)1122 assert "rename" in message1123 assert "Use named arguments" in message1124 def test_assign_columns(self, float_frame):1125 float_frame["hi"] = "there"1126 df = float_frame.copy()1127 df.columns = ["foo", "bar", "baz", "quux", "foo2"]1128 tm.assert_series_equal(float_frame["C"], df["baz"], check_names=False)1129 tm.assert_series_equal(float_frame["hi"], df["foo2"], check_names=False)1130 def test_set_index_preserve_categorical_dtype(self):1131 # GH13743, GH138541132 df = DataFrame(1133 {1134 "A": [1, 2, 1, 1, 2],1135 "B": [10, 16, 22, 28, 34],1136 "C1": Categorical(list("abaab"), categories=list("bac"), ordered=False),1137 "C2": Categorical(list("abaab"), categories=list("bac"), ordered=True),1138 }1139 )1140 for cols in ["C1", "C2", ["A", "C1"], ["A", "C2"], ["C1", "C2"]]:1141 result = df.set_index(cols).reset_index()1142 result = result.reindex(columns=df.columns)1143 tm.assert_frame_equal(result, df)1144 def test_ambiguous_warns(self):1145 df = DataFrame({"A": [1, 2]})1146 with tm.assert_produces_warning(FutureWarning):1147 df.rename(id, id)1148 with tm.assert_produces_warning(FutureWarning):1149 df.rename({0: 10}, {"A": "B"})1150 def test_rename_signature(self):1151 sig = inspect.signature(DataFrame.rename)1152 parameters = set(sig.parameters)1153 assert parameters == {1154 "self",1155 "mapper",1156 "index",1157 "columns",1158 "axis",1159 "inplace",1160 "copy",1161 "level",1162 "errors",1163 }1164 def test_reindex_signature(self):1165 sig = inspect.signature(DataFrame.reindex)1166 parameters = set(sig.parameters)1167 assert parameters == {1168 "self",1169 "labels",1170 "index",1171 "columns",1172 "axis",1173 "limit",1174 "copy",1175 "level",1176 "method",1177 "fill_value",1178 "tolerance",1179 }1180 def test_droplevel(self):1181 # GH203421182 df = DataFrame([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])1183 df = df.set_index([0, 1]).rename_axis(["a", "b"])1184 df.columns = MultiIndex.from_tuples(1185 [("c", "e"), ("d", "f")], names=["level_1", "level_2"]1186 )1187 # test that dropping of a level in index works1188 expected = df.reset_index("a", drop=True)1189 result = df.droplevel("a", axis="index")1190 tm.assert_frame_equal(result, expected)1191 # test that dropping of a level in columns works1192 expected = df.copy()1193 expected.columns = Index(["c", "d"], name="level_1")1194 result = df.droplevel("level_2", axis="columns")1195 tm.assert_frame_equal(result, expected)1196@pytest.mark.filterwarnings("ignore:Sparse:FutureWarning")1197class TestIntervalIndex:1198 def test_setitem(self):1199 df = DataFrame({"A": range(10)})1200 s = cut(df.A, 5)1201 assert isinstance(s.cat.categories, IntervalIndex)1202 # B & D end up as Categoricals1203 # the remainer are converted to in-line objects1204 # contining an IntervalIndex.values1205 df["B"] = s1206 df["C"] = np.array(s)1207 df["D"] = s.values1208 df["E"] = np.array(s.values)1209 assert is_categorical_dtype(df["B"])1210 assert is_interval_dtype(df["B"].cat.categories)1211 assert is_categorical_dtype(df["D"])1212 assert is_interval_dtype(df["D"].cat.categories)1213 assert is_object_dtype(df["C"])1214 assert is_object_dtype(df["E"])1215 # they compare equal as Index1216 # when converted to numpy objects1217 c = lambda x: Index(np.array(x))1218 tm.assert_index_equal(c(df.B), c(df.B), check_names=False)1219 tm.assert_index_equal(c(df.B), c(df.C), check_names=False)1220 tm.assert_index_equal(c(df.B), c(df.D), check_names=False)1221 tm.assert_index_equal(c(df.B), c(df.D), check_names=False)1222 # B & D are the same Series1223 tm.assert_series_equal(df["B"], df["B"], check_names=False)1224 tm.assert_series_equal(df["B"], df["D"], check_names=False)1225 # C & E are the same Series1226 tm.assert_series_equal(df["C"], df["C"], check_names=False)1227 tm.assert_series_equal(df["C"], df["E"], check_names=False)1228 def test_set_reset_index(self):1229 df = DataFrame({"A": range(10)})1230 s = cut(df.A, 5)1231 df["B"] = s1232 df = df.set_index("B")1233 df = df.reset_index()1234 def test_set_axis_inplace(self):1235 # GH146361236 df = DataFrame(1237 {"A": [1.1, 2.2, 3.3], "B": [5.0, 6.1, 7.2], "C": [4.4, 5.5, 6.6]},1238 index=[2010, 2011, 2012],1239 )1240 expected = {0: df.copy(), 1: df.copy()}1241 expected[0].index = list("abc")1242 expected[1].columns = list("abc")1243 expected["index"] = expected[0]1244 expected["columns"] = expected[1]1245 for axis in expected:1246 # inplace=True1247 # The FutureWarning comes from the fact that we would like to have...

Full Screen

Full Screen

test_range.py

Source:test_range.py Github

copy

Full Screen

...19 index=RangeIndex(0, 20, 2, name="foo"),20 index_dec=RangeIndex(18, -1, -2, name="bar"),21 )22 self.setup_indices()23 def create_index(self):24 return RangeIndex(5)25 def test_can_hold_identifiers(self):26 idx = self.create_index()27 key = idx[0]28 assert idx._can_hold_identifiers_and_holds_name(key) is False29 def test_too_many_names(self):30 with pytest.raises(ValueError, match="^Length"):31 self.index.names = ["roger", "harold"]32 @pytest.mark.parametrize("name", [None, "foo"])33 @pytest.mark.parametrize(34 "args, kwargs, start, stop, step",35 [36 ((5,), dict(), 0, 5, 1),37 ((1, 5), dict(), 1, 5, 1),38 ((1, 5, 2), dict(), 1, 5, 2),39 ((0,), dict(), 0, 0, 1),40 ((0, 0), dict(), 0, 0, 1),41 (tuple(), dict(start=0), 0, 0, 1),42 (tuple(), dict(stop=0), 0, 0, 1),43 ],44 )45 def test_constructor(self, args, kwargs, start, stop, step, name):46 result = RangeIndex(*args, name=name, **kwargs)47 expected = Index(np.arange(start, stop, step, dtype=np.int64), name=name)48 assert isinstance(result, RangeIndex)49 assert result.name is name50 assert result._range == range(start, stop, step)51 tm.assert_index_equal(result, expected)52 def test_constructor_invalid_args(self):53 msg = "RangeIndex\\(\\.\\.\\.\\) must be called with integers"54 with pytest.raises(TypeError, match=msg):55 RangeIndex()56 with pytest.raises(TypeError, match=msg):57 RangeIndex(name="Foo")58 # invalid args59 for i in [60 Index(["a", "b"]),61 Series(["a", "b"]),62 np.array(["a", "b"]),63 [],64 "foo",65 datetime(2000, 1, 1, 0, 0),66 np.arange(0, 10),67 np.array([1]),68 [1],69 ]:70 with pytest.raises(TypeError):71 RangeIndex(i)72 # we don't allow on a bare Index73 msg = (74 r"Index\(\.\.\.\) must be called with a collection of some "75 r"kind, 0 was passed"76 )77 with pytest.raises(TypeError, match=msg):78 Index(0, 1000)79 def test_constructor_same(self):80 # pass thru w and w/o copy81 index = RangeIndex(1, 5, 2)82 result = RangeIndex(index, copy=False)83 assert result.identical(index)84 result = RangeIndex(index, copy=True)85 tm.assert_index_equal(result, index, exact=True)86 result = RangeIndex(index)87 tm.assert_index_equal(result, index, exact=True)88 with pytest.raises(TypeError):89 RangeIndex(index, dtype="float64")90 def test_constructor_range(self):91 msg = "Value needs to be a scalar value, was type <class 'range'>"92 with pytest.raises(TypeError, match=msg):93 result = RangeIndex(range(1, 5, 2))94 result = RangeIndex.from_range(range(1, 5, 2))95 expected = RangeIndex(1, 5, 2)96 tm.assert_index_equal(result, expected, exact=True)97 result = RangeIndex.from_range(range(5, 6))98 expected = RangeIndex(5, 6, 1)99 tm.assert_index_equal(result, expected, exact=True)100 # an invalid range101 result = RangeIndex.from_range(range(5, 1))102 expected = RangeIndex(0, 0, 1)103 tm.assert_index_equal(result, expected, exact=True)104 result = RangeIndex.from_range(range(5))105 expected = RangeIndex(0, 5, 1)106 tm.assert_index_equal(result, expected, exact=True)107 result = Index(range(1, 5, 2))108 expected = RangeIndex(1, 5, 2)109 tm.assert_index_equal(result, expected, exact=True)110 with pytest.raises(TypeError):111 Index(range(1, 5, 2), dtype="float64")112 msg = r"^from_range\(\) got an unexpected keyword argument"113 with pytest.raises(TypeError, match=msg):114 pd.RangeIndex.from_range(range(10), copy=True)115 def test_constructor_name(self):116 # GH12288117 orig = RangeIndex(10)118 orig.name = "original"119 copy = RangeIndex(orig)120 copy.name = "copy"121 assert orig.name == "original"122 assert copy.name == "copy"123 new = Index(copy)124 assert new.name == "copy"125 new.name = "new"126 assert orig.name == "original"127 assert copy.name == "copy"128 assert new.name == "new"129 def test_constructor_corner(self):130 arr = np.array([1, 2, 3, 4], dtype=object)131 index = RangeIndex(1, 5)132 assert index.values.dtype == np.int64133 tm.assert_index_equal(index, Index(arr))134 # non-int raise Exception135 with pytest.raises(TypeError):136 RangeIndex("1", "10", "1")137 with pytest.raises(TypeError):138 RangeIndex(1.1, 10.2, 1.3)139 # invalid passed type140 with pytest.raises(TypeError):141 RangeIndex(1, 5, dtype="float64")142 @pytest.mark.parametrize(143 "index, start, stop, step",144 [145 (RangeIndex(5), 0, 5, 1),146 (RangeIndex(0, 5), 0, 5, 1),147 (RangeIndex(5, step=2), 0, 5, 2),148 (RangeIndex(1, 5, 2), 1, 5, 2),149 ],150 )151 def test_start_stop_step_attrs(self, index, start, stop, step):152 # GH 25710153 assert index.start == start154 assert index.stop == stop155 assert index.step == step156 @pytest.mark.parametrize("attr_name", ["_start", "_stop", "_step"])157 def test_deprecated_start_stop_step_attrs(self, attr_name):158 # GH 26581159 idx = self.create_index()160 with tm.assert_produces_warning(DeprecationWarning):161 getattr(idx, attr_name)162 def test_copy(self):163 i = RangeIndex(5, name="Foo")164 i_copy = i.copy()165 assert i_copy is not i166 assert i_copy.identical(i)167 assert i_copy._range == range(0, 5, 1)168 assert i_copy.name == "Foo"169 def test_repr(self):170 i = RangeIndex(5, name="Foo")171 result = repr(i)172 expected = "RangeIndex(start=0, stop=5, step=1, name='Foo')"173 assert result == expected174 result = eval(result)175 tm.assert_index_equal(result, i, exact=True)176 i = RangeIndex(5, 0, -1)177 result = repr(i)178 expected = "RangeIndex(start=5, stop=0, step=-1)"179 assert result == expected180 result = eval(result)181 tm.assert_index_equal(result, i, exact=True)182 def test_insert(self):183 idx = RangeIndex(5, name="Foo")184 result = idx[1:4]185 # test 0th element186 tm.assert_index_equal(idx[0:4], result.insert(0, idx[0]))187 # GH 18295 (test missing)188 expected = Float64Index([0, np.nan, 1, 2, 3, 4])189 for na in (np.nan, pd.NaT, None):190 result = RangeIndex(5).insert(1, na)191 tm.assert_index_equal(result, expected)192 def test_delete(self):193 idx = RangeIndex(5, name="Foo")194 expected = idx[1:].astype(int)195 result = idx.delete(0)196 tm.assert_index_equal(result, expected)197 assert result.name == expected.name198 expected = idx[:-1].astype(int)199 result = idx.delete(-1)200 tm.assert_index_equal(result, expected)201 assert result.name == expected.name202 with pytest.raises((IndexError, ValueError)):203 # either depending on numpy version204 result = idx.delete(len(idx))205 def test_view(self):206 i = RangeIndex(0, name="Foo")207 i_view = i.view()208 assert i_view.name == "Foo"209 i_view = i.view("i8")210 tm.assert_numpy_array_equal(i.values, i_view)211 i_view = i.view(RangeIndex)212 tm.assert_index_equal(i, i_view)213 def test_dtype(self):214 assert self.index.dtype == np.int64215 def test_cached_data(self):216 # GH 26565, GH26617217 # Calling RangeIndex._data caches an int64 array of the same length at218 # self._cached_data. This test checks whether _cached_data has been set219 idx = RangeIndex(0, 100, 10)220 assert idx._cached_data is None221 repr(idx)222 assert idx._cached_data is None223 str(idx)224 assert idx._cached_data is None225 idx.get_loc(20)226 assert idx._cached_data is None227 90 in idx228 assert idx._cached_data is None229 91 in idx230 assert idx._cached_data is None231 with tm.assert_produces_warning(FutureWarning):232 idx.contains(90)233 assert idx._cached_data is None234 with tm.assert_produces_warning(FutureWarning):235 idx.contains(91)236 assert idx._cached_data is None237 idx.all()238 assert idx._cached_data is None239 idx.any()240 assert idx._cached_data is None241 df = pd.DataFrame({"a": range(10)}, index=idx)242 df.loc[50]243 assert idx._cached_data is None244 with pytest.raises(KeyError, match="51"):245 df.loc[51]246 assert idx._cached_data is None247 df.loc[10:50]248 assert idx._cached_data is None249 df.iloc[5:10]250 assert idx._cached_data is None251 # actually calling idx._data252 assert isinstance(idx._data, np.ndarray)253 assert isinstance(idx._cached_data, np.ndarray)254 def test_is_monotonic(self):255 assert self.index.is_monotonic is True256 assert self.index.is_monotonic_increasing is True257 assert self.index.is_monotonic_decreasing is False258 assert self.index._is_strictly_monotonic_increasing is True259 assert self.index._is_strictly_monotonic_decreasing is False260 index = RangeIndex(4, 0, -1)261 assert index.is_monotonic is False262 assert index._is_strictly_monotonic_increasing is False263 assert index.is_monotonic_decreasing is True264 assert index._is_strictly_monotonic_decreasing is True265 index = RangeIndex(1, 2)266 assert index.is_monotonic is True267 assert index.is_monotonic_increasing is True268 assert index.is_monotonic_decreasing is True269 assert index._is_strictly_monotonic_increasing is True270 assert index._is_strictly_monotonic_decreasing is True271 index = RangeIndex(2, 1)272 assert index.is_monotonic is True273 assert index.is_monotonic_increasing is True274 assert index.is_monotonic_decreasing is True275 assert index._is_strictly_monotonic_increasing is True276 assert index._is_strictly_monotonic_decreasing is True277 index = RangeIndex(1, 1)278 assert index.is_monotonic is True279 assert index.is_monotonic_increasing is True280 assert index.is_monotonic_decreasing is True281 assert index._is_strictly_monotonic_increasing is True282 assert index._is_strictly_monotonic_decreasing is True283 def test_equals_range(self):284 equiv_pairs = [285 (RangeIndex(0, 9, 2), RangeIndex(0, 10, 2)),286 (RangeIndex(0), RangeIndex(1, -1, 3)),287 (RangeIndex(1, 2, 3), RangeIndex(1, 3, 4)),288 (RangeIndex(0, -9, -2), RangeIndex(0, -10, -2)),289 ]290 for left, right in equiv_pairs:291 assert left.equals(right)292 assert right.equals(left)293 def test_logical_compat(self):294 idx = self.create_index()295 assert idx.all() == idx.values.all()296 assert idx.any() == idx.values.any()297 def test_identical(self):298 i = Index(self.index.copy())299 assert i.identical(self.index)300 # we don't allow object dtype for RangeIndex301 if isinstance(self.index, RangeIndex):302 return303 same_values_different_type = Index(i, dtype=object)304 assert not i.identical(same_values_different_type)305 i = self.index.copy(dtype=object)306 i = i.rename("foo")307 same_values = Index(i, dtype=object)308 assert same_values.identical(self.index.copy(dtype=object))309 assert not i.identical(self.index)310 assert Index(same_values, name="foo", dtype=object).identical(i)311 assert not self.index.copy(dtype=object).identical(312 self.index.copy(dtype="int64")313 )314 def test_get_indexer(self):315 target = RangeIndex(10)316 indexer = self.index.get_indexer(target)317 expected = np.array([0, -1, 1, -1, 2, -1, 3, -1, 4, -1], dtype=np.intp)318 tm.assert_numpy_array_equal(indexer, expected)319 def test_get_indexer_pad(self):320 target = RangeIndex(10)321 indexer = self.index.get_indexer(target, method="pad")322 expected = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4], dtype=np.intp)323 tm.assert_numpy_array_equal(indexer, expected)324 def test_get_indexer_backfill(self):325 target = RangeIndex(10)326 indexer = self.index.get_indexer(target, method="backfill")327 expected = np.array([0, 1, 1, 2, 2, 3, 3, 4, 4, 5], dtype=np.intp)328 tm.assert_numpy_array_equal(indexer, expected)329 def test_join_outer(self):330 # join with Int64Index331 other = Int64Index(np.arange(25, 14, -1))332 res, lidx, ridx = self.index.join(other, how="outer", return_indexers=True)333 noidx_res = self.index.join(other, how="outer")334 tm.assert_index_equal(res, noidx_res)335 eres = Int64Index(336 [0, 2, 4, 6, 8, 10, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]337 )338 elidx = np.array(339 [0, 1, 2, 3, 4, 5, 6, 7, -1, 8, -1, 9, -1, -1, -1, -1, -1, -1, -1],340 dtype=np.intp,341 )342 eridx = np.array(343 [-1, -1, -1, -1, -1, -1, -1, -1, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0],344 dtype=np.intp,345 )346 assert isinstance(res, Int64Index)347 assert not isinstance(res, RangeIndex)348 tm.assert_index_equal(res, eres)349 tm.assert_numpy_array_equal(lidx, elidx)350 tm.assert_numpy_array_equal(ridx, eridx)351 # join with RangeIndex352 other = RangeIndex(25, 14, -1)353 res, lidx, ridx = self.index.join(other, how="outer", return_indexers=True)354 noidx_res = self.index.join(other, how="outer")355 tm.assert_index_equal(res, noidx_res)356 assert isinstance(res, Int64Index)357 assert not isinstance(res, RangeIndex)358 tm.assert_index_equal(res, eres)359 tm.assert_numpy_array_equal(lidx, elidx)360 tm.assert_numpy_array_equal(ridx, eridx)361 def test_join_inner(self):362 # Join with non-RangeIndex363 other = Int64Index(np.arange(25, 14, -1))364 res, lidx, ridx = self.index.join(other, how="inner", return_indexers=True)365 # no guarantee of sortedness, so sort for comparison purposes366 ind = res.argsort()367 res = res.take(ind)368 lidx = lidx.take(ind)369 ridx = ridx.take(ind)370 eres = Int64Index([16, 18])371 elidx = np.array([8, 9], dtype=np.intp)372 eridx = np.array([9, 7], dtype=np.intp)373 assert isinstance(res, Int64Index)374 tm.assert_index_equal(res, eres)375 tm.assert_numpy_array_equal(lidx, elidx)376 tm.assert_numpy_array_equal(ridx, eridx)377 # Join two RangeIndex378 other = RangeIndex(25, 14, -1)379 res, lidx, ridx = self.index.join(other, how="inner", return_indexers=True)380 assert isinstance(res, RangeIndex)381 tm.assert_index_equal(res, eres)382 tm.assert_numpy_array_equal(lidx, elidx)383 tm.assert_numpy_array_equal(ridx, eridx)384 def test_join_left(self):385 # Join with Int64Index386 other = Int64Index(np.arange(25, 14, -1))387 res, lidx, ridx = self.index.join(other, how="left", return_indexers=True)388 eres = self.index389 eridx = np.array([-1, -1, -1, -1, -1, -1, -1, -1, 9, 7], dtype=np.intp)390 assert isinstance(res, RangeIndex)391 tm.assert_index_equal(res, eres)392 assert lidx is None393 tm.assert_numpy_array_equal(ridx, eridx)394 # Join withRangeIndex395 other = Int64Index(np.arange(25, 14, -1))396 res, lidx, ridx = self.index.join(other, how="left", return_indexers=True)397 assert isinstance(res, RangeIndex)398 tm.assert_index_equal(res, eres)399 assert lidx is None400 tm.assert_numpy_array_equal(ridx, eridx)401 def test_join_right(self):402 # Join with Int64Index403 other = Int64Index(np.arange(25, 14, -1))404 res, lidx, ridx = self.index.join(other, how="right", return_indexers=True)405 eres = other406 elidx = np.array([-1, -1, -1, -1, -1, -1, -1, 9, -1, 8, -1], dtype=np.intp)407 assert isinstance(other, Int64Index)408 tm.assert_index_equal(res, eres)409 tm.assert_numpy_array_equal(lidx, elidx)410 assert ridx is None411 # Join withRangeIndex412 other = RangeIndex(25, 14, -1)413 res, lidx, ridx = self.index.join(other, how="right", return_indexers=True)414 eres = other415 assert isinstance(other, RangeIndex)416 tm.assert_index_equal(res, eres)417 tm.assert_numpy_array_equal(lidx, elidx)418 assert ridx is None419 def test_join_non_int_index(self):420 other = Index([3, 6, 7, 8, 10], dtype=object)421 outer = self.index.join(other, how="outer")422 outer2 = other.join(self.index, how="outer")423 expected = Index([0, 2, 3, 4, 6, 7, 8, 10, 12, 14, 16, 18])424 tm.assert_index_equal(outer, outer2)425 tm.assert_index_equal(outer, expected)426 inner = self.index.join(other, how="inner")427 inner2 = other.join(self.index, how="inner")428 expected = Index([6, 8, 10])429 tm.assert_index_equal(inner, inner2)430 tm.assert_index_equal(inner, expected)431 left = self.index.join(other, how="left")432 tm.assert_index_equal(left, self.index.astype(object))433 left2 = other.join(self.index, how="left")...

Full Screen

Full Screen

common.py

Source:common.py Github

copy

Full Screen

...38 with pytest.raises(TypeError, match=msg):39 self._holder()40 def test_to_series(self):41 # assert that we are creating a copy of the index42 idx = self.create_index()43 s = idx.to_series()44 assert s.values is not idx.values45 assert s.index is not idx46 assert s.name == idx.name47 def test_to_series_with_arguments(self):48 # GH1869949 # index kwarg50 idx = self.create_index()51 s = idx.to_series(index=idx)52 assert s.values is not idx.values53 assert s.index is idx54 assert s.name == idx.name55 # name kwarg56 idx = self.create_index()57 s = idx.to_series(name="__test")58 assert s.values is not idx.values59 assert s.index is not idx60 assert s.name != idx.name61 @pytest.mark.parametrize("name", [None, "new_name"])62 def test_to_frame(self, name):63 # see GH-15230, GH-2258064 idx = self.create_index()65 if name:66 idx_name = name67 else:68 idx_name = idx.name or 069 df = idx.to_frame(name=idx_name)70 assert df.index is idx71 assert len(df.columns) == 172 assert df.columns[0] == idx_name73 assert df[idx_name].values is not idx.values74 df = idx.to_frame(index=False, name=idx_name)75 assert df.index is not idx76 def test_to_frame_datetime_tz(self):77 # GH 2580978 idx = pd.date_range(start="2019-01-01", end="2019-01-30", freq="D")79 idx = idx.tz_localize("UTC")80 result = idx.to_frame()81 expected = pd.DataFrame(idx, index=idx)82 tm.assert_frame_equal(result, expected)83 def test_shift(self):84 # GH8083 test the base class for shift85 idx = self.create_index()86 msg = "Not supported for type {}".format(type(idx).__name__)87 with pytest.raises(NotImplementedError, match=msg):88 idx.shift(1)89 with pytest.raises(NotImplementedError, match=msg):90 idx.shift(1, 2)91 def test_create_index_existing_name(self):92 # GH11193, when an existing index is passed, and a new name is not93 # specified, the new index should inherit the previous object name94 expected = self.create_index()95 if not isinstance(expected, MultiIndex):96 expected.name = "foo"97 result = pd.Index(expected)98 tm.assert_index_equal(result, expected)99 result = pd.Index(expected, name="bar")100 expected.name = "bar"101 tm.assert_index_equal(result, expected)102 else:103 expected.names = ["foo", "bar"]104 result = pd.Index(expected)105 tm.assert_index_equal(106 result,107 Index(108 Index(109 [110 ("foo", "one"),111 ("foo", "two"),112 ("bar", "one"),113 ("baz", "two"),114 ("qux", "one"),115 ("qux", "two"),116 ],117 dtype="object",118 ),119 names=["foo", "bar"],120 ),121 )122 result = pd.Index(expected, names=["A", "B"])123 tm.assert_index_equal(124 result,125 Index(126 Index(127 [128 ("foo", "one"),129 ("foo", "two"),130 ("bar", "one"),131 ("baz", "two"),132 ("qux", "one"),133 ("qux", "two"),134 ],135 dtype="object",136 ),137 names=["A", "B"],138 ),139 )140 def test_numeric_compat(self):141 idx = self.create_index()142 with pytest.raises(TypeError, match="cannot perform __mul__"):143 idx * 1144 with pytest.raises(TypeError, match="cannot perform __rmul__"):145 1 * idx146 div_err = "cannot perform __truediv__"147 with pytest.raises(TypeError, match=div_err):148 idx / 1149 div_err = div_err.replace(" __", " __r")150 with pytest.raises(TypeError, match=div_err):151 1 / idx152 with pytest.raises(TypeError, match="cannot perform __floordiv__"):153 idx // 1154 with pytest.raises(TypeError, match="cannot perform __rfloordiv__"):155 1 // idx156 def test_logical_compat(self):157 idx = self.create_index()158 with pytest.raises(TypeError, match="cannot perform all"):159 idx.all()160 with pytest.raises(TypeError, match="cannot perform any"):161 idx.any()162 def test_boolean_context_compat(self):163 # boolean context compat164 idx = self.create_index()165 with pytest.raises(ValueError, match="The truth value of a"):166 if idx:167 pass168 def test_reindex_base(self):169 idx = self.create_index()170 expected = np.arange(idx.size, dtype=np.intp)171 actual = idx.get_indexer(idx)172 tm.assert_numpy_array_equal(expected, actual)173 with pytest.raises(ValueError, match="Invalid fill method"):174 idx.get_indexer(idx, method="invalid")175 def test_get_indexer_consistency(self):176 # See GH 16819177 for name, index in self.indices.items():178 if isinstance(index, IntervalIndex):179 continue180 if index.is_unique or isinstance(index, CategoricalIndex):181 indexer = index.get_indexer(index[0:2])182 assert isinstance(indexer, np.ndarray)183 assert indexer.dtype == np.intp184 else:185 e = "Reindexing only valid with uniquely valued Index objects"186 with pytest.raises(InvalidIndexError, match=e):187 index.get_indexer(index[0:2])188 indexer, _ = index.get_indexer_non_unique(index[0:2])189 assert isinstance(indexer, np.ndarray)190 assert indexer.dtype == np.intp191 def test_ndarray_compat_properties(self):192 idx = self.create_index()193 assert idx.T.equals(idx)194 assert idx.transpose().equals(idx)195 values = idx.values196 for prop in self._compat_props:197 assert getattr(idx, prop) == getattr(values, prop)198 # test for validity199 idx.nbytes200 idx.values.nbytes201 def test_repr_roundtrip(self):202 idx = self.create_index()203 tm.assert_index_equal(eval(repr(idx)), idx)204 def test_str(self):205 # test the string repr206 idx = self.create_index()207 idx.name = "foo"208 assert "'foo'" in str(idx)209 assert idx.__class__.__name__ in str(idx)210 def test_repr_max_seq_item_setting(self):211 # GH10182212 idx = self.create_index()213 idx = idx.repeat(50)214 with pd.option_context("display.max_seq_items", None):215 repr(idx)216 assert "..." not in str(idx)217 def test_copy_name(self):218 # gh-12309: Check that the "name" argument219 # passed at initialization is honored.220 for name, index in self.indices.items():221 if isinstance(index, MultiIndex):222 continue223 first = index.__class__(index, copy=True, name="mario")224 second = first.__class__(first, copy=False)225 # Even though "copy=False", we want a new object.226 assert first is not second227 # Not using tm.assert_index_equal() since names differ.228 assert index.equals(first)229 assert first.name == "mario"230 assert second.name == "mario"231 s1 = Series(2, index=first)232 s2 = Series(3, index=second[:-1])233 if not isinstance(index, CategoricalIndex):234 # See gh-13365235 s3 = s1 * s2236 assert s3.index.name == "mario"237 def test_ensure_copied_data(self):238 # Check the "copy" argument of each Index.__new__ is honoured239 # GH12309240 for name, index in self.indices.items():241 init_kwargs = {}242 if isinstance(index, PeriodIndex):243 # Needs "freq" specification:244 init_kwargs["freq"] = index.freq245 elif isinstance(index, (RangeIndex, MultiIndex, CategoricalIndex)):246 # RangeIndex cannot be initialized from data247 # MultiIndex and CategoricalIndex are tested separately248 continue249 index_type = index.__class__250 result = index_type(index.values, copy=True, **init_kwargs)251 tm.assert_index_equal(index, result)252 tm.assert_numpy_array_equal(253 index._ndarray_values, result._ndarray_values, check_same="copy"254 )255 if isinstance(index, PeriodIndex):256 # .values an object array of Period, thus copied257 result = index_type(ordinal=index.asi8, copy=False, **init_kwargs)258 tm.assert_numpy_array_equal(259 index._ndarray_values, result._ndarray_values, check_same="same"260 )261 elif isinstance(index, IntervalIndex):262 # checked in test_interval.py263 pass264 else:265 result = index_type(index.values, copy=False, **init_kwargs)266 tm.assert_numpy_array_equal(267 index.values, result.values, check_same="same"268 )269 tm.assert_numpy_array_equal(270 index._ndarray_values, result._ndarray_values, check_same="same"271 )272 def test_memory_usage(self):273 for name, index in self.indices.items():274 result = index.memory_usage()275 if len(index):276 index.get_loc(index[0])277 result2 = index.memory_usage()278 result3 = index.memory_usage(deep=True)279 # RangeIndex, IntervalIndex280 # don't have engines281 if not isinstance(index, (RangeIndex, IntervalIndex)):282 assert result2 > result283 if index.inferred_type == "object":284 assert result3 > result2285 else:286 # we report 0 for no-length287 assert result == 0288 def test_argsort(self):289 for k, ind in self.indices.items():290 # separately tested291 if k in ["catIndex"]:292 continue293 result = ind.argsort()294 expected = np.array(ind).argsort()295 tm.assert_numpy_array_equal(result, expected, check_dtype=False)296 def test_numpy_argsort(self):297 for k, ind in self.indices.items():298 result = np.argsort(ind)299 expected = ind.argsort()300 tm.assert_numpy_array_equal(result, expected)301 # these are the only two types that perform302 # pandas compatibility input validation - the303 # rest already perform separate (or no) such304 # validation via their 'values' attribute as305 # defined in pandas.core.indexes/base.py - they306 # cannot be changed at the moment due to307 # backwards compatibility concerns308 if isinstance(type(ind), (CategoricalIndex, RangeIndex)):309 msg = "the 'axis' parameter is not supported"310 with pytest.raises(ValueError, match=msg):311 np.argsort(ind, axis=1)312 msg = "the 'kind' parameter is not supported"313 with pytest.raises(ValueError, match=msg):314 np.argsort(ind, kind="mergesort")315 msg = "the 'order' parameter is not supported"316 with pytest.raises(ValueError, match=msg):317 np.argsort(ind, order=("a", "b"))318 def test_take(self):319 indexer = [4, 3, 0, 2]320 for k, ind in self.indices.items():321 # separate322 if k in ["boolIndex", "tuples", "empty"]:323 continue324 result = ind.take(indexer)325 expected = ind[indexer]326 assert result.equals(expected)327 if not isinstance(ind, (DatetimeIndex, PeriodIndex, TimedeltaIndex)):328 # GH 10791329 with pytest.raises(AttributeError):330 ind.freq331 def test_take_invalid_kwargs(self):332 idx = self.create_index()333 indices = [1, 2]334 msg = r"take\(\) got an unexpected keyword argument 'foo'"335 with pytest.raises(TypeError, match=msg):336 idx.take(indices, foo=2)337 msg = "the 'out' parameter is not supported"338 with pytest.raises(ValueError, match=msg):339 idx.take(indices, out=indices)340 msg = "the 'mode' parameter is not supported"341 with pytest.raises(ValueError, match=msg):342 idx.take(indices, mode="clip")343 def test_repeat(self):344 rep = 2345 i = self.create_index()346 expected = pd.Index(i.values.repeat(rep), name=i.name)347 tm.assert_index_equal(i.repeat(rep), expected)348 i = self.create_index()349 rep = np.arange(len(i))350 expected = pd.Index(i.values.repeat(rep), name=i.name)351 tm.assert_index_equal(i.repeat(rep), expected)352 def test_numpy_repeat(self):353 rep = 2354 i = self.create_index()355 expected = i.repeat(rep)356 tm.assert_index_equal(np.repeat(i, rep), expected)357 msg = "the 'axis' parameter is not supported"358 with pytest.raises(ValueError, match=msg):359 np.repeat(i, rep, axis=0)360 @pytest.mark.parametrize("klass", [list, tuple, np.array, Series])361 def test_where(self, klass):362 i = self.create_index()363 cond = [True] * len(i)364 result = i.where(klass(cond))365 expected = i366 tm.assert_index_equal(result, expected)367 cond = [False] + [True] * len(i[1:])368 expected = pd.Index([i._na_value] + i[1:].tolist(), dtype=i.dtype)369 result = i.where(klass(cond))370 tm.assert_index_equal(result, expected)371 @pytest.mark.parametrize("case", [0.5, "xxx"])372 @pytest.mark.parametrize(373 "method", ["intersection", "union", "difference", "symmetric_difference"]374 )375 def test_set_ops_error_cases(self, case, method):376 for name, idx in self.indices.items():377 # non-iterable input378 msg = "Input must be Index or array-like"379 with pytest.raises(TypeError, match=msg):380 getattr(idx, method)(case)381 def test_intersection_base(self):382 for name, idx in self.indices.items():383 first = idx[:5]384 second = idx[:3]385 intersect = first.intersection(second)386 if isinstance(idx, CategoricalIndex):387 pass388 else:389 assert tm.equalContents(intersect, second)390 # GH 10149391 cases = [klass(second.values) for klass in [np.array, Series, list]]392 for case in cases:393 if isinstance(idx, CategoricalIndex):394 pass395 else:396 result = first.intersection(case)397 assert tm.equalContents(result, second)398 if isinstance(idx, MultiIndex):399 msg = "other must be a MultiIndex or a list of tuples"400 with pytest.raises(TypeError, match=msg):401 first.intersection([1, 2, 3])402 def test_union_base(self):403 for name, idx in self.indices.items():404 first = idx[3:]405 second = idx[:5]406 everything = idx407 union = first.union(second)408 assert tm.equalContents(union, everything)409 # GH 10149410 cases = [klass(second.values) for klass in [np.array, Series, list]]411 for case in cases:412 if isinstance(idx, CategoricalIndex):413 pass414 else:415 result = first.union(case)416 assert tm.equalContents(result, everything)417 if isinstance(idx, MultiIndex):418 msg = "other must be a MultiIndex or a list of tuples"419 with pytest.raises(TypeError, match=msg):420 first.union([1, 2, 3])421 @pytest.mark.parametrize("sort", [None, False])422 def test_difference_base(self, sort):423 for name, idx in self.indices.items():424 first = idx[2:]425 second = idx[:4]426 answer = idx[4:]427 result = first.difference(second, sort)428 if isinstance(idx, CategoricalIndex):429 pass430 else:431 assert tm.equalContents(result, answer)432 # GH 10149433 cases = [klass(second.values) for klass in [np.array, Series, list]]434 for case in cases:435 if isinstance(idx, CategoricalIndex):436 pass437 elif isinstance(idx, (DatetimeIndex, TimedeltaIndex)):438 assert result.__class__ == answer.__class__439 tm.assert_numpy_array_equal(440 result.sort_values().asi8, answer.sort_values().asi8441 )442 else:443 result = first.difference(case, sort)444 assert tm.equalContents(result, answer)445 if isinstance(idx, MultiIndex):446 msg = "other must be a MultiIndex or a list of tuples"447 with pytest.raises(TypeError, match=msg):448 first.difference([1, 2, 3], sort)449 def test_symmetric_difference(self):450 for name, idx in self.indices.items():451 first = idx[1:]452 second = idx[:-1]453 if isinstance(idx, CategoricalIndex):454 pass455 else:456 answer = idx[[0, -1]]457 result = first.symmetric_difference(second)458 assert tm.equalContents(result, answer)459 # GH 10149460 cases = [klass(second.values) for klass in [np.array, Series, list]]461 for case in cases:462 if isinstance(idx, CategoricalIndex):463 pass464 else:465 result = first.symmetric_difference(case)466 assert tm.equalContents(result, answer)467 if isinstance(idx, MultiIndex):468 msg = "other must be a MultiIndex or a list of tuples"469 with pytest.raises(TypeError, match=msg):470 first.symmetric_difference([1, 2, 3])471 def test_insert_base(self):472 for name, idx in self.indices.items():473 result = idx[1:4]474 if not len(idx):475 continue476 # test 0th element477 assert idx[0:4].equals(result.insert(0, idx[0]))478 def test_delete_base(self):479 for name, idx in self.indices.items():480 if not len(idx):481 continue482 if isinstance(idx, RangeIndex):483 # tested in class484 continue485 expected = idx[1:]486 result = idx.delete(0)487 assert result.equals(expected)488 assert result.name == expected.name489 expected = idx[:-1]490 result = idx.delete(-1)491 assert result.equals(expected)492 assert result.name == expected.name493 with pytest.raises((IndexError, ValueError)):494 # either depending on numpy version495 idx.delete(len(idx))496 def test_equals(self):497 for name, idx in self.indices.items():498 assert idx.equals(idx)499 assert idx.equals(idx.copy())500 assert idx.equals(idx.astype(object))501 assert not idx.equals(list(idx))502 assert not idx.equals(np.array(idx))503 # Cannot pass in non-int64 dtype to RangeIndex504 if not isinstance(idx, RangeIndex):505 same_values = Index(idx, dtype=object)506 assert idx.equals(same_values)507 assert same_values.equals(idx)508 if idx.nlevels == 1:509 # do not test MultiIndex510 assert not idx.equals(pd.Series(idx))511 def test_equals_op(self):512 # GH9947, GH10637513 index_a = self.create_index()514 if isinstance(index_a, PeriodIndex):515 pytest.skip("Skip check for PeriodIndex")516 n = len(index_a)517 index_b = index_a[0:-1]518 index_c = index_a[0:-1].append(index_a[-2:-1])519 index_d = index_a[0:1]520 msg = "Lengths must match|could not be broadcast"521 with pytest.raises(ValueError, match=msg):522 index_a == index_b523 expected1 = np.array([True] * n)524 expected2 = np.array([True] * (n - 1) + [False])525 tm.assert_numpy_array_equal(index_a == index_a, expected1)526 tm.assert_numpy_array_equal(index_a == index_c, expected2)527 # test comparisons with numpy arrays528 array_a = np.array(index_a)529 array_b = np.array(index_a[0:-1])530 array_c = np.array(index_a[0:-1].append(index_a[-2:-1]))531 array_d = np.array(index_a[0:1])532 with pytest.raises(ValueError, match=msg):533 index_a == array_b534 tm.assert_numpy_array_equal(index_a == array_a, expected1)535 tm.assert_numpy_array_equal(index_a == array_c, expected2)536 # test comparisons with Series537 series_a = Series(array_a)538 series_b = Series(array_b)539 series_c = Series(array_c)540 series_d = Series(array_d)541 with pytest.raises(ValueError, match=msg):542 index_a == series_b543 tm.assert_numpy_array_equal(index_a == series_a, expected1)544 tm.assert_numpy_array_equal(index_a == series_c, expected2)545 # cases where length is 1 for one of them546 with pytest.raises(ValueError, match="Lengths must match"):547 index_a == index_d548 with pytest.raises(ValueError, match="Lengths must match"):549 index_a == series_d550 with pytest.raises(ValueError, match="Lengths must match"):551 index_a == array_d552 msg = "Can only compare identically-labeled Series objects"553 with pytest.raises(ValueError, match=msg):554 series_a == series_d555 with pytest.raises(ValueError, match="Lengths must match"):556 series_a == array_d557 # comparing with a scalar should broadcast; note that we are excluding558 # MultiIndex because in this case each item in the index is a tuple of559 # length 2, and therefore is considered an array of length 2 in the560 # comparison instead of a scalar561 if not isinstance(index_a, MultiIndex):562 expected3 = np.array([False] * (len(index_a) - 2) + [True, False])563 # assuming the 2nd to last item is unique in the data564 item = index_a[-2]565 tm.assert_numpy_array_equal(index_a == item, expected3)566 tm.assert_series_equal(series_a == item, Series(expected3))567 def test_hasnans_isnans(self):568 # GH 11343, added tests for hasnans / isnans569 for name, index in self.indices.items():570 if isinstance(index, MultiIndex):571 pass572 else:573 idx = index.copy()574 # cases in indices doesn't include NaN575 expected = np.array([False] * len(idx), dtype=bool)576 tm.assert_numpy_array_equal(idx._isnan, expected)577 assert idx.hasnans is False578 idx = index.copy()579 values = np.asarray(idx.values)580 if len(index) == 0:581 continue582 elif isinstance(index, DatetimeIndexOpsMixin):583 values[1] = iNaT584 elif isinstance(index, (Int64Index, UInt64Index)):585 continue586 else:587 values[1] = np.nan588 if isinstance(index, PeriodIndex):589 idx = index.__class__(values, freq=index.freq)590 else:591 idx = index.__class__(values)592 expected = np.array([False] * len(idx), dtype=bool)593 expected[1] = True594 tm.assert_numpy_array_equal(idx._isnan, expected)595 assert idx.hasnans is True596 def test_fillna(self):597 # GH 11343598 for name, index in self.indices.items():599 if len(index) == 0:600 pass601 elif isinstance(index, MultiIndex):602 idx = index.copy()603 msg = "isna is not defined for MultiIndex"604 with pytest.raises(NotImplementedError, match=msg):605 idx.fillna(idx[0])606 else:607 idx = index.copy()608 result = idx.fillna(idx[0])609 tm.assert_index_equal(result, idx)610 assert result is not idx611 msg = "'value' must be a scalar, passed: "612 with pytest.raises(TypeError, match=msg):613 idx.fillna([idx[0]])614 idx = index.copy()615 values = np.asarray(idx.values)616 if isinstance(index, DatetimeIndexOpsMixin):617 values[1] = iNaT618 elif isinstance(index, (Int64Index, UInt64Index)):619 continue620 else:621 values[1] = np.nan622 if isinstance(index, PeriodIndex):623 idx = index.__class__(values, freq=index.freq)624 else:625 idx = index.__class__(values)626 expected = np.array([False] * len(idx), dtype=bool)627 expected[1] = True628 tm.assert_numpy_array_equal(idx._isnan, expected)629 assert idx.hasnans is True630 def test_nulls(self):631 # this is really a smoke test for the methods632 # as these are adequately tested for function elsewhere633 for name, index in self.indices.items():634 if len(index) == 0:635 tm.assert_numpy_array_equal(index.isna(), np.array([], dtype=bool))636 elif isinstance(index, MultiIndex):637 idx = index.copy()638 msg = "isna is not defined for MultiIndex"639 with pytest.raises(NotImplementedError, match=msg):640 idx.isna()641 else:642 if not index.hasnans:643 tm.assert_numpy_array_equal(644 index.isna(), np.zeros(len(index), dtype=bool)645 )646 tm.assert_numpy_array_equal(647 index.notna(), np.ones(len(index), dtype=bool)648 )649 else:650 result = isna(index)651 tm.assert_numpy_array_equal(index.isna(), result)652 tm.assert_numpy_array_equal(index.notna(), ~result)653 def test_empty(self):654 # GH 15270655 index = self.create_index()656 assert not index.empty657 assert index[:0].empty658 def test_join_self_unique(self, join_type):659 index = self.create_index()660 if index.is_unique:661 joined = index.join(index, how=join_type)662 assert (index == joined).all()663 def test_map(self):664 # callable665 index = self.create_index()666 # we don't infer UInt64667 if isinstance(index, pd.UInt64Index):668 expected = index.astype("int64")669 else:670 expected = index671 result = index.map(lambda x: x)672 tm.assert_index_equal(result, expected)673 @pytest.mark.parametrize(674 "mapper",675 [676 lambda values, index: {i: e for e, i in zip(values, index)},677 lambda values, index: pd.Series(values, index),678 ],679 )680 def test_map_dictlike(self, mapper):681 index = self.create_index()682 if isinstance(index, (pd.CategoricalIndex, pd.IntervalIndex)):683 pytest.skip("skipping tests for {}".format(type(index)))684 identity = mapper(index.values, index)685 # we don't infer to UInt64 for a dict686 if isinstance(index, pd.UInt64Index) and isinstance(identity, dict):687 expected = index.astype("int64")688 else:689 expected = index690 result = index.map(identity)691 tm.assert_index_equal(result, expected)692 # empty mappable693 expected = pd.Index([np.nan] * len(index))694 result = index.map(mapper(expected, index))695 tm.assert_index_equal(result, expected)696 def test_putmask_with_wrong_mask(self):697 # GH18368698 index = self.create_index()699 with pytest.raises(ValueError):700 index.putmask(np.ones(len(index) + 1, np.bool), 1)701 with pytest.raises(ValueError):702 index.putmask(np.ones(len(index) - 1, np.bool), 1)703 with pytest.raises(ValueError):704 index.putmask("foo", 1)705 @pytest.mark.parametrize("copy", [True, False])706 @pytest.mark.parametrize("name", [None, "foo"])707 @pytest.mark.parametrize("ordered", [True, False])708 def test_astype_category(self, copy, name, ordered):709 # GH 18630710 index = self.create_index()711 if name:712 index = index.rename(name)713 # standard categories714 dtype = CategoricalDtype(ordered=ordered)715 result = index.astype(dtype, copy=copy)716 expected = CategoricalIndex(index.values, name=name, ordered=ordered)717 tm.assert_index_equal(result, expected)718 # non-standard categories719 dtype = CategoricalDtype(index.unique().tolist()[:-1], ordered)720 result = index.astype(dtype, copy=copy)721 expected = CategoricalIndex(index.values, name=name, dtype=dtype)722 tm.assert_index_equal(result, expected)723 if ordered is False:724 # dtype='category' defaults to ordered=False, so only test once725 result = index.astype("category", copy=copy)726 expected = CategoricalIndex(index.values, name=name)727 tm.assert_index_equal(result, expected)728 def test_is_unique(self):729 # initialize a unique index730 index = self.create_index().drop_duplicates()731 assert index.is_unique is True732 # empty index should be unique733 index_empty = index[:0]734 assert index_empty.is_unique is True735 # test basic dupes736 index_dup = index.insert(0, index[0])737 assert index_dup.is_unique is False738 # single NA should be unique739 index_na = index.insert(0, np.nan)740 assert index_na.is_unique is True741 # multiple NA should not be unique742 index_na_dup = index_na.insert(0, np.nan)...

Full Screen

Full Screen

conway.py

Source:conway.py Github

copy

Full Screen

...232 y = 0233 checkForRule = lines[valueLineNumber].find(", rule")234 if checkForRule == -1:235 #no rule 236 commaIndex = lines[valueLineNumber].index(',')237 x = int(lines[valueLineNumber][4:commaIndex])238 y = int(lines[valueLineNumber][commaIndex + 5:])239 else:240 subs = lines[valueLineNumber][0:checkForRule]241 commaIndex = subs.index(',')242 x = int(subs[4:commaIndex])243 y = int(subs[commaIndex + 5:])244 if x >= self.rows or y >= self.cols:245 raise Exception("Pattern to big to fit in grid")246 if x + position[0] >= self.cols or y + position[1] >= self.rows:247 raise Exception("Pattern unable to fit pattern at that position")248 joinedLines = "".join(lines[valueLineNumber+1:]).split("$")249 i = 0250 while i < y:251 j = 0252 xindex = 0253 while j < len(joinedLines[i]):254 #has run_counter255 run_counter = 1...

Full Screen

Full Screen

test_setops.py

Source:test_setops.py Github

copy

Full Screen

...7 return request.param8@pytest.fixture(params=[None, False])9def sort(request):10 return request.param11def monotonic_index(start, end, dtype="int64", closed="right"):12 return IntervalIndex.from_breaks(np.arange(start, end, dtype=dtype), closed=closed)13def empty_index(dtype="int64", closed="right"):14 return IntervalIndex(np.array([], dtype=dtype), closed=closed)15class TestIntervalIndex:16 def test_union(self, closed, sort):17 index = monotonic_index(0, 11, closed=closed)18 other = monotonic_index(5, 13, closed=closed)19 expected = monotonic_index(0, 13, closed=closed)20 result = index[::-1].union(other, sort=sort)21 if sort is None:22 tm.assert_index_equal(result, expected)23 assert tm.equalContents(result, expected)24 result = other[::-1].union(index, sort=sort)25 if sort is None:26 tm.assert_index_equal(result, expected)27 assert tm.equalContents(result, expected)28 tm.assert_index_equal(index.union(index, sort=sort), index)29 tm.assert_index_equal(index.union(index[:1], sort=sort), index)30 # GH 19101: empty result, same dtype31 index = empty_index(dtype="int64", closed=closed)32 result = index.union(index, sort=sort)33 tm.assert_index_equal(result, index)34 # GH 19101: empty result, different dtypes35 other = empty_index(dtype="float64", closed=closed)36 result = index.union(other, sort=sort)37 tm.assert_index_equal(result, index)38 def test_intersection(self, closed, sort):39 index = monotonic_index(0, 11, closed=closed)40 other = monotonic_index(5, 13, closed=closed)41 expected = monotonic_index(5, 11, closed=closed)42 result = index[::-1].intersection(other, sort=sort)43 if sort is None:44 tm.assert_index_equal(result, expected)45 assert tm.equalContents(result, expected)46 result = other[::-1].intersection(index, sort=sort)47 if sort is None:48 tm.assert_index_equal(result, expected)49 assert tm.equalContents(result, expected)50 tm.assert_index_equal(index.intersection(index, sort=sort), index)51 # GH 19101: empty result, same dtype52 other = monotonic_index(300, 314, closed=closed)53 expected = empty_index(dtype="int64", closed=closed)54 result = index.intersection(other, sort=sort)55 tm.assert_index_equal(result, expected)56 # GH 19101: empty result, different dtypes57 other = monotonic_index(300, 314, dtype="float64", closed=closed)58 result = index.intersection(other, sort=sort)59 tm.assert_index_equal(result, expected)60 # GH 26225: nested intervals61 index = IntervalIndex.from_tuples([(1, 2), (1, 3), (1, 4), (0, 2)])62 other = IntervalIndex.from_tuples([(1, 2), (1, 3)])63 expected = IntervalIndex.from_tuples([(1, 2), (1, 3)])64 result = index.intersection(other)65 tm.assert_index_equal(result, expected)66 # GH 26225: duplicate element67 index = IntervalIndex.from_tuples([(1, 2), (1, 2), (2, 3), (3, 4)])68 other = IntervalIndex.from_tuples([(1, 2), (2, 3)])69 expected = IntervalIndex.from_tuples([(1, 2), (1, 2), (2, 3)])70 result = index.intersection(other)71 tm.assert_index_equal(result, expected)72 # GH 2622573 index = IntervalIndex.from_tuples([(0, 3), (0, 2)])74 other = IntervalIndex.from_tuples([(0, 2), (1, 3)])75 expected = IntervalIndex.from_tuples([(0, 2)])76 result = index.intersection(other)77 tm.assert_index_equal(result, expected)78 # GH 26225: duplicate nan element79 index = IntervalIndex([np.nan, np.nan])80 other = IntervalIndex([np.nan])81 expected = IntervalIndex([np.nan])82 result = index.intersection(other)83 tm.assert_index_equal(result, expected)84 def test_difference(self, closed, sort):85 index = IntervalIndex.from_arrays([1, 0, 3, 2], [1, 2, 3, 4], closed=closed)86 result = index.difference(index[:1], sort=sort)87 expected = index[1:]88 if sort is None:89 expected = expected.sort_values()90 tm.assert_index_equal(result, expected)91 # GH 19101: empty result, same dtype92 result = index.difference(index, sort=sort)93 expected = empty_index(dtype="int64", closed=closed)94 tm.assert_index_equal(result, expected)95 # GH 19101: empty result, different dtypes96 other = IntervalIndex.from_arrays(97 index.left.astype("float64"), index.right, closed=closed98 )99 result = index.difference(other, sort=sort)100 tm.assert_index_equal(result, expected)101 def test_symmetric_difference(self, closed, sort):102 index = monotonic_index(0, 11, closed=closed)103 result = index[1:].symmetric_difference(index[:-1], sort=sort)104 expected = IntervalIndex([index[0], index[-1]])105 if sort is None:106 tm.assert_index_equal(result, expected)107 assert tm.equalContents(result, expected)108 # GH 19101: empty result, same dtype109 result = index.symmetric_difference(index, sort=sort)110 expected = empty_index(dtype="int64", closed=closed)111 if sort is None:112 tm.assert_index_equal(result, expected)113 assert tm.equalContents(result, expected)114 # GH 19101: empty result, different dtypes115 other = IntervalIndex.from_arrays(116 index.left.astype("float64"), index.right, closed=closed117 )118 result = index.symmetric_difference(other, sort=sort)119 tm.assert_index_equal(result, expected)120 @pytest.mark.parametrize(121 "op_name", ["union", "intersection", "difference", "symmetric_difference"]122 )123 @pytest.mark.parametrize("sort", [None, False])124 def test_set_incompatible_types(self, closed, op_name, sort):125 index = monotonic_index(0, 11, closed=closed)126 set_op = getattr(index, op_name)127 # TODO: standardize return type of non-union setops type(self vs other)128 # non-IntervalIndex129 if op_name == "difference":130 expected = index131 else:132 expected = getattr(index.astype("O"), op_name)(Index([1, 2, 3]))133 result = set_op(Index([1, 2, 3]), sort=sort)134 tm.assert_index_equal(result, expected)135 # mixed closed136 msg = (137 "can only do set operations between two IntervalIndex objects "138 "that are closed on the same side"139 )140 for other_closed in {"right", "left", "both", "neither"} - {closed}:141 other = monotonic_index(0, 11, closed=other_closed)142 with pytest.raises(ValueError, match=msg):143 set_op(other, sort=sort)144 # GH 19016: incompatible dtypes145 other = interval_range(Timestamp("20180101"), periods=9, closed=closed)146 msg = (147 "can only do {op} between two IntervalIndex objects that have "148 "compatible dtypes"149 ).format(op=op_name)150 with pytest.raises(TypeError, match=msg):...

Full Screen

Full Screen

test_period.py

Source:test_period.py Github

copy

Full Screen

...32 rs = Index(df["Index"])33 tm.assert_index_equal(rs, rng, check_names=False)34 assert rs.name == "Index"35 assert rng.name == "index"36 rs = df.reset_index().set_index("index")37 assert isinstance(rs.index, PeriodIndex)38 tm.assert_index_equal(rs.index, rng)39 def test_frame_to_time_stamp(self):40 K = 541 index = period_range(freq="A", start="1/1/2001", end="12/1/2009")42 df = DataFrame(np.random.randn(len(index), K), index=index)43 df["mix"] = "a"44 exp_index = date_range("1/1/2001", end="12/31/2009", freq="A-DEC")45 exp_index = exp_index + Timedelta(1, "D") - Timedelta(1, "ns")46 result = df.to_timestamp("D", "end")47 tm.assert_index_equal(result.index, exp_index)48 tm.assert_numpy_array_equal(result.values, df.values)49 exp_index = date_range("1/1/2001", end="1/1/2009", freq="AS-JAN")50 result = df.to_timestamp("D", "start")...

Full Screen

Full Screen

test_indexed_read.py

Source:test_indexed_read.py Github

copy

Full Screen

1import ctypes2import ecl3from ecl import EclPrototype4from ecl import EclDataType5from ecl.eclfile import EclKW, EclFile, FortIO6from ecl.util.test import TestAreaContext7from tests import EclTest8from ecl.util.util import IntVector9class EclIndexedReadTest(EclTest):10 _freadIndexedData = EclPrototype("void ecl_kw_fread_indexed_data_python(fortio, int, ecl_data_type, int, int_vector, char*)", bind = False) # fortio, offset, type, count, index_map, buffer11 _eclFileIndexedRead = EclPrototype("void ecl_file_indexed_read(ecl_file, char*, int, int_vector, char*)", bind = False) # ecl_file, kw, index, index_map, buffer12 def test_ecl_kw_indexed_read(self):13 with TestAreaContext("ecl_kw_indexed_read") as area:14 fortio = FortIO("index_test", mode=FortIO.WRITE_MODE)15 element_count = 10000016 ecl_kw = EclKW("TEST", element_count, EclDataType.ECL_INT)17 for index in range(element_count):18 ecl_kw[index] = index19 ecl_kw.fwrite(fortio)20 fortio.close()21 fortio = FortIO("index_test", mode=FortIO.READ_MODE)22 new_ecl_kw = EclKW.fread(fortio)23 for index in range(element_count):24 self.assertEqual(new_ecl_kw[index], index)25 index_map = IntVector()26 index_map.append(2)27 index_map.append(3)28 index_map.append(5)29 index_map.append(7)30 index_map.append(11)31 index_map.append(13)32 index_map.append(313)33 index_map.append(1867)34 index_map.append(5227)35 index_map.append(7159)36 index_map.append(12689)37 index_map.append(18719)38 index_map.append(32321)39 index_map.append(37879)40 index_map.append(54167)41 index_map.append(77213)42 index_map.append(88843)43 index_map.append(99991)44 char_buffer = ctypes.create_string_buffer(len(index_map) * ctypes.sizeof(ctypes.c_int))45 self._freadIndexedData(fortio, 24, EclDataType.ECL_INT, element_count, index_map, char_buffer)46 int_buffer = ctypes.cast(char_buffer, ctypes.POINTER(ctypes.c_int))47 for index, index_map_value in enumerate(index_map):48 self.assertEqual(index_map_value, int_buffer[index])49 def test_ecl_file_indexed_read(self):50 with TestAreaContext("ecl_file_indexed_read") as area:51 fortio = FortIO("ecl_file_index_test", mode=FortIO.WRITE_MODE)52 element_count = 10000053 ecl_kw_1 = EclKW("TEST1", element_count, EclDataType.ECL_INT)54 ecl_kw_2 = EclKW("TEST2", element_count, EclDataType.ECL_INT)55 for index in range(element_count):56 ecl_kw_1[index] = index57 ecl_kw_2[index] = index + 358 ecl_kw_1.fwrite(fortio)59 ecl_kw_2.fwrite(fortio)60 fortio.close()61 ecl_file = EclFile("ecl_file_index_test")62 index_map = IntVector()63 index_map.append(2)64 index_map.append(3)65 index_map.append(5)66 index_map.append(7)67 index_map.append(11)68 index_map.append(13)69 index_map.append(313)70 index_map.append(1867)71 index_map.append(5227)72 index_map.append(7159)73 index_map.append(12689)74 index_map.append(18719)75 index_map.append(32321)76 index_map.append(37879)77 index_map.append(54167)78 index_map.append(77213)79 index_map.append(88843)80 index_map.append(99991)81 char_buffer_1 = ctypes.create_string_buffer(len(index_map) * ctypes.sizeof(ctypes.c_int))82 char_buffer_2 = ctypes.create_string_buffer(len(index_map) * ctypes.sizeof(ctypes.c_int))83 self._eclFileIndexedRead(ecl_file, "TEST2", 0, index_map, char_buffer_2)84 self._eclFileIndexedRead(ecl_file, "TEST1", 0, index_map, char_buffer_1)85 int_buffer_1 = ctypes.cast(char_buffer_1, ctypes.POINTER(ctypes.c_int))86 int_buffer_2 = ctypes.cast(char_buffer_2, ctypes.POINTER(ctypes.c_int))87 for index, index_map_value in enumerate(index_map):88 self.assertEqual(index_map_value, int_buffer_1[index])...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var DeviceFarmer = require('devicefarmer-stf-client');2var deviceFarmer = new DeviceFarmer({host: 'localhost', port: 7100});3deviceFarmer.index(function(err, devices) {4 console.log('Devices: ' + devices);5});6var DeviceFarmer = require('devicefarmer-stf-client');7var deviceFarmer = new DeviceFarmer({host: 'localhost', port: 7100});8deviceFarmer.index(function(err, devices) {9 console.log('Devices: ' + devices);10});11var DeviceFarmer = require('devicefarmer-stf-client');12var deviceFarmer = new DeviceFarmer({host: 'localhost', port: 7100});13deviceFarmer.index(function(err, devices) {14 console.log('Devices: ' + devices);15});16var DeviceFarmer = require('devicefarmer-stf-client');17var deviceFarmer = new DeviceFarmer({host: 'localhost', port: 7100});18deviceFarmer.index(function(err, devices) {19 console.log('Devices: ' + devices);20});21var DeviceFarmer = require('devicefarmer-stf-client');22var deviceFarmer = new DeviceFarmer({host: 'localhost', port: 7100});23deviceFarmer.index(function(err, devices) {24 console.log('Devices: ' + devices);25});26var DeviceFarmer = require('devicefarmer-stf-client');27var deviceFarmer = new DeviceFarmer({host: 'localhost', port: 7100});28deviceFarmer.index(function(err, devices) {29 console.log('Devices: ' + devices);30});31var DeviceFarmer = require('devicefarmer-stf-client');32var deviceFarmer = new DeviceFarmer({host: 'localhost', port: 7100});33deviceFarmer.index(function(err

Full Screen

Using AI Code Generation

copy

Full Screen

1var stf = require('devicefarmer-stf-client');2var client = new stf.Client();3client.index().then(function(devices) {4 console.log(devices);5});6var stf = require('devicefarmer-stf-client');7var client = new stf.Client();8client.index().then(function(devices) {9 console.log(devices);10});11var stf = require('devicefarmer-stf-client');12var client = new stf.Client();13client.index().then(function(devices) {14 console.log(devices);15});16var stf = require('devicefarmer-stf-client');17var client = new stf.Client();18client.index().then(function(devices) {19 console.log(devices);20});21var stf = require('devicefarmer-stf-client');22var client = new stf.Client();23client.index().then(function(devices) {24 console.log(devices);25});26var stf = require('devicefarmer-stf-client');27var client = new stf.Client();28client.index().then(function(devices) {29 console.log(devices);30});31var stf = require('devicefarmer-stf-client');32var client = new stf.Client();33client.index().then(function(devices) {34 console.log(devices);35});36var stf = require('devicefarmer-stf-client');37var client = new stf.Client();38client.index().then(function(devices) {39 console.log(devices);40});41var stf = require('devicefarmer-stf-client');42var client = new stf.Client();43client.index().then(function(devices) {44 console.log(devices);45});46var stf = require('devicefarmer-stf-client');47var client = new stf.Client();48client.index().then(function(devices) {49 console.log(devices);50});

Full Screen

Using AI Code Generation

copy

Full Screen

1var client = require('devicefarmer-stf-client');2var DeviceFarm = client.DeviceFarm;3farm.index(function(err, devices) {4 if (err) {5 console.log(err);6 } else {7 console.log(devices);8 }9});10var client = require('devicefarmer-stf-client');11var DeviceFarm = client.DeviceFarm;12var device = farm.getDevice('123456789');13device.use(function(err, device) {14 if (err) {15 console.log(err);16 } else {17 console.log(device);18 }19});20var client = require('devicefarmer-stf-client');21var DeviceFarm = client.DeviceFarm;22var device = farm.getDevice('123456789');23device.use(function(err, device) {24 if (err) {25 console.log(err);26 } else {27 device.getDevice(function(err, device) {28 if (err) {29 console.log(err);30 } else {31 console.log(device);32 }33 });34 }35});36var client = require('devicefarmer-stf-client');37var DeviceFarm = client.DeviceFarm;38var device = farm.getDevice('123456789');39device.use(function(err, device) {40 if (err) {41 console.log(err);42 } else {43 device.getDevice(function(err, device) {44 if (err) {45 console.log(err);46 } else {47 console.log(device);48 }49 });50 }51});52var client = require('devicefarmer-stf-client');53var DeviceFarm = client.DeviceFarm;54var device = farm.getDevice('123456789');55device.use(function(err, device) {56 if (err) {57 console.log(err);

Full Screen

Using AI Code Generation

copy

Full Screen

1var devicefarm = require('devicefarmer-stf-client');2client.index(function(err, res) {3 if (err) {4 console.log(err);5 } else {6 console.log(res);7 }8});9{ devices:10 [ { serial: '0123456789ABCDEF',11 battery: { level: 100, isCharging: false },12 display: { width: 1080, height: 1920, xdpi: 441, ydpi: 440, fps: 60, density: 480, rotation: 0, secure: true },13 phone: { imei: '0123456789ABCDEF', imsi: '0123456789ABCDEF', phoneNumber: '0123456789ABCDEF', manufacturer: 'motorola', model: 'XT1032', network: 'LTE' },14 product: { name: 'osprey_umts', model: 'XT1032', brand: 'motorola', manufacturer: 'motorola', device: 'osprey', board: 'msm8960', hardware: 'qcom', serial: '0123456789ABCDEF', androidVersion: '4.4.4', androidSdkVersion: 19, carrier: 'T-Mobile' },15 cpu: { abi: [ 'armeabi-v7a', 'armeabi' ], abi2: null, features: [ 'swp', 'half', 'thumb', 'fastmult', 'vfpv3', 'neon', 'vfpv4', 'idiva', 'idivt' ] },16 memory: { ram: 1863, disk: 0 },17 network: { wifi: true, wifiIp: '

Full Screen

Using AI Code Generation

copy

Full Screen

1var stf = require('devicefarmer-stf-api');2var index = stf.index;3index(function(response){4 console.log(response);5});6var stf = require('devicefarmer-stf-api');7var index = stf.index;8index(function(response){9 console.log(response);10});

Full Screen

Using AI Code Generation

copy

Full Screen

1var df = require('devicefarmer-stf-api');2client.index(function(err, data) {3 console.log(data);4});5var df = require('devicefarmer-stf-api');6client.index(function(err, data) {7 console.log(data);8});9var df = require('devicefarmer-stf-api');10client.index(function(err, data) {11 console.log(data);12});13var df = require('devicefarmer-stf-api');14client.index(function(err, data) {15 console.log(data);16});17var df = require('devicefarmer-stf-api');18client.index(function(err, data) {19 console.log(data);20});21var df = require('devicefarmer-stf-api');22client.index(function(err, data) {23 console.log(data);24});25var df = require('devicefarmer-stf-api');26client.index(function(err, data) {27 console.log(data);28});29var df = require('devicefarmer-stf-api');30client.index(function(err, data) {31 console.log(data);32});33var df = require('devicefarmer-stf-api');

Full Screen

Using AI Code Generation

copy

Full Screen

1var devicefarmer = require('devicefarmer-stf');2var path = require('path');3var fs = require('fs');4var device = new devicefarmer.Device();5var options = {6};7device.index(options, function(err, data) {8 if (err) {9 console.error(err);10 } else {11 console.log(data);12 }13});14var devicefarmer = require('devicefarmer-stf');15var path = require('path');16var fs = require('fs');17var device = new devicefarmer.Device();18var options = {19};20device.index(options, function(err, data) {21 if (err) {22 console.error(err);23 } else {24 console.log(data);25 }26});27var devicefarmer = require('devicefarmer-stf');28var path = require('path');29var fs = require('fs');30var device = new devicefarmer.Device();31var options = {

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run devicefarmer-stf automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful