Skip to content

Commit

Permalink
STY: Underscores for long numbers (pandas-dev#30166)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaharNaveh authored and proost committed Dec 19, 2019
1 parent 3685fd5 commit 4156ec1
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 39 deletions.
16 changes: 8 additions & 8 deletions pandas/_libs/testing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ cpdef assert_almost_equal(a, b,
check_less_precise=False,
bint check_dtype=True,
obj=None, lobj=None, robj=None):
"""Check that left and right objects are almost equal.
"""
Check that left and right objects are almost equal.
Parameters
----------
Expand All @@ -89,7 +90,6 @@ cpdef assert_almost_equal(a, b,
Specify right object name being compared, internally used to show
appropriate assertion message
"""

cdef:
int decimal
double diff = 0.0
Expand Down Expand Up @@ -127,9 +127,9 @@ cpdef assert_almost_equal(a, b,
# classes can't be the same, to raise error
assert_class_equal(a, b, obj=obj)

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

if a_is_ndarray and b_is_ndarray:
na, nb = a.size, b.size
Expand Down Expand Up @@ -157,7 +157,7 @@ cpdef assert_almost_equal(a, b,
else:
r = None

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

for i in xrange(len(a)):
try:
Expand All @@ -169,8 +169,8 @@ cpdef assert_almost_equal(a, b,

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

return True
Expand Down
27 changes: 14 additions & 13 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,16 +391,15 @@ def isin(comps, values) -> np.ndarray:
ndarray[bool]
Same length as `comps`.
"""

if not is_list_like(comps):
raise TypeError(
"only list-like objects are allowed to be passed"
f" to isin(), you passed a [{type(comps).__name__}]"
"only list-like objects are allowed to be passed "
f"to isin(), you passed a [{type(comps).__name__}]"
)
if not is_list_like(values):
raise TypeError(
"only list-like objects are allowed to be passed"
f" to isin(), you passed a [{type(values).__name__}]"
"only list-like objects are allowed to be passed "
f"to isin(), you passed a [{type(values).__name__}]"
)

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

# GH16012
# Ensure np.in1d doesn't get object types or it *may* throw an exception
if len(comps) > 1000000 and not is_object_dtype(comps):
if len(comps) > 1_000_000 and not is_object_dtype(comps):
f = np.in1d
elif is_integer_dtype(comps):
try:
Expand Down Expand Up @@ -833,8 +832,8 @@ def mode(values, dropna: bool = True) -> ABCSeries:
result = f(values, dropna=dropna)
try:
result = np.sort(result)
except TypeError as e:
warn(f"Unable to sort modes: {e}")
except TypeError as err:
warn(f"Unable to sort modes: {err}")

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

def _interpolate(a, b, fraction):
"""Returns the point at the given fraction between a and b, where
"""
Returns the point at the given fraction between a and b, where
'fraction' must be between 0 and 1.
"""
return a + (b - a) * fraction
Expand Down Expand Up @@ -1192,7 +1192,8 @@ def compute(self, method):
)

def get_indexer(current_indexer, other_indexer):
"""Helper function to concat `current_indexer` and `other_indexer`
"""
Helper function to concat `current_indexer` and `other_indexer`
depending on `method`
"""
if method == "nsmallest":
Expand Down Expand Up @@ -1660,7 +1661,7 @@ def take_nd(

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

if not is_list_like(codes):
raise TypeError(
"Only list-like objects or None are allowed to be"
"passed to safe_sort as codes"
"Only list-like objects or None are allowed to "
"be passed to safe_sort as codes"
)
codes = ensure_platform_int(np.asarray(codes))

Expand Down
5 changes: 2 additions & 3 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,14 +494,13 @@ def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
if is_extension_array_dtype(dtype):
return array(self, dtype=dtype, copy=copy) # type: ignore # GH 28770
if is_integer_dtype(dtype) and self.isna().any():
msg = "Cannot convert float NaN to integer"
raise ValueError(msg)
raise ValueError("Cannot convert float NaN to integer")
return np.array(self, dtype=dtype, copy=copy)

@cache_readonly
def size(self) -> int:
"""
return the len of myself
Return the len of myself.
"""
return self._codes.size

Expand Down
8 changes: 6 additions & 2 deletions pandas/core/computation/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def _zip_axes_from_type(typ, new_axes):


def _any_pandas_objects(terms) -> bool:
"""Check a sequence of terms for instances of PandasObject."""
"""
Check a sequence of terms for instances of PandasObject.
"""
return any(isinstance(term.value, PandasObject) for term in terms)


Expand Down Expand Up @@ -116,7 +118,9 @@ def _align_core(terms):


def align_terms(terms):
"""Align a set of terms"""
"""
Align a set of terms.
"""
try:
# flatten the parse tree (a nested list, really)
terms = list(com.flatten(terms))
Expand Down
16 changes: 11 additions & 5 deletions pandas/core/computation/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@


def _ensure_decoded(s):
""" if we have bytes, decode them to unicode """
"""
If we have bytes, decode them to unicode.
"""
if isinstance(s, (np.bytes_, bytes)):
s = s.decode(get_option("display.encoding"))
return s


def result_type_many(*arrays_and_dtypes):
""" wrapper around numpy.result_type which overcomes the NPY_MAXARGS (32)
argument limit """
"""
Wrapper around numpy.result_type which overcomes the NPY_MAXARGS (32)
argument limit.
"""
try:
return np.result_type(*arrays_and_dtypes)
except ValueError:
Expand All @@ -26,8 +30,10 @@ def result_type_many(*arrays_and_dtypes):


def _remove_spaces_column_name(name):
"""Check if name contains any spaces, if it contains any spaces
the spaces will be removed and an underscore suffix is added."""
"""
Check if name contains any spaces, if it contains any spaces
the spaces will be removed and an underscore suffix is added.
"""
if not isinstance(name, str) or " " not in name:
return name

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/computation/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,8 @@ def __init__(self, lhs, rhs, **kwargs):

if not isnumeric(lhs.return_type) or not isnumeric(rhs.return_type):
raise TypeError(
f"unsupported operand type(s) for {self.op}:"
f" '{lhs.return_type}' and '{rhs.return_type}'"
f"unsupported operand type(s) for {self.op}: "
f"'{lhs.return_type}' and '{rhs.return_type}'"
)

# do not upcast float32s to float64 un-necessarily
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2376,7 +2376,8 @@ def test_east_asian_unicode_series(self):

# object dtype, longer than unicode repr
s = Series(
[1, 22, 3333, 44444], index=[1, "AB", pd.Timestamp("2011-01-01"), "あああ"]
[1, 22, 3333, 44444],
index=[1, "AB", pd.Timestamp("2011-01-01"), "あああ"],
)
expected = (
"1 1\n"
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/parser/test_usecols.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def test_usecols_with_whitespace(all_parsers):
# Column selection by index.
([0, 1], DataFrame(data=[[1000, 2000], [4000, 5000]], columns=["2", "0"])),
# Column selection by name.
(["0", "1"], DataFrame(data=[[2000, 3000], [5000, 6000]], columns=["0", "1"])),
(["0", "1"], DataFrame(data=[[2000, 3000], [5000, 6000]], columns=["0", "1"]),),
],
)
def test_usecols_with_integer_like_header(all_parsers, usecols, expected):
Expand Down
9 changes: 5 additions & 4 deletions pandas/tests/io/pytables/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@

@pytest.fixture
def pytables_hdf5_file():
"""Use PyTables to create a simple HDF5 file."""

"""
Use PyTables to create a simple HDF5 file.
"""
table_schema = {
"c0": tables.Time64Col(pos=0),
"c1": tables.StringCol(5, pos=1),
"c2": tables.Int64Col(pos=2),
}

t0 = 1561105000.0
t0 = 1_561_105_000.0

testsamples = [
{"c0": t0, "c1": "aaaaa", "c2": 1},
{"c0": t0 + 1, "c1": "bbbbb", "c2": 2},
{"c0": t0 + 2, "c1": "ccccc", "c2": 10 ** 5},
{"c0": t0 + 3, "c1": "ddddd", "c2": 4294967295},
{"c0": t0 + 3, "c1": "ddddd", "c2": 4_294_967_295},
]

objname = "pandas_test_timeseries"
Expand Down

0 comments on commit 4156ec1

Please sign in to comment.