Skip to content

Commit 5122241

Browse files
jbrockmendeljreback
authored andcommitted
CLN: Assorted cleanups (#28848)
1 parent acde02b commit 5122241

24 files changed

+576
-364
lines changed

ci/code_checks.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
122122
MSG='Check for non-standard imports' ; echo $MSG
123123
invgrep -R --include="*.py*" -E "from pandas.core.common import " pandas
124124
invgrep -R --include="*.py*" -E "from collections.abc import " pandas
125-
# invgrep -R --include="*.py*" -E "from numpy import nan " pandas # GH#24822 not yet implemented since the offending imports have not all been removed
125+
invgrep -R --include="*.py*" -E "from numpy import nan " pandas
126126
RET=$(($RET + $?)) ; echo $MSG "DONE"
127127

128128
MSG='Check for use of exec' ; echo $MSG

pandas/core/internals/concat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def get_empty_dtype_and_na(join_units):
287287
return np.float64, np.nan
288288

289289
if is_uniform_reindex(join_units):
290-
# XXX: integrate property
290+
# FIXME: integrate property
291291
empty_dtype = join_units[0].block.dtype
292292
upcasted_na = join_units[0].block.fill_value
293293
return empty_dtype, upcasted_na

pandas/core/internals/managers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2035,7 +2035,7 @@ def concatenate_block_managers(mgrs_indexers, axes, concat_axis, copy):
20352035
values = b.values
20362036
if copy:
20372037
values = values.copy()
2038-
elif not copy:
2038+
else:
20392039
values = values.view()
20402040
b = b.make_block_same_class(values, placement=placement)
20412041
elif is_uniform_join_units(join_units):

pandas/core/ops/__init__.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -766,9 +766,9 @@ def f(self, other, axis=default_axis, level=None):
766766
return f
767767

768768

769-
def _comp_method_FRAME(cls, func, special):
770-
str_rep = _get_opstr(func)
771-
op_name = _get_op_name(func, special)
769+
def _comp_method_FRAME(cls, op, special):
770+
str_rep = _get_opstr(op)
771+
op_name = _get_op_name(op, special)
772772

773773
@Appender("Wrapper for comparison method {name}".format(name=op_name))
774774
def f(self, other):
@@ -781,18 +781,18 @@ def f(self, other):
781781
raise ValueError(
782782
"Can only compare identically-labeled DataFrame objects"
783783
)
784-
new_data = dispatch_to_series(self, other, func, str_rep)
784+
new_data = dispatch_to_series(self, other, op, str_rep)
785785
return self._construct_result(new_data)
786786

787787
elif isinstance(other, ABCSeries):
788788
return _combine_series_frame(
789-
self, other, func, fill_value=None, axis=None, level=None
789+
self, other, op, fill_value=None, axis=None, level=None
790790
)
791791
else:
792792

793793
# straight boolean comparisons we want to allow all columns
794794
# (regardless of dtype to pass thru) See #4537 for discussion.
795-
new_data = dispatch_to_series(self, other, func)
795+
new_data = dispatch_to_series(self, other, op)
796796
return self._construct_result(new_data)
797797

798798
f.__name__ = op_name

pandas/core/ops/array_ops.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@ def masked_arith_op(x, y, op):
118118
return result
119119

120120

121-
def define_na_arithmetic_op(op, str_rep, eval_kwargs):
121+
def define_na_arithmetic_op(op, str_rep: str, eval_kwargs):
122122
def na_op(x, y):
123123
return na_arithmetic_op(x, y, op, str_rep, eval_kwargs)
124124

125125
return na_op
126126

127127

128-
def na_arithmetic_op(left, right, op, str_rep, eval_kwargs):
128+
def na_arithmetic_op(left, right, op, str_rep: str, eval_kwargs):
129129
"""
130130
Return the result of evaluating op on the passed in values.
131131
@@ -173,6 +173,7 @@ def arithmetic_op(
173173
Cannot be a DataFrame or Index. Series is *not* excluded.
174174
op : {operator.add, operator.sub, ...}
175175
Or one of the reversed variants from roperator.
176+
str_rep : str
176177
177178
Returns
178179
-------
@@ -279,8 +280,16 @@ def comparison_op(
279280
return res_values
280281

281282

282-
def na_logical_op(x, y, op):
283+
def na_logical_op(x: np.ndarray, y, op):
283284
try:
285+
# For exposition, write:
286+
# yarr = isinstance(y, np.ndarray)
287+
# yint = is_integer(y) or (yarr and y.dtype.kind == "i")
288+
# ybool = is_bool(y) or (yarr and y.dtype.kind == "b")
289+
# xint = x.dtype.kind == "i"
290+
# xbool = x.dtype.kind == "b"
291+
# Then Cases where this goes through without raising include:
292+
# (xint or xbool) and (yint or bool)
284293
result = op(x, y)
285294
except TypeError:
286295
if isinstance(y, np.ndarray):
@@ -304,9 +313,9 @@ def na_logical_op(x, y, op):
304313
NotImplementedError,
305314
):
306315
raise TypeError(
307-
"cannot compare a dtyped [{dtype}] array "
308-
"with a scalar of type [{typ}]".format(
309-
dtype=x.dtype, typ=type(y).__name__
316+
"Cannot perform '{op}' with a dtyped [{dtype}] array "
317+
"and scalar of type [{typ}]".format(
318+
op=op.__name__, dtype=x.dtype, typ=type(y).__name__
310319
)
311320
)
312321

pandas/tests/groupby/test_bin_groupby.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import numpy as np
2-
from numpy import nan
32
import pytest
43

54
from pandas._libs import groupby, lib, reduction as libreduction
@@ -96,17 +95,17 @@ def _check(dtype):
9695

9796
def _ohlc(group):
9897
if isna(group).all():
99-
return np.repeat(nan, 4)
98+
return np.repeat(np.nan, 4)
10099
return [group[0], group.max(), group.min(), group[-1]]
101100

102101
expected = np.array([_ohlc(obj[:6]), _ohlc(obj[6:12]), _ohlc(obj[12:])])
103102

104103
assert_almost_equal(out, expected)
105104
tm.assert_numpy_array_equal(counts, np.array([6, 6, 8], dtype=np.int64))
106105

107-
obj[:6] = nan
106+
obj[:6] = np.nan
108107
func(out, counts, obj[:, None], labels)
109-
expected[0] = nan
108+
expected[0] = np.nan
110109
assert_almost_equal(out, expected)
111110

112111
_check("float32")

pandas/tests/groupby/test_timegrouper.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from io import StringIO
55

66
import numpy as np
7-
from numpy import nan
87
import pytest
98
import pytz
109

@@ -699,13 +698,13 @@ def test_first_last_max_min_on_time_data(self):
699698
df_test = DataFrame(
700699
{
701700
"dt": [
702-
nan,
701+
np.nan,
703702
"2015-07-24 10:10",
704703
"2015-07-25 11:11",
705704
"2015-07-23 12:12",
706-
nan,
705+
np.nan,
707706
],
708-
"td": [nan, td(days=1), td(days=2), td(days=3), nan],
707+
"td": [np.nan, td(days=1), td(days=2), td(days=3), np.nan],
709708
}
710709
)
711710
df_test.dt = pd.to_datetime(df_test.dt)

pandas/tests/io/excel/test_writers.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import os
55

66
import numpy as np
7-
from numpy import nan
87
import pytest
98

109
from pandas.compat import PY36
@@ -323,7 +322,7 @@ def test_excel_writer_context_manager(self, frame, engine, ext):
323322

324323
def test_roundtrip(self, engine, ext, frame):
325324
frame = frame.copy()
326-
frame["A"][:5] = nan
325+
frame["A"][:5] = np.nan
327326

328327
frame.to_excel(self.path, "test1")
329328
frame.to_excel(self.path, "test1", columns=["A", "B"])
@@ -388,7 +387,7 @@ def test_ts_frame(self, tsframe, engine, ext):
388387

389388
def test_basics_with_nan(self, engine, ext, frame):
390389
frame = frame.copy()
391-
frame["A"][:5] = nan
390+
frame["A"][:5] = np.nan
392391
frame.to_excel(self.path, "test1")
393392
frame.to_excel(self.path, "test1", columns=["A", "B"])
394393
frame.to_excel(self.path, "test1", header=False)
@@ -450,7 +449,7 @@ def test_inf_roundtrip(self, engine, ext):
450449

451450
def test_sheets(self, engine, ext, frame, tsframe):
452451
frame = frame.copy()
453-
frame["A"][:5] = nan
452+
frame["A"][:5] = np.nan
454453

455454
frame.to_excel(self.path, "test1")
456455
frame.to_excel(self.path, "test1", columns=["A", "B"])
@@ -473,7 +472,7 @@ def test_sheets(self, engine, ext, frame, tsframe):
473472

474473
def test_colaliases(self, engine, ext, frame):
475474
frame = frame.copy()
476-
frame["A"][:5] = nan
475+
frame["A"][:5] = np.nan
477476

478477
frame.to_excel(self.path, "test1")
479478
frame.to_excel(self.path, "test1", columns=["A", "B"])
@@ -491,7 +490,7 @@ def test_colaliases(self, engine, ext, frame):
491490

492491
def test_roundtrip_indexlabels(self, merge_cells, engine, ext, frame):
493492
frame = frame.copy()
494-
frame["A"][:5] = nan
493+
frame["A"][:5] = np.nan
495494

496495
frame.to_excel(self.path, "test1")
497496
frame.to_excel(self.path, "test1", columns=["A", "B"])

pandas/tests/io/parser/test_textreader.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import os
77

88
import numpy as np
9-
from numpy import nan
109
import pytest
1110

1211
import pandas._libs.parsers as parser
@@ -309,10 +308,15 @@ def test_empty_field_eof(self):
309308
assert_array_dicts_equal(result, expected)
310309

311310
# GH5664
312-
a = DataFrame([["b"], [nan]], columns=["a"], index=["a", "c"])
311+
a = DataFrame([["b"], [np.nan]], columns=["a"], index=["a", "c"])
313312
b = DataFrame([[1, 1, 1, 0], [1, 1, 1, 0]], columns=list("abcd"), index=[1, 1])
314313
c = DataFrame(
315-
[[1, 2, 3, 4], [6, nan, nan, nan], [8, 9, 10, 11], [13, 14, nan, nan]],
314+
[
315+
[1, 2, 3, 4],
316+
[6, np.nan, np.nan, np.nan],
317+
[8, 9, 10, 11],
318+
[13, 14, np.nan, np.nan],
319+
],
316320
columns=list("abcd"),
317321
index=[0, 5, 7, 12],
318322
)

pandas/tests/reshape/merge/test_merge.py

+8-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from collections import OrderedDict
2-
from datetime import date, datetime
2+
from datetime import date, datetime, timedelta
33
import random
44
import re
55

66
import numpy as np
7-
from numpy import nan
87
import pytest
98

109
from pandas.core.dtypes.common import is_categorical_dtype, is_object_dtype
@@ -565,9 +564,7 @@ def test_merge_all_na_column(self, series_of_dtype, series_of_dtype_all_na):
565564
assert_frame_equal(actual, expected)
566565

567566
def test_merge_nosort(self):
568-
# #2098, anything to do?
569-
570-
from datetime import datetime
567+
# GH#2098, TODO: anything to do?
571568

572569
d = {
573570
"var1": np.random.randint(0, 10, size=10),
@@ -621,9 +618,9 @@ def test_merge_nan_right(self):
621618
expected = DataFrame(
622619
{
623620
"i1": {0: 0, 1: 1},
624-
"i1_": {0: 0.0, 1: nan},
621+
"i1_": {0: 0.0, 1: np.nan},
625622
"i2": {0: 0.5, 1: 1.5},
626-
"i3": {0: 0.69999999999999996, 1: nan},
623+
"i3": {0: 0.69999999999999996, 1: np.nan},
627624
}
628625
)[["i1", "i2", "i1_", "i3"]]
629626
assert_frame_equal(result, expected)
@@ -640,21 +637,17 @@ def _constructor(self):
640637
assert isinstance(result, NotADataFrame)
641638

642639
def test_join_append_timedeltas(self):
643-
644-
import datetime as dt
645-
from pandas import NaT
646-
647640
# timedelta64 issues with join/merge
648641
# GH 5695
649642

650-
d = {"d": dt.datetime(2013, 11, 5, 5, 56), "t": dt.timedelta(0, 22500)}
643+
d = {"d": datetime(2013, 11, 5, 5, 56), "t": timedelta(0, 22500)}
651644
df = DataFrame(columns=list("dt"))
652645
df = df.append(d, ignore_index=True)
653646
result = df.append(d, ignore_index=True)
654647
expected = DataFrame(
655648
{
656-
"d": [dt.datetime(2013, 11, 5, 5, 56), dt.datetime(2013, 11, 5, 5, 56)],
657-
"t": [dt.timedelta(0, 22500), dt.timedelta(0, 22500)],
649+
"d": [datetime(2013, 11, 5, 5, 56), datetime(2013, 11, 5, 5, 56)],
650+
"t": [timedelta(0, 22500), timedelta(0, 22500)],
658651
}
659652
)
660653
assert_frame_equal(result, expected)
@@ -667,7 +660,7 @@ def test_join_append_timedeltas(self):
667660
expected = DataFrame(
668661
{
669662
"0": Series([td, td], index=list("AB")),
670-
"0r": Series([td, NaT], index=list("AB")),
663+
"0r": Series([td, pd.NaT], index=list("AB")),
671664
}
672665
)
673666
assert_frame_equal(result, expected)

pandas/tests/reshape/merge/test_merge_ordered.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from numpy import nan
1+
import numpy as np
22
import pytest
33

44
import pandas as pd
@@ -17,8 +17,8 @@ def test_basic(self):
1717
expected = DataFrame(
1818
{
1919
"key": ["a", "b", "c", "d", "e", "f"],
20-
"lvalue": [1, nan, 2, nan, 3, nan],
21-
"rvalue": [nan, 1, 2, 3, nan, 4],
20+
"lvalue": [1, np.nan, 2, np.nan, 3, np.nan],
21+
"rvalue": [np.nan, 1, 2, 3, np.nan, 4],
2222
}
2323
)
2424

@@ -30,7 +30,7 @@ def test_ffill(self):
3030
{
3131
"key": ["a", "b", "c", "d", "e", "f"],
3232
"lvalue": [1.0, 1, 2, 2, 3, 3.0],
33-
"rvalue": [nan, 1, 2, 3, 3, 4],
33+
"rvalue": [np.nan, 1, 2, 3, 3, 4],
3434
}
3535
)
3636
assert_frame_equal(result, expected)
@@ -47,7 +47,7 @@ def test_multigroup(self):
4747
{
4848
"key": ["a", "b", "c", "d", "e", "f"] * 2,
4949
"lvalue": [1.0, 1, 2, 2, 3, 3.0] * 2,
50-
"rvalue": [nan, 1, 2, 3, 3, 4] * 2,
50+
"rvalue": [np.nan, 1, 2, 3, 3, 4] * 2,
5151
}
5252
)
5353
expected["group"] = ["a"] * 6 + ["b"] * 6
@@ -110,7 +110,7 @@ def test_doc_example(self):
110110
"group": list("aaaaabbbbb"),
111111
"key": ["a", "b", "c", "d", "e"] * 2,
112112
"lvalue": [1, 1, 2, 2, 3] * 2,
113-
"rvalue": [nan, 1, 2, 3, 3] * 2,
113+
"rvalue": [np.nan, 1, 2, 3, 3] * 2,
114114
}
115115
)
116116

pandas/tests/reshape/merge/test_multi.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from collections import OrderedDict
22

33
import numpy as np
4-
from numpy import nan
54
from numpy.random import randn
65
import pytest
76

@@ -311,11 +310,11 @@ def test_left_join_index_multi_match_multiindex(self):
311310
[
312311
["X", "Y", "C", "a", 6],
313312
["X", "Y", "C", "a", 9],
314-
["W", "Y", "C", "e", nan],
313+
["W", "Y", "C", "e", np.nan],
315314
["V", "Q", "A", "h", -3],
316315
["V", "R", "D", "i", 2],
317316
["V", "R", "D", "i", -1],
318-
["X", "Y", "D", "b", nan],
317+
["X", "Y", "D", "b", np.nan],
319318
["X", "Y", "A", "c", 1],
320319
["X", "Y", "A", "c", 4],
321320
["W", "Q", "B", "f", 3],
@@ -365,10 +364,10 @@ def test_left_join_index_multi_match(self):
365364
["c", 0, "x"],
366365
["c", 0, "r"],
367366
["c", 0, "s"],
368-
["b", 1, nan],
367+
["b", 1, np.nan],
369368
["a", 2, "v"],
370369
["a", 2, "z"],
371-
["b", 3, nan],
370+
["b", 3, np.nan],
372371
],
373372
columns=["tag", "val", "char"],
374373
index=[2, 2, 2, 2, 0, 1, 1, 3],

0 commit comments

Comments
 (0)