Skip to content

STYLE: Inconsistent namespace - Indexes (#39992) #40001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def test_numpy_repeat(self):
@pytest.mark.parametrize("klass", [list, tuple, np.array, Series])
def test_where(self, klass):
i = self.create_index()
if isinstance(i, (pd.DatetimeIndex, pd.TimedeltaIndex)):
if isinstance(i, (DatetimeIndex, TimedeltaIndex)):
# where does not preserve freq
i = i._with_freq(None)

Expand Down Expand Up @@ -605,7 +605,7 @@ def test_map(self):
index = self.create_index()

# we don't infer UInt64
if isinstance(index, pd.UInt64Index):
if isinstance(index, UInt64Index):
expected = index.astype("int64")
else:
expected = index
Expand All @@ -624,13 +624,13 @@ def test_map(self):
def test_map_dictlike(self, mapper):

index = self.create_index()
if isinstance(index, pd.CategoricalIndex):
if isinstance(index, CategoricalIndex):
pytest.skip(f"skipping tests for {type(index)}")

identity = mapper(index.values, index)

# we don't infer to UInt64 for a dict
if isinstance(index, pd.UInt64Index) and isinstance(identity, dict):
if isinstance(index, UInt64Index) and isinstance(identity, dict):
expected = index.astype("int64")
else:
expected = index
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/datetimes/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ def test_astype_object_tz(self, tz):

def test_astype_object_with_nat(self):
idx = DatetimeIndex(
[datetime(2013, 1, 1), datetime(2013, 1, 2), pd.NaT, datetime(2013, 1, 4)],
[datetime(2013, 1, 1), datetime(2013, 1, 2), NaT, datetime(2013, 1, 4)],
name="idx",
)
expected_list = [
Timestamp("2013-01-01"),
Timestamp("2013-01-02"),
pd.NaT,
NaT,
Timestamp("2013-01-04"),
]
expected = Index(expected_list, dtype=object, name="idx")
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,8 @@ def test_construction_outofbounds(self):
def test_construction_with_ndarray(self):
# GH 5152
dates = [datetime(2013, 10, 7), datetime(2013, 10, 8), datetime(2013, 10, 9)]
data = DatetimeIndex(dates, freq=pd.offsets.BDay()).values
result = DatetimeIndex(data, freq=pd.offsets.BDay())
data = DatetimeIndex(dates, freq=offsets.BDay()).values
result = DatetimeIndex(data, freq=offsets.BDay())
expected = DatetimeIndex(["2013-10-07", "2013-10-08", "2013-10-09"], freq="B")
tm.assert_index_equal(result, expected)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/datetimes/test_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ def test_range_with_millisecond_resolution(self, start_end):

def test_date_range_with_custom_holidays():
# GH 30593
freq = pd.offsets.CustomBusinessHour(start="15:00", holidays=["2020-11-26"])
freq = offsets.CustomBusinessHour(start="15:00", holidays=["2020-11-26"])
result = date_range(start="2020-11-25 15:00", periods=4, freq=freq)
expected = DatetimeIndex(
[
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def test_asarray_tz_naive(self):
# optionally, object
result = np.asarray(idx, dtype=object)

expected = np.array([pd.Timestamp("2000-01-01"), pd.Timestamp("2000-01-02")])
expected = np.array([Timestamp("2000-01-01"), Timestamp("2000-01-02")])
tm.assert_numpy_array_equal(result, expected)

def test_asarray_tz_aware(self):
Expand All @@ -235,7 +235,7 @@ def test_asarray_tz_aware(self):

# Future behavior with no warning
expected = np.array(
[pd.Timestamp("2000-01-01", tz=tz), pd.Timestamp("2000-01-02", tz=tz)]
[Timestamp("2000-01-01", tz=tz), Timestamp("2000-01-02", tz=tz)]
)
result = np.asarray(idx, dtype=object)

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/multi/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_to_frame():

def test_to_frame_dtype_fidelity():
# GH 22420
mi = pd.MultiIndex.from_arrays(
mi = MultiIndex.from_arrays(
[
pd.date_range("19910905", periods=6, tz="US/Eastern"),
[1, 1, 1, 2, 2, 2],
Expand Down Expand Up @@ -119,7 +119,7 @@ def test_to_frame_dtype_fidelity():
def test_to_frame_resulting_column_order():
# GH 22420
expected = ["z", 0, "a"]
mi = pd.MultiIndex.from_arrays(
mi = MultiIndex.from_arrays(
[["a", "b", "c"], ["x", "y", "z"], ["q", "w", "e"]], names=expected
)
result = mi.to_frame().columns.tolist()
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/multi/test_drop.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def test_single_level_drop_partially_missing_elements():

def test_droplevel_multiindex_one_level():
# GH#37208
index = pd.MultiIndex.from_tuples([(2,)], names=("b",))
index = MultiIndex.from_tuples([(2,)], names=("b",))
result = index.droplevel([])
expected = pd.Int64Index([2], name="b")
tm.assert_index_equal(result, expected)
6 changes: 3 additions & 3 deletions pandas/tests/indexes/multi/test_equivalence.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ def test_equals_missing_values():

def test_equals_missing_values_differently_sorted():
# GH#38439
mi1 = pd.MultiIndex.from_tuples([(81.0, np.nan), (np.nan, np.nan)])
mi2 = pd.MultiIndex.from_tuples([(np.nan, np.nan), (81.0, np.nan)])
mi1 = MultiIndex.from_tuples([(81.0, np.nan), (np.nan, np.nan)])
mi2 = MultiIndex.from_tuples([(np.nan, np.nan), (81.0, np.nan)])
assert not mi1.equals(mi2)

mi2 = pd.MultiIndex.from_tuples([(81.0, np.nan), (np.nan, np.nan)])
mi2 = MultiIndex.from_tuples([(81.0, np.nan), (np.nan, np.nan)])
assert mi1.equals(mi2)


Expand Down
13 changes: 6 additions & 7 deletions pandas/tests/indexes/multi/test_join.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import numpy as np
import pytest

import pandas as pd
from pandas import (
Index,
MultiIndex,
Expand Down Expand Up @@ -54,12 +53,12 @@ def test_join_self(idx, join_type):

def test_join_multi():
# GH 10665
midx = pd.MultiIndex.from_product([np.arange(4), np.arange(4)], names=["a", "b"])
midx = MultiIndex.from_product([np.arange(4), np.arange(4)], names=["a", "b"])
idx = Index([1, 2, 5], name="b")

# inner
jidx, lidx, ridx = midx.join(idx, how="inner", return_indexers=True)
exp_idx = pd.MultiIndex.from_product([np.arange(4), [1, 2]], names=["a", "b"])
exp_idx = MultiIndex.from_product([np.arange(4), [1, 2]], names=["a", "b"])
exp_lidx = np.array([1, 2, 5, 6, 9, 10, 13, 14], dtype=np.intp)
exp_ridx = np.array([0, 1, 0, 1, 0, 1, 0, 1], dtype=np.intp)
tm.assert_index_equal(jidx, exp_idx)
Expand Down Expand Up @@ -96,8 +95,8 @@ def test_join_multi_wrong_order():
# GH 25760
# GH 28956

midx1 = pd.MultiIndex.from_product([[1, 2], [3, 4]], names=["a", "b"])
midx2 = pd.MultiIndex.from_product([[1, 2], [3, 4]], names=["b", "a"])
midx1 = MultiIndex.from_product([[1, 2], [3, 4]], names=["a", "b"])
midx2 = MultiIndex.from_product([[1, 2], [3, 4]], names=["b", "a"])

join_idx, lidx, ridx = midx1.join(midx2, return_indexers=True)

Expand All @@ -111,8 +110,8 @@ def test_join_multi_wrong_order():
def test_join_multi_return_indexers():
# GH 34074

midx1 = pd.MultiIndex.from_product([[1, 2], [3, 4], [5, 6]], names=["a", "b", "c"])
midx2 = pd.MultiIndex.from_product([[1, 2], [3, 4]], names=["a", "b"])
midx1 = MultiIndex.from_product([[1, 2], [3, 4], [5, 6]], names=["a", "b", "c"])
midx2 = MultiIndex.from_product([[1, 2], [3, 4]], names=["a", "b"])

result = midx1.join(midx2, return_indexers=False)
tm.assert_index_equal(result, midx1)
10 changes: 5 additions & 5 deletions pandas/tests/indexes/multi/test_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_reindex_preserves_names_when_target_is_list_or_ndarray(idx):
target = idx.copy()
idx.names = target.names = [None, None]

other_dtype = pd.MultiIndex.from_product([[1, 2], [3, 4]])
other_dtype = MultiIndex.from_product([[1, 2], [3, 4]])

# list & ndarray cases
assert idx.reindex([])[0].names == [None, None]
Expand All @@ -73,14 +73,14 @@ def test_reindex_preserves_names_when_target_is_list_or_ndarray(idx):

def test_reindex_lvl_preserves_names_when_target_is_list_or_array():
# GH7774
idx = pd.MultiIndex.from_product([[0, 1], ["a", "b"]], names=["foo", "bar"])
idx = MultiIndex.from_product([[0, 1], ["a", "b"]], names=["foo", "bar"])
assert idx.reindex([], level=0)[0].names == ["foo", "bar"]
assert idx.reindex([], level=1)[0].names == ["foo", "bar"]


def test_reindex_lvl_preserves_type_if_target_is_empty_list_or_array():
# GH7774
idx = pd.MultiIndex.from_product([[0, 1], ["a", "b"]])
idx = MultiIndex.from_product([[0, 1], ["a", "b"]])
assert idx.reindex([], level=0)[0].levels[0].dtype.type == np.int64
assert idx.reindex([], level=1)[0].levels[1].dtype.type == np.object_

Expand All @@ -97,9 +97,9 @@ def test_reindex_base(idx):


def test_reindex_non_unique():
idx = pd.MultiIndex.from_tuples([(0, 0), (1, 1), (1, 1), (2, 2)])
idx = MultiIndex.from_tuples([(0, 0), (1, 1), (1, 1), (2, 2)])
a = pd.Series(np.arange(4), index=idx)
new_idx = pd.MultiIndex.from_tuples([(0, 0), (1, 1), (2, 2)])
new_idx = MultiIndex.from_tuples([(0, 0), (1, 1), (2, 2)])

msg = "cannot handle a non-unique multi-index!"
with pytest.raises(ValueError, match=msg):
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/multi/test_reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ def test_insert(idx):
+ [("test", 17), ("test", 18)]
)

left = pd.Series(np.linspace(0, 10, 11), pd.MultiIndex.from_tuples(idx[:-2]))
left = pd.Series(np.linspace(0, 10, 11), MultiIndex.from_tuples(idx[:-2]))

left.loc[("test", 17)] = 11
left.loc[("test", 18)] = 12

right = pd.Series(np.linspace(0, 12, 13), pd.MultiIndex.from_tuples(idx))
right = pd.Series(np.linspace(0, 12, 13), MultiIndex.from_tuples(idx))

tm.assert_series_equal(left, right)

Expand Down
42 changes: 21 additions & 21 deletions pandas/tests/indexes/multi/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_difference(idx, sort):

# name from non-empty array
result = first.difference([("foo", "one")], sort=sort)
expected = pd.MultiIndex.from_tuples(
expected = MultiIndex.from_tuples(
[("bar", "one"), ("baz", "two"), ("foo", "two"), ("qux", "one"), ("qux", "two")]
)
expected.names = first.names
Expand All @@ -193,7 +193,7 @@ def test_difference(idx, sort):

def test_difference_sort_special():
# GH-24959
idx = pd.MultiIndex.from_product([[1, 0], ["a", "b"]])
idx = MultiIndex.from_product([[1, 0], ["a", "b"]])
# sort=None, the default
result = idx.difference([])
tm.assert_index_equal(result, idx)
Expand All @@ -202,17 +202,17 @@ def test_difference_sort_special():
@pytest.mark.xfail(reason="Not implemented.")
def test_difference_sort_special_true():
# TODO decide on True behaviour
idx = pd.MultiIndex.from_product([[1, 0], ["a", "b"]])
idx = MultiIndex.from_product([[1, 0], ["a", "b"]])
result = idx.difference([], sort=True)
expected = pd.MultiIndex.from_product([[0, 1], ["a", "b"]])
expected = MultiIndex.from_product([[0, 1], ["a", "b"]])
tm.assert_index_equal(result, expected)


def test_difference_sort_incomparable():
# GH-24959
idx = pd.MultiIndex.from_product([[1, pd.Timestamp("2000"), 2], ["a", "b"]])
idx = MultiIndex.from_product([[1, pd.Timestamp("2000"), 2], ["a", "b"]])

other = pd.MultiIndex.from_product([[3, pd.Timestamp("2000"), 4], ["c", "d"]])
other = MultiIndex.from_product([[3, pd.Timestamp("2000"), 4], ["c", "d"]])
# sort=None, the default
# MultiIndex.difference deviates here from other difference
# implementations in not catching the TypeError
Expand All @@ -226,8 +226,8 @@ def test_difference_sort_incomparable():


def test_difference_sort_incomparable_true():
idx = pd.MultiIndex.from_product([[1, pd.Timestamp("2000"), 2], ["a", "b"]])
other = pd.MultiIndex.from_product([[3, pd.Timestamp("2000"), 4], ["c", "d"]])
idx = MultiIndex.from_product([[1, pd.Timestamp("2000"), 2], ["a", "b"]])
other = MultiIndex.from_product([[3, pd.Timestamp("2000"), 4], ["c", "d"]])

msg = "The 'sort' keyword only takes the values of None or False; True was passed."
with pytest.raises(ValueError, match=msg):
Expand Down Expand Up @@ -332,23 +332,23 @@ def test_intersection_non_object(idx, sort):

def test_intersect_equal_sort():
# GH-24959
idx = pd.MultiIndex.from_product([[1, 0], ["a", "b"]])
idx = MultiIndex.from_product([[1, 0], ["a", "b"]])
tm.assert_index_equal(idx.intersection(idx, sort=False), idx)
tm.assert_index_equal(idx.intersection(idx, sort=None), idx)


@pytest.mark.xfail(reason="Not implemented.")
def test_intersect_equal_sort_true():
# TODO decide on True behaviour
idx = pd.MultiIndex.from_product([[1, 0], ["a", "b"]])
sorted_ = pd.MultiIndex.from_product([[0, 1], ["a", "b"]])
idx = MultiIndex.from_product([[1, 0], ["a", "b"]])
sorted_ = MultiIndex.from_product([[0, 1], ["a", "b"]])
tm.assert_index_equal(idx.intersection(idx, sort=True), sorted_)


@pytest.mark.parametrize("slice_", [slice(None), slice(0)])
def test_union_sort_other_empty(slice_):
# https://github.com/pandas-dev/pandas/issues/24959
idx = pd.MultiIndex.from_product([[1, 0], ["a", "b"]])
idx = MultiIndex.from_product([[1, 0], ["a", "b"]])

# default, sort=None
other = idx[slice_]
Expand All @@ -364,16 +364,16 @@ def test_union_sort_other_empty(slice_):
def test_union_sort_other_empty_sort(slice_):
# TODO decide on True behaviour
# # sort=True
idx = pd.MultiIndex.from_product([[1, 0], ["a", "b"]])
idx = MultiIndex.from_product([[1, 0], ["a", "b"]])
other = idx[:0]
result = idx.union(other, sort=True)
expected = pd.MultiIndex.from_product([[0, 1], ["a", "b"]])
expected = MultiIndex.from_product([[0, 1], ["a", "b"]])
tm.assert_index_equal(result, expected)


def test_union_sort_other_incomparable():
# https://github.com/pandas-dev/pandas/issues/24959
idx = pd.MultiIndex.from_product([[1, pd.Timestamp("2000")], ["a", "b"]])
idx = MultiIndex.from_product([[1, pd.Timestamp("2000")], ["a", "b"]])

# default, sort=None
with tm.assert_produces_warning(RuntimeWarning):
Expand All @@ -389,14 +389,14 @@ def test_union_sort_other_incomparable():
def test_union_sort_other_incomparable_sort():
# TODO decide on True behaviour
# # sort=True
idx = pd.MultiIndex.from_product([[1, pd.Timestamp("2000")], ["a", "b"]])
idx = MultiIndex.from_product([[1, pd.Timestamp("2000")], ["a", "b"]])
with pytest.raises(TypeError, match="Cannot compare"):
idx.union(idx[:1], sort=True)


def test_union_non_object_dtype_raises():
# GH#32646 raise NotImplementedError instead of less-informative error
mi = pd.MultiIndex.from_product([["a", "b"], [1, 2]])
mi = MultiIndex.from_product([["a", "b"], [1, 2]])

idx = mi.levels[1]

Expand All @@ -418,8 +418,8 @@ def test_union_empty_self_different_names():
"method", ["union", "intersection", "difference", "symmetric_difference"]
)
def test_setops_disallow_true(method):
idx1 = pd.MultiIndex.from_product([["a", "b"], [1, 2]])
idx2 = pd.MultiIndex.from_product([["b", "c"], [1, 2]])
idx1 = MultiIndex.from_product([["a", "b"], [1, 2]])
idx2 = MultiIndex.from_product([["b", "c"], [1, 2]])

with pytest.raises(ValueError, match="The 'sort' keyword only takes"):
getattr(idx1, method)(idx2, sort=True)
Expand Down Expand Up @@ -465,8 +465,8 @@ def test_intersect_with_duplicates(tuples, exp_tuples):
)
def test_maybe_match_names(data, names, expected):
# GH#38323
mi = pd.MultiIndex.from_tuples([], names=["a", "b"])
mi2 = pd.MultiIndex.from_tuples([data], names=names)
mi = MultiIndex.from_tuples([], names=["a", "b"])
mi2 = MultiIndex.from_tuples([data], names=names)
result = mi._maybe_match_names(mi2)
assert result == expected

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/period/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def test_difference_freq(self, sort):

def test_intersection_equal_duplicates(self):
# GH#38302
idx = pd.period_range("2011-01-01", periods=2)
idx = period_range("2011-01-01", periods=2)
idx_dup = idx.append(idx)
result = idx_dup.intersection(idx_dup)
tm.assert_index_equal(result, idx)
Loading