Skip to content

Commit f287794

Browse files
ShaharNavehsimonjayhawkins
authored andcommitted
STY: Underscores for long numbers (#30166)
1 parent c3ee0c3 commit f287794

File tree

9 files changed

+51
-39
lines changed

9 files changed

+51
-39
lines changed

pandas/_libs/testing.pyx

+8-8
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ cpdef assert_almost_equal(a, b,
6666
check_less_precise=False,
6767
bint check_dtype=True,
6868
obj=None, lobj=None, robj=None):
69-
"""Check that left and right objects are almost equal.
69+
"""
70+
Check that left and right objects are almost equal.
7071
7172
Parameters
7273
----------
@@ -89,7 +90,6 @@ cpdef assert_almost_equal(a, b,
8990
Specify right object name being compared, internally used to show
9091
appropriate assertion message
9192
"""
92-
9393
cdef:
9494
int decimal
9595
double diff = 0.0
@@ -127,9 +127,9 @@ cpdef assert_almost_equal(a, b,
127127
# classes can't be the same, to raise error
128128
assert_class_equal(a, b, obj=obj)
129129

130-
assert has_length(a) and has_length(b), (
131-
f"Can't compare objects without length, one or both is invalid: "
132-
f"({a}, {b})")
130+
assert has_length(a) and has_length(b), ("Can't compare objects without "
131+
"length, one or both is invalid: "
132+
f"({a}, {b})")
133133

134134
if a_is_ndarray and b_is_ndarray:
135135
na, nb = a.size, b.size
@@ -157,7 +157,7 @@ cpdef assert_almost_equal(a, b,
157157
else:
158158
r = None
159159

160-
raise_assert_detail(obj, f'{obj} length are different', na, nb, r)
160+
raise_assert_detail(obj, f"{obj} length are different", na, nb, r)
161161

162162
for i in xrange(len(a)):
163163
try:
@@ -169,8 +169,8 @@ cpdef assert_almost_equal(a, b,
169169

170170
if is_unequal:
171171
from pandas.util.testing import raise_assert_detail
172-
msg = (f'{obj} values are different '
173-
f'({np.round(diff * 100.0 / na, 5)} %)')
172+
msg = (f"{obj} values are different "
173+
f"({np.round(diff * 100.0 / na, 5)} %)")
174174
raise_assert_detail(obj, msg, lobj, robj)
175175

176176
return True

pandas/core/algorithms.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -391,16 +391,15 @@ def isin(comps, values) -> np.ndarray:
391391
ndarray[bool]
392392
Same length as `comps`.
393393
"""
394-
395394
if not is_list_like(comps):
396395
raise TypeError(
397-
"only list-like objects are allowed to be passed"
398-
f" to isin(), you passed a [{type(comps).__name__}]"
396+
"only list-like objects are allowed to be passed "
397+
f"to isin(), you passed a [{type(comps).__name__}]"
399398
)
400399
if not is_list_like(values):
401400
raise TypeError(
402-
"only list-like objects are allowed to be passed"
403-
f" to isin(), you passed a [{type(values).__name__}]"
401+
"only list-like objects are allowed to be passed "
402+
f"to isin(), you passed a [{type(values).__name__}]"
404403
)
405404

406405
if not isinstance(values, (ABCIndex, ABCSeries, np.ndarray)):
@@ -421,7 +420,7 @@ def isin(comps, values) -> np.ndarray:
421420

422421
# GH16012
423422
# Ensure np.in1d doesn't get object types or it *may* throw an exception
424-
if len(comps) > 1000000 and not is_object_dtype(comps):
423+
if len(comps) > 1_000_000 and not is_object_dtype(comps):
425424
f = np.in1d
426425
elif is_integer_dtype(comps):
427426
try:
@@ -833,8 +832,8 @@ def mode(values, dropna: bool = True) -> ABCSeries:
833832
result = f(values, dropna=dropna)
834833
try:
835834
result = np.sort(result)
836-
except TypeError as e:
837-
warn(f"Unable to sort modes: {e}")
835+
except TypeError as err:
836+
warn(f"Unable to sort modes: {err}")
838837

839838
result = _reconstruct_data(result, original.dtype, original)
840839
return Series(result)
@@ -1019,7 +1018,8 @@ def quantile(x, q, interpolation_method="fraction"):
10191018
values = np.sort(x)
10201019

10211020
def _interpolate(a, b, fraction):
1022-
"""Returns the point at the given fraction between a and b, where
1021+
"""
1022+
Returns the point at the given fraction between a and b, where
10231023
'fraction' must be between 0 and 1.
10241024
"""
10251025
return a + (b - a) * fraction
@@ -1192,7 +1192,8 @@ def compute(self, method):
11921192
)
11931193

11941194
def get_indexer(current_indexer, other_indexer):
1195-
"""Helper function to concat `current_indexer` and `other_indexer`
1195+
"""
1196+
Helper function to concat `current_indexer` and `other_indexer`
11961197
depending on `method`
11971198
"""
11981199
if method == "nsmallest":
@@ -1660,7 +1661,7 @@ def take_nd(
16601661

16611662
def take_2d_multi(arr, indexer, fill_value=np.nan):
16621663
"""
1663-
Specialized Cython take which sets NaN values in one pass
1664+
Specialized Cython take which sets NaN values in one pass.
16641665
"""
16651666
# This is only called from one place in DataFrame._reindex_multi,
16661667
# so we know indexer is well-behaved.
@@ -1988,8 +1989,8 @@ def sort_mixed(values):
19881989

19891990
if not is_list_like(codes):
19901991
raise TypeError(
1991-
"Only list-like objects or None are allowed to be"
1992-
"passed to safe_sort as codes"
1992+
"Only list-like objects or None are allowed to "
1993+
"be passed to safe_sort as codes"
19931994
)
19941995
codes = ensure_platform_int(np.asarray(codes))
19951996

pandas/core/arrays/categorical.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -494,14 +494,13 @@ def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
494494
if is_extension_array_dtype(dtype):
495495
return array(self, dtype=dtype, copy=copy) # type: ignore # GH 28770
496496
if is_integer_dtype(dtype) and self.isna().any():
497-
msg = "Cannot convert float NaN to integer"
498-
raise ValueError(msg)
497+
raise ValueError("Cannot convert float NaN to integer")
499498
return np.array(self, dtype=dtype, copy=copy)
500499

501500
@cache_readonly
502501
def size(self) -> int:
503502
"""
504-
return the len of myself
503+
Return the len of myself.
505504
"""
506505
return self._codes.size
507506

pandas/core/computation/align.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ def _zip_axes_from_type(typ, new_axes):
3535

3636

3737
def _any_pandas_objects(terms) -> bool:
38-
"""Check a sequence of terms for instances of PandasObject."""
38+
"""
39+
Check a sequence of terms for instances of PandasObject.
40+
"""
3941
return any(isinstance(term.value, PandasObject) for term in terms)
4042

4143

@@ -116,7 +118,9 @@ def _align_core(terms):
116118

117119

118120
def align_terms(terms):
119-
"""Align a set of terms"""
121+
"""
122+
Align a set of terms.
123+
"""
120124
try:
121125
# flatten the parse tree (a nested list, really)
122126
terms = list(com.flatten(terms))

pandas/core/computation/common.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99

1010

1111
def _ensure_decoded(s):
12-
""" if we have bytes, decode them to unicode """
12+
"""
13+
If we have bytes, decode them to unicode.
14+
"""
1315
if isinstance(s, (np.bytes_, bytes)):
1416
s = s.decode(get_option("display.encoding"))
1517
return s
1618

1719

1820
def result_type_many(*arrays_and_dtypes):
19-
""" wrapper around numpy.result_type which overcomes the NPY_MAXARGS (32)
20-
argument limit """
21+
"""
22+
Wrapper around numpy.result_type which overcomes the NPY_MAXARGS (32)
23+
argument limit.
24+
"""
2125
try:
2226
return np.result_type(*arrays_and_dtypes)
2327
except ValueError:
@@ -26,8 +30,10 @@ def result_type_many(*arrays_and_dtypes):
2630

2731

2832
def _remove_spaces_column_name(name):
29-
"""Check if name contains any spaces, if it contains any spaces
30-
the spaces will be removed and an underscore suffix is added."""
33+
"""
34+
Check if name contains any spaces, if it contains any spaces
35+
the spaces will be removed and an underscore suffix is added.
36+
"""
3137
if not isinstance(name, str) or " " not in name:
3238
return name
3339

pandas/core/computation/ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,8 @@ def __init__(self, lhs, rhs, **kwargs):
506506

507507
if not isnumeric(lhs.return_type) or not isnumeric(rhs.return_type):
508508
raise TypeError(
509-
f"unsupported operand type(s) for {self.op}:"
510-
f" '{lhs.return_type}' and '{rhs.return_type}'"
509+
f"unsupported operand type(s) for {self.op}: "
510+
f"'{lhs.return_type}' and '{rhs.return_type}'"
511511
)
512512

513513
# do not upcast float32s to float64 un-necessarily

pandas/tests/io/formats/test_format.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2376,7 +2376,8 @@ def test_east_asian_unicode_series(self):
23762376

23772377
# object dtype, longer than unicode repr
23782378
s = Series(
2379-
[1, 22, 3333, 44444], index=[1, "AB", pd.Timestamp("2011-01-01"), "あああ"]
2379+
[1, 22, 3333, 44444],
2380+
index=[1, "AB", pd.Timestamp("2011-01-01"), "あああ"],
23802381
)
23812382
expected = (
23822383
"1 1\n"

pandas/tests/io/parser/test_usecols.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def test_usecols_with_whitespace(all_parsers):
199199
# Column selection by index.
200200
([0, 1], DataFrame(data=[[1000, 2000], [4000, 5000]], columns=["2", "0"])),
201201
# Column selection by name.
202-
(["0", "1"], DataFrame(data=[[2000, 3000], [5000, 6000]], columns=["0", "1"])),
202+
(["0", "1"], DataFrame(data=[[2000, 3000], [5000, 6000]], columns=["0", "1"]),),
203203
],
204204
)
205205
def test_usecols_with_integer_like_header(all_parsers, usecols, expected):

pandas/tests/io/pytables/test_compat.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@
99

1010
@pytest.fixture
1111
def pytables_hdf5_file():
12-
"""Use PyTables to create a simple HDF5 file."""
13-
12+
"""
13+
Use PyTables to create a simple HDF5 file.
14+
"""
1415
table_schema = {
1516
"c0": tables.Time64Col(pos=0),
1617
"c1": tables.StringCol(5, pos=1),
1718
"c2": tables.Int64Col(pos=2),
1819
}
1920

20-
t0 = 1561105000.0
21+
t0 = 1_561_105_000.0
2122

2223
testsamples = [
2324
{"c0": t0, "c1": "aaaaa", "c2": 1},
2425
{"c0": t0 + 1, "c1": "bbbbb", "c2": 2},
2526
{"c0": t0 + 2, "c1": "ccccc", "c2": 10 ** 5},
26-
{"c0": t0 + 3, "c1": "ddddd", "c2": 4294967295},
27+
{"c0": t0 + 3, "c1": "ddddd", "c2": 4_294_967_295},
2728
]
2829

2930
objname = "pandas_test_timeseries"

0 commit comments

Comments
 (0)