Skip to content
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

STYLE: Resolved Inconsistent namespace #40286

Merged
merged 22 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5d55412
STYLE: Inconsistent namespace - tools (pandas-dev#39992)
deepang17 Mar 7, 2021
c9c3eff
Pre-Commit Fixes
deepang17 Mar 7, 2021
6b4e259
Merge branch 'master' of https://github.com/pandas-dev/pandas
deepang17 Mar 7, 2021
5723060
STYLE: Resolved Inconsistent namespace except test_algos.py - (#39992)
deepang17 Mar 7, 2021
6bc371c
STYLE: Resolved Inconsistent namespace by excluding test_algos.py
deepang17 Mar 7, 2021
06387fb
test_algos.py fixes
deepang17 Mar 7, 2021
58d1017
Merge branch 'master' of https://github.com/pandas-dev/pandas
deepang17 Mar 8, 2021
9c631eb
Updated test_algos.py
deepang17 Mar 8, 2021
6af3b2d
STYLE: inconsistent-namespace-usage fixes
deepang17 Mar 8, 2021
91cba42
updated test_interval.py
deepang17 Mar 8, 2021
61108f7
FIX: Improved varible names
deepang17 Mar 8, 2021
23de5ef
test_interval.py fixes
deepang17 Mar 8, 2021
57f78a9
Merge branch 'master' of https://github.com/pandas-dev/pandas
deepang17 Mar 9, 2021
373b4e4
FIX: Resolved confusing variable names
deepang17 Mar 9, 2021
f29cce7
Merge branch 'master' of https://github.com/pandas-dev/pandas
deepang17 Mar 9, 2021
92c177b
test_string.py fixes
deepang17 Mar 9, 2021
1e1d76e
Merge branch 'master' of https://github.com/pandas-dev/pandas
deepang17 Mar 10, 2021
17a82ca
Merge branch 'master' of https://github.com/pandas-dev/pandas
deepang17 Mar 10, 2021
a3ceb65
getitem.py fixes
deepang17 Mar 10, 2021
40a35ee
rename more variables to arr
MarcoGorelli Mar 11, 2021
51700e7
Merge remote-tracking branch 'upstream/master' into pr/deepang17/40286
MarcoGorelli Mar 11, 2021
ac1ad61
Merge remote-tracking branch 'upstream/master' into pr/deepang17/40286
MarcoGorelli Mar 11, 2021
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ repos:
entry: python scripts/check_for_inconsistent_pandas_namespace.py
language: python
types: [python]
files: ^pandas/tests/frame/
files: ^pandas/tests/
- id: incorrect-code-directives
name: Check for incorrect code block or IPython directives
language: pygrep
Expand Down
92 changes: 47 additions & 45 deletions pandas/tests/arithmetic/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def left_right_dtypes(request):


@pytest.fixture
def array(left_right_dtypes):
def interval_array(left_right_dtypes):
"""
Fixture to generate an IntervalArray of various dtypes containing NA if possible
"""
Expand Down Expand Up @@ -98,42 +98,42 @@ def interval_constructor(self, request):
"""
return request.param

def elementwise_comparison(self, op, array, other):
def elementwise_comparison(self, op, interval_array, other):
"""
Helper that performs elementwise comparisons between `array` and `other`
"""
other = other if is_list_like(other) else [other] * len(array)
expected = np.array([op(x, y) for x, y in zip(array, other)])
other = other if is_list_like(other) else [other] * len(interval_array)
expected = np.array([op(x, y) for x, y in zip(interval_array, other)])
if isinstance(other, Series):
return Series(expected, index=other.index)
return expected

def test_compare_scalar_interval(self, op, array):
def test_compare_scalar_interval(self, op, interval_array):
# matches first interval
other = array[0]
result = op(array, other)
expected = self.elementwise_comparison(op, array, other)
other = interval_array[0]
result = op(interval_array, other)
expected = self.elementwise_comparison(op, interval_array, other)
tm.assert_numpy_array_equal(result, expected)

# matches on a single endpoint but not both
other = Interval(array.left[0], array.right[1])
result = op(array, other)
expected = self.elementwise_comparison(op, array, other)
other = Interval(interval_array.left[0], interval_array.right[1])
result = op(interval_array, other)
expected = self.elementwise_comparison(op, interval_array, other)
tm.assert_numpy_array_equal(result, expected)

def test_compare_scalar_interval_mixed_closed(self, op, closed, other_closed):
array = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed)
interval_array = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed)
other = Interval(0, 1, closed=other_closed)

result = op(array, other)
expected = self.elementwise_comparison(op, array, other)
result = op(interval_array, other)
expected = self.elementwise_comparison(op, interval_array, other)
tm.assert_numpy_array_equal(result, expected)

def test_compare_scalar_na(self, op, array, nulls_fixture, request):
result = op(array, nulls_fixture)
expected = self.elementwise_comparison(op, array, nulls_fixture)
def test_compare_scalar_na(self, op, interval_array, nulls_fixture, request):
result = op(interval_array, nulls_fixture)
expected = self.elementwise_comparison(op, interval_array, nulls_fixture)

if nulls_fixture is pd.NA and array.dtype.subtype != "int64":
if nulls_fixture is pd.NA and interval_array.dtype.subtype != "int64":
mark = pytest.mark.xfail(
reason="broken for non-integer IntervalArray; see GH 31882"
)
Expand All @@ -154,38 +154,40 @@ def test_compare_scalar_na(self, op, array, nulls_fixture, request):
Period("2017-01-01", "D"),
],
)
def test_compare_scalar_other(self, op, array, other):
result = op(array, other)
expected = self.elementwise_comparison(op, array, other)
def test_compare_scalar_other(self, op, interval_array, other):
result = op(interval_array, other)
expected = self.elementwise_comparison(op, interval_array, other)
tm.assert_numpy_array_equal(result, expected)

def test_compare_list_like_interval(self, op, array, interval_constructor):
def test_compare_list_like_interval(self, op, interval_array, interval_constructor):
# same endpoints
other = interval_constructor(array.left, array.right)
result = op(array, other)
expected = self.elementwise_comparison(op, array, other)
other = interval_constructor(interval_array.left, interval_array.right)
result = op(interval_array, other)
expected = self.elementwise_comparison(op, interval_array, other)
tm.assert_equal(result, expected)

# different endpoints
other = interval_constructor(array.left[::-1], array.right[::-1])
result = op(array, other)
expected = self.elementwise_comparison(op, array, other)
other = interval_constructor(
interval_array.left[::-1], interval_array.right[::-1]
)
result = op(interval_array, other)
expected = self.elementwise_comparison(op, interval_array, other)
tm.assert_equal(result, expected)

# all nan endpoints
other = interval_constructor([np.nan] * 4, [np.nan] * 4)
result = op(array, other)
expected = self.elementwise_comparison(op, array, other)
result = op(interval_array, other)
expected = self.elementwise_comparison(op, interval_array, other)
tm.assert_equal(result, expected)

def test_compare_list_like_interval_mixed_closed(
self, op, interval_constructor, closed, other_closed
):
array = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed)
interval_array = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed)
other = interval_constructor(range(2), range(1, 3), closed=other_closed)

result = op(array, other)
expected = self.elementwise_comparison(op, array, other)
result = op(interval_array, other)
expected = self.elementwise_comparison(op, interval_array, other)
tm.assert_equal(result, expected)

@pytest.mark.parametrize(
Expand All @@ -206,17 +208,17 @@ def test_compare_list_like_interval_mixed_closed(
),
],
)
def test_compare_list_like_object(self, op, array, other):
result = op(array, other)
expected = self.elementwise_comparison(op, array, other)
def test_compare_list_like_object(self, op, interval_array, other):
result = op(interval_array, other)
expected = self.elementwise_comparison(op, interval_array, other)
tm.assert_numpy_array_equal(result, expected)

def test_compare_list_like_nan(self, op, array, nulls_fixture, request):
def test_compare_list_like_nan(self, op, interval_array, nulls_fixture, request):
other = [nulls_fixture] * 4
result = op(array, other)
expected = self.elementwise_comparison(op, array, other)
result = op(interval_array, other)
expected = self.elementwise_comparison(op, interval_array, other)

if nulls_fixture is pd.NA and array.dtype.subtype != "i8":
if nulls_fixture is pd.NA and interval_array.dtype.subtype != "i8":
reason = "broken for non-integer IntervalArray; see GH 31882"
mark = pytest.mark.xfail(reason=reason)
request.node.add_marker(mark)
Expand All @@ -239,18 +241,18 @@ def test_compare_list_like_nan(self, op, array, nulls_fixture, request):
],
ids=lambda x: str(x.dtype),
)
def test_compare_list_like_other(self, op, array, other):
result = op(array, other)
expected = self.elementwise_comparison(op, array, other)
def test_compare_list_like_other(self, op, interval_array, other):
result = op(interval_array, other)
expected = self.elementwise_comparison(op, interval_array, other)
tm.assert_numpy_array_equal(result, expected)

@pytest.mark.parametrize("length", [1, 3, 5])
@pytest.mark.parametrize("other_constructor", [IntervalArray, list])
def test_compare_length_mismatch_errors(self, op, other_constructor, length):
array = IntervalArray.from_arrays(range(4), range(1, 5))
interval_array = IntervalArray.from_arrays(range(4), range(1, 5))
other = other_constructor([Interval(0, 1)] * length)
with pytest.raises(ValueError, match="Lengths must match to compare"):
op(array, other)
op(interval_array, other)

@pytest.mark.parametrize(
"constructor, expected_type, assert_func",
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/arrays/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ def test_astype_all(self, any_real_dtype):
tm.assert_numpy_array_equal(np.asarray(res.to_dense()), vals.astype(typ))

@pytest.mark.parametrize(
"array, dtype, expected",
"sparse_array, dtype, expected",
[
(
SparseArray([0, 1]),
Expand Down Expand Up @@ -557,8 +557,8 @@ def test_astype_all(self, any_real_dtype):
),
],
)
def test_astype_more(self, array, dtype, expected):
result = array.astype(dtype)
def test_astype_more(self, sparse_array, dtype, expected):
result = sparse_array.astype(dtype)
tm.assert_sp_array_equal(result, expected)

def test_astype_nan_raises(self):
Expand Down
16 changes: 8 additions & 8 deletions pandas/tests/arrays/string_/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,31 +223,31 @@ def test_mul(dtype, request):

@pytest.mark.xfail(reason="GH-28527")
def test_add_strings(dtype):
array = pd.array(["a", "b", "c", "d"], dtype=dtype)
pd_array = pd.array(["a", "b", "c", "d"], dtype=dtype)
deepang17 marked this conversation as resolved.
Show resolved Hide resolved
df = pd.DataFrame([["t", "u", "v", "w"]])
assert array.__add__(df) is NotImplemented
assert pd_array.__add__(df) is NotImplemented

result = array + df
result = pd_array + df
expected = pd.DataFrame([["at", "bu", "cv", "dw"]]).astype(dtype)
tm.assert_frame_equal(result, expected)

result = df + array
result = df + pd_array
expected = pd.DataFrame([["ta", "ub", "vc", "wd"]]).astype(dtype)
tm.assert_frame_equal(result, expected)


@pytest.mark.xfail(reason="GH-28527")
def test_add_frame(dtype):
array = pd.array(["a", "b", np.nan, np.nan], dtype=dtype)
pd_array = pd.array(["a", "b", np.nan, np.nan], dtype=dtype)
df = pd.DataFrame([["x", np.nan, "y", np.nan]])

assert array.__add__(df) is NotImplemented
assert pd_array.__add__(df) is NotImplemented

result = array + df
result = pd_array + df
expected = pd.DataFrame([["ax", np.nan, np.nan, np.nan]]).astype(dtype)
tm.assert_frame_equal(result, expected)

result = df + array
result = df + pd_array
expected = pd.DataFrame([["xa", np.nan, np.nan, np.nan]]).astype(dtype)
tm.assert_frame_equal(result, expected)

Expand Down
34 changes: 17 additions & 17 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ def test_strftime_nat(self):


@pytest.mark.parametrize(
"array,casting_nats",
"ts_array,casting_nats",
[
(
TimedeltaIndex(["1 Day", "3 Hours", "NaT"])._data,
Expand All @@ -1174,17 +1174,17 @@ def test_strftime_nat(self):
],
ids=lambda x: type(x).__name__,
)
def test_casting_nat_setitem_array(array, casting_nats):
expected = type(array)._from_sequence([NaT, array[1], array[2]])
def test_casting_nat_setitem_array(ts_array, casting_nats):
expected = type(ts_array)._from_sequence([NaT, ts_array[1], ts_array[2]])

for nat in casting_nats:
arr = array.copy()
arr = ts_array.copy()
arr[0] = nat
tm.assert_equal(arr, expected)


@pytest.mark.parametrize(
"array,non_casting_nats",
"ts_array,non_casting_nats",
[
(
TimedeltaIndex(["1 Day", "3 Hours", "NaT"])._data,
Expand All @@ -1201,50 +1201,50 @@ def test_casting_nat_setitem_array(array, casting_nats):
],
ids=lambda x: type(x).__name__,
)
def test_invalid_nat_setitem_array(array, non_casting_nats):
def test_invalid_nat_setitem_array(ts_array, non_casting_nats):
msg = (
"value should be a '(Timestamp|Timedelta|Period)', 'NaT', or array of those. "
"Got '(timedelta64|datetime64|int)' instead."
)

for nat in non_casting_nats:
with pytest.raises(TypeError, match=msg):
array[0] = nat
ts_array[0] = nat


@pytest.mark.parametrize(
"array",
"ts_array",
[
pd.date_range("2000", periods=4).array,
pd.timedelta_range("2000", periods=4).array,
],
)
def test_to_numpy_extra(array):
def test_to_numpy_extra(ts_array):
if np_version_under1p18:
# np.isnan(NaT) raises, so use pandas'
isnan = pd.isna
else:
isnan = np.isnan

array[0] = NaT
original = array.copy()
ts_array[0] = NaT
original = ts_array.copy()

result = array.to_numpy()
result = ts_array.to_numpy()
assert isnan(result[0])

result = array.to_numpy(dtype="int64")
result = ts_array.to_numpy(dtype="int64")
assert result[0] == -9223372036854775808

result = array.to_numpy(dtype="int64", na_value=0)
result = ts_array.to_numpy(dtype="int64", na_value=0)
assert result[0] == 0

result = array.to_numpy(na_value=array[1].to_numpy())
result = ts_array.to_numpy(na_value=ts_array[1].to_numpy())
assert result[0] == result[1]

result = array.to_numpy(na_value=array[1].to_numpy(copy=False))
result = ts_array.to_numpy(na_value=ts_array[1].to_numpy(copy=False))
assert result[0] == result[1]

tm.assert_equal(array, original)
tm.assert_equal(ts_array, original)


@pytest.mark.parametrize("as_index", [True, False])
Expand Down
Loading