Skip to content

making namespace usage more consistent #37852

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 2 commits into from
Nov 16, 2020
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
102 changes: 54 additions & 48 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@
import pytest

import pandas as pd
from pandas import Index, Int64Index, Series, Timedelta, TimedeltaIndex, array
from pandas import (
Float64Index,
Index,
Int64Index,
RangeIndex,
Series,
Timedelta,
TimedeltaIndex,
UInt64Index,
array,
)
import pandas._testing as tm
from pandas.core import ops

Expand Down Expand Up @@ -43,7 +53,7 @@ def adjust_negative_zero(zero, expected):
# List comprehension has incompatible type List[PandasObject]; expected List[RangeIndex]
# See GH#29725
ser_or_index: List[Any] = [Series, Index]
lefts: List[Any] = [pd.RangeIndex(10, 40, 10)]
lefts: List[Any] = [RangeIndex(10, 40, 10)]
lefts.extend(
[
cls([10, 20, 30], dtype=dtype)
Expand Down Expand Up @@ -364,7 +374,7 @@ def test_divmod_zero(self, zero, numeric_idx):
@pytest.mark.parametrize("op", [operator.truediv, operator.floordiv])
def test_div_negative_zero(self, zero, numeric_idx, op):
# Check that -1 / -0.0 returns np.inf, not -np.inf
if isinstance(numeric_idx, pd.UInt64Index):
if isinstance(numeric_idx, UInt64Index):
return
idx = numeric_idx - 3

Expand Down Expand Up @@ -634,15 +644,15 @@ def test_mul_int_array(self, numeric_idx):
result = idx * np.array(5, dtype="int64")
tm.assert_index_equal(result, idx * 5)

arr_dtype = "uint64" if isinstance(idx, pd.UInt64Index) else "int64"
arr_dtype = "uint64" if isinstance(idx, UInt64Index) else "int64"
result = idx * np.arange(5, dtype=arr_dtype)
tm.assert_index_equal(result, didx)

def test_mul_int_series(self, numeric_idx):
idx = numeric_idx
didx = idx * idx

arr_dtype = "uint64" if isinstance(idx, pd.UInt64Index) else "int64"
arr_dtype = "uint64" if isinstance(idx, UInt64Index) else "int64"
result = idx * Series(np.arange(5, dtype=arr_dtype))
tm.assert_series_equal(result, Series(didx))

Expand All @@ -657,7 +667,7 @@ def test_mul_float_series(self, numeric_idx):
def test_mul_index(self, numeric_idx):
# in general not true for RangeIndex
idx = numeric_idx
if not isinstance(idx, pd.RangeIndex):
if not isinstance(idx, RangeIndex):
result = idx * idx
tm.assert_index_equal(result, idx ** 2)

Expand All @@ -680,7 +690,7 @@ def test_pow_float(self, op, numeric_idx, box_with_array):
# test power calculations both ways, GH#14973
box = box_with_array
idx = numeric_idx
expected = pd.Float64Index(op(idx.values, 2.0))
expected = Float64Index(op(idx.values, 2.0))

idx = tm.box_expected(idx, box)
expected = tm.box_expected(expected, box)
Expand Down Expand Up @@ -1040,74 +1050,70 @@ def test_series_divmod_zero(self):
class TestUFuncCompat:
@pytest.mark.parametrize(
"holder",
[pd.Int64Index, pd.UInt64Index, pd.Float64Index, pd.RangeIndex, Series],
[Int64Index, UInt64Index, Float64Index, RangeIndex, Series],
)
def test_ufunc_compat(self, holder):
box = Series if holder is Series else Index

if holder is pd.RangeIndex:
idx = pd.RangeIndex(0, 5)
if holder is RangeIndex:
idx = RangeIndex(0, 5)
else:
idx = holder(np.arange(5, dtype="int64"))
result = np.sin(idx)
expected = box(np.sin(np.arange(5, dtype="int64")))
tm.assert_equal(result, expected)

@pytest.mark.parametrize(
"holder", [pd.Int64Index, pd.UInt64Index, pd.Float64Index, Series]
)
@pytest.mark.parametrize("holder", [Int64Index, UInt64Index, Float64Index, Series])
def test_ufunc_coercions(self, holder):
idx = holder([1, 2, 3, 4, 5], name="x")
box = Series if holder is Series else Index

result = np.sqrt(idx)
assert result.dtype == "f8" and isinstance(result, box)
exp = pd.Float64Index(np.sqrt(np.array([1, 2, 3, 4, 5])), name="x")
exp = Float64Index(np.sqrt(np.array([1, 2, 3, 4, 5])), name="x")
exp = tm.box_expected(exp, box)
tm.assert_equal(result, exp)

result = np.divide(idx, 2.0)
assert result.dtype == "f8" and isinstance(result, box)
exp = pd.Float64Index([0.5, 1.0, 1.5, 2.0, 2.5], name="x")
exp = Float64Index([0.5, 1.0, 1.5, 2.0, 2.5], name="x")
exp = tm.box_expected(exp, box)
tm.assert_equal(result, exp)

# _evaluate_numeric_binop
result = idx + 2.0
assert result.dtype == "f8" and isinstance(result, box)
exp = pd.Float64Index([3.0, 4.0, 5.0, 6.0, 7.0], name="x")
exp = Float64Index([3.0, 4.0, 5.0, 6.0, 7.0], name="x")
exp = tm.box_expected(exp, box)
tm.assert_equal(result, exp)

result = idx - 2.0
assert result.dtype == "f8" and isinstance(result, box)
exp = pd.Float64Index([-1.0, 0.0, 1.0, 2.0, 3.0], name="x")
exp = Float64Index([-1.0, 0.0, 1.0, 2.0, 3.0], name="x")
exp = tm.box_expected(exp, box)
tm.assert_equal(result, exp)

result = idx * 1.0
assert result.dtype == "f8" and isinstance(result, box)
exp = pd.Float64Index([1.0, 2.0, 3.0, 4.0, 5.0], name="x")
exp = Float64Index([1.0, 2.0, 3.0, 4.0, 5.0], name="x")
exp = tm.box_expected(exp, box)
tm.assert_equal(result, exp)

result = idx / 2.0
assert result.dtype == "f8" and isinstance(result, box)
exp = pd.Float64Index([0.5, 1.0, 1.5, 2.0, 2.5], name="x")
exp = Float64Index([0.5, 1.0, 1.5, 2.0, 2.5], name="x")
exp = tm.box_expected(exp, box)
tm.assert_equal(result, exp)

@pytest.mark.parametrize(
"holder", [pd.Int64Index, pd.UInt64Index, pd.Float64Index, Series]
)
@pytest.mark.parametrize("holder", [Int64Index, UInt64Index, Float64Index, Series])
def test_ufunc_multiple_return_values(self, holder):
obj = holder([1, 2, 3], name="x")
box = Series if holder is Series else Index

result = np.modf(obj)
assert isinstance(result, tuple)
exp1 = pd.Float64Index([0.0, 0.0, 0.0], name="x")
exp2 = pd.Float64Index([1.0, 2.0, 3.0], name="x")
exp1 = Float64Index([0.0, 0.0, 0.0], name="x")
exp2 = Float64Index([1.0, 2.0, 3.0], name="x")
tm.assert_equal(result[0], tm.box_expected(exp1, box))
tm.assert_equal(result[1], tm.box_expected(exp2, box))

Expand Down Expand Up @@ -1173,12 +1179,12 @@ def check_binop(self, ops, scalars, idxs):
for op in ops:
for a, b in combinations(idxs, 2):
result = op(a, b)
expected = op(pd.Int64Index(a), pd.Int64Index(b))
expected = op(Int64Index(a), Int64Index(b))
tm.assert_index_equal(result, expected)
for idx in idxs:
for scalar in scalars:
result = op(idx, scalar)
expected = op(pd.Int64Index(idx), scalar)
expected = op(Int64Index(idx), scalar)
tm.assert_index_equal(result, expected)

def test_binops(self):
Expand All @@ -1191,10 +1197,10 @@ def test_binops(self):
]
scalars = [-1, 1, 2]
idxs = [
pd.RangeIndex(0, 10, 1),
pd.RangeIndex(0, 20, 2),
pd.RangeIndex(-10, 10, 2),
pd.RangeIndex(5, -5, -1),
RangeIndex(0, 10, 1),
RangeIndex(0, 20, 2),
RangeIndex(-10, 10, 2),
RangeIndex(5, -5, -1),
]
self.check_binop(ops, scalars, idxs)

Expand All @@ -1203,7 +1209,7 @@ def test_binops_pow(self):
# https://github.com/numpy/numpy/pull/8127
ops = [pow]
scalars = [1, 2]
idxs = [pd.RangeIndex(0, 10, 1), pd.RangeIndex(0, 20, 2)]
idxs = [RangeIndex(0, 10, 1), RangeIndex(0, 20, 2)]
self.check_binop(ops, scalars, idxs)

# TODO: mod, divmod?
Expand All @@ -1221,7 +1227,7 @@ def test_binops_pow(self):
def test_arithmetic_with_frame_or_series(self, op):
# check that we return NotImplemented when operating with Series
# or DataFrame
index = pd.RangeIndex(5)
index = RangeIndex(5)
other = Series(np.random.randn(5))

expected = op(Series(index), other)
Expand All @@ -1237,26 +1243,26 @@ def test_numeric_compat2(self):
# validate that we are handling the RangeIndex overrides to numeric ops
# and returning RangeIndex where possible

idx = pd.RangeIndex(0, 10, 2)
idx = RangeIndex(0, 10, 2)

result = idx * 2
expected = pd.RangeIndex(0, 20, 4)
expected = RangeIndex(0, 20, 4)
tm.assert_index_equal(result, expected, exact=True)

result = idx + 2
expected = pd.RangeIndex(2, 12, 2)
expected = RangeIndex(2, 12, 2)
tm.assert_index_equal(result, expected, exact=True)

result = idx - 2
expected = pd.RangeIndex(-2, 8, 2)
expected = RangeIndex(-2, 8, 2)
tm.assert_index_equal(result, expected, exact=True)

result = idx / 2
expected = pd.RangeIndex(0, 5, 1).astype("float64")
expected = RangeIndex(0, 5, 1).astype("float64")
tm.assert_index_equal(result, expected, exact=True)

result = idx / 4
expected = pd.RangeIndex(0, 10, 2) / 4
expected = RangeIndex(0, 10, 2) / 4
tm.assert_index_equal(result, expected, exact=True)

result = idx // 1
Expand All @@ -1269,25 +1275,25 @@ def test_numeric_compat2(self):
tm.assert_index_equal(result, expected, exact=True)

# __pow__
idx = pd.RangeIndex(0, 1000, 2)
idx = RangeIndex(0, 1000, 2)
result = idx ** 2
expected = idx._int64index ** 2
tm.assert_index_equal(Index(result.values), expected, exact=True)

# __floordiv__
cases_exact = [
(pd.RangeIndex(0, 1000, 2), 2, pd.RangeIndex(0, 500, 1)),
(pd.RangeIndex(-99, -201, -3), -3, pd.RangeIndex(33, 67, 1)),
(pd.RangeIndex(0, 1000, 1), 2, pd.RangeIndex(0, 1000, 1)._int64index // 2),
(RangeIndex(0, 1000, 2), 2, RangeIndex(0, 500, 1)),
(RangeIndex(-99, -201, -3), -3, RangeIndex(33, 67, 1)),
(RangeIndex(0, 1000, 1), 2, RangeIndex(0, 1000, 1)._int64index // 2),
(
pd.RangeIndex(0, 100, 1),
RangeIndex(0, 100, 1),
2.0,
pd.RangeIndex(0, 100, 1)._int64index // 2.0,
RangeIndex(0, 100, 1)._int64index // 2.0,
),
(pd.RangeIndex(0), 50, pd.RangeIndex(0)),
(pd.RangeIndex(2, 4, 2), 3, pd.RangeIndex(0, 1, 1)),
(pd.RangeIndex(-5, -10, -6), 4, pd.RangeIndex(-2, -1, 1)),
(pd.RangeIndex(-100, -200, 3), 2, pd.RangeIndex(0)),
(RangeIndex(0), 50, RangeIndex(0)),
(RangeIndex(2, 4, 2), 3, RangeIndex(0, 1, 1)),
(RangeIndex(-5, -10, -6), 4, RangeIndex(-2, -1, 1)),
(RangeIndex(-100, -200, 3), 2, RangeIndex(0)),
]
for idx, div, expected in cases_exact:
tm.assert_index_equal(idx // div, expected, exact=True)
Expand Down
17 changes: 9 additions & 8 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
Index,
Interval,
Period,
PeriodIndex,
Series,
Timedelta,
TimedeltaIndex,
Expand Down Expand Up @@ -884,30 +885,30 @@ def test_infer_dtype_timedelta_with_na(self, na_value, delta):

def test_infer_dtype_period(self):
# GH 13664
arr = np.array([pd.Period("2011-01", freq="D"), pd.Period("2011-02", freq="D")])
arr = np.array([Period("2011-01", freq="D"), Period("2011-02", freq="D")])
assert lib.infer_dtype(arr, skipna=True) == "period"

arr = np.array([pd.Period("2011-01", freq="D"), pd.Period("2011-02", freq="M")])
arr = np.array([Period("2011-01", freq="D"), Period("2011-02", freq="M")])
assert lib.infer_dtype(arr, skipna=True) == "period"

def test_infer_dtype_period_mixed(self):
arr = np.array(
[pd.Period("2011-01", freq="M"), np.datetime64("nat")], dtype=object
[Period("2011-01", freq="M"), np.datetime64("nat")], dtype=object
)
assert lib.infer_dtype(arr, skipna=False) == "mixed"

arr = np.array(
[np.datetime64("nat"), pd.Period("2011-01", freq="M")], dtype=object
[np.datetime64("nat"), Period("2011-01", freq="M")], dtype=object
)
assert lib.infer_dtype(arr, skipna=False) == "mixed"

@pytest.mark.parametrize("na_value", [pd.NaT, np.nan])
def test_infer_dtype_period_with_na(self, na_value):
# starts with nan
arr = np.array([na_value, pd.Period("2011-01", freq="D")])
arr = np.array([na_value, Period("2011-01", freq="D")])
assert lib.infer_dtype(arr, skipna=True) == "period"

arr = np.array([na_value, pd.Period("2011-01", freq="D"), na_value])
arr = np.array([na_value, Period("2011-01", freq="D"), na_value])
assert lib.infer_dtype(arr, skipna=True) == "period"

@pytest.mark.parametrize(
Expand Down Expand Up @@ -1192,8 +1193,8 @@ def test_to_object_array_width(self):
tm.assert_numpy_array_equal(out, expected)

def test_is_period(self):
assert lib.is_period(pd.Period("2011-01", freq="M"))
assert not lib.is_period(pd.PeriodIndex(["2011-01"], freq="M"))
assert lib.is_period(Period("2011-01", freq="M"))
assert not lib.is_period(PeriodIndex(["2011-01"], freq="M"))
assert not lib.is_period(Timestamp("2011-01"))
assert not lib.is_period(1)
assert not lib.is_period(np.nan)
Expand Down
21 changes: 15 additions & 6 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@
from pandas.errors import PerformanceWarning

import pandas as pd
from pandas import DataFrame, Index, MultiIndex, Series, Timestamp, date_range, read_csv
from pandas import (
DataFrame,
Grouper,
Index,
MultiIndex,
Series,
Timestamp,
date_range,
read_csv,
)
import pandas._testing as tm
from pandas.core.base import SpecificationError
import pandas.core.common as com


def test_repr():
# GH18203
result = repr(pd.Grouper(key="A", level="B"))
result = repr(Grouper(key="A", level="B"))
expected = "Grouper(key='A', level='B', axis=0, sort=False)"
assert result == expected

Expand Down Expand Up @@ -1218,7 +1227,7 @@ def test_groupby_keys_same_size_as_index():
start=Timestamp("2015-09-29T11:34:44-0700"), periods=2, freq=freq
)
df = DataFrame([["A", 10], ["B", 15]], columns=["metric", "values"], index=index)
result = df.groupby([pd.Grouper(level=0, freq=freq), "metric"]).mean()
result = df.groupby([Grouper(level=0, freq=freq), "metric"]).mean()
expected = df.set_index([df.index, "metric"])

tm.assert_frame_equal(result, expected)
Expand Down Expand Up @@ -1815,7 +1824,7 @@ def test_groupby_agg_ohlc_non_first():
index=pd.date_range("2018-01-01", periods=2, freq="D"),
)

result = df.groupby(pd.Grouper(freq="D")).agg(["sum", "ohlc"])
result = df.groupby(Grouper(freq="D")).agg(["sum", "ohlc"])

tm.assert_frame_equal(result, expected)

Expand Down Expand Up @@ -1866,11 +1875,11 @@ def test_groupby_groups_in_BaseGrouper():
# Test if DataFrame grouped with a pandas.Grouper has correct groups
mi = MultiIndex.from_product([["A", "B"], ["C", "D"]], names=["alpha", "beta"])
df = DataFrame({"foo": [1, 2, 1, 2], "bar": [1, 2, 3, 4]}, index=mi)
result = df.groupby([pd.Grouper(level="alpha"), "beta"])
result = df.groupby([Grouper(level="alpha"), "beta"])
expected = df.groupby(["alpha", "beta"])
assert result.groups == expected.groups

result = df.groupby(["beta", pd.Grouper(level="alpha")])
result = df.groupby(["beta", Grouper(level="alpha")])
expected = df.groupby(["beta", "alpha"])
assert result.groups == expected.groups

Expand Down
Loading