Skip to content

STY: Underscores for long numbers #30169

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
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 11 additions & 11 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ class DatetimeArray(dtl.DatetimeLikeArrayMixin, dtl.TimelikeOps, dtl.DatelikeOps
# Timestamp.__richcmp__(DateTimeArray) operates pointwise

# ensure that operations with numpy arrays defer to our implementation
__array_priority__ = 1000
__array_priority__ = 1_000

# -----------------------------------------------------------------
# Constructors
Expand Down Expand Up @@ -663,7 +663,7 @@ def __iter__(self):
# convert in chunks of 10k for efficiency
data = self.asi8
length = len(self)
chunksize = 10000
chunksize = 10_000
chunks = int(length / chunksize) + 1
for i in range(chunks):
start_i = i * chunksize
Expand Down Expand Up @@ -1076,9 +1076,9 @@ def tz_localize(self, tz, ambiguous="raise", nonexistent="raise"):
nonexistent, timedelta
):
raise ValueError(
"The nonexistent argument must be one of 'raise',"
" 'NaT', 'shift_forward', 'shift_backward' or"
" a timedelta object"
"The nonexistent argument must be one of 'raise', "
"'NaT', 'shift_forward', 'shift_backward' or "
"a timedelta object"
)

if self.tz is not None:
Expand Down Expand Up @@ -1151,7 +1151,7 @@ def normalize(self):
"""
if self.tz is None or timezones.is_utc(self.tz):
not_null = ~self.isna()
DAY_NS = ccalendar.DAY_SECONDS * 1000000000
DAY_NS = ccalendar.DAY_SECONDS * 1_000_000_000
new_values = self.asi8.copy()
adjustment = new_values[not_null] % DAY_NS
new_values[not_null] = new_values[not_null] - adjustment
Expand Down Expand Up @@ -1767,13 +1767,13 @@ def to_julian_date(self):
+ np.floor(year / 4)
- np.floor(year / 100)
+ np.floor(year / 400)
+ 1721118.5
+ 1_721_118.5
+ (
self.hour
+ self.minute / 60.0
+ self.second / 3600.0
+ self.microsecond / 3600.0 / 1e6
+ self.nanosecond / 3600.0 / 1e9
+ self.second / 3_600.0
+ self.microsecond / 3_600.0 / 1e6
+ self.nanosecond / 3_600.0 / 1e9
)
/ 24.0
)
Expand Down Expand Up @@ -2031,7 +2031,7 @@ def maybe_convert_dtype(data, copy):
# Note: without explicitly raising here, PeriodIndex
# test_setops.test_join_does_not_recur fails
raise TypeError(
"Passing PeriodDtype data is invalid. Use `data.to_timestamp()` instead"
"Passing PeriodDtype data is invalid. Use `data.to_timestamp()` instead"
)

elif is_categorical_dtype(data):
Expand Down
7 changes: 3 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,6 @@ def _repr_fits_horizontal_(self, ignore_width: bool = False) -> bool:
users expect. display.max_columns remains in effect.
GH3541, GH3573
"""

width, height = console.get_console_size()
max_columns = get_option("display.max_columns")
nb_columns = len(self.columns)
Expand Down Expand Up @@ -2359,11 +2358,11 @@ def _non_verbose_repr():
def _sizeof_fmt(num, size_qualifier):
# returns size in human readable format
for x in ["bytes", "KB", "MB", "GB", "TB"]:
if num < 1024.0:
if num < 1_024.0:
return "{num:3.1f}{size_q} {x}".format(
num=num, size_q=size_qualifier, x=x
)
num /= 1024.0
num /= 1_024.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont think this adds clarity. AFAIK we dont have a policy on this, but maybe something like 7+ digits?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I also think for relatively small numbers this does not improve readability IMO.

Also, I don't think we need to do this for random numbers in the tests. Yes to using underscores in meaningful numbers (such as in ccalendar.DAY_SECONDS * 1_000_000_000), but for random values in tests asserts, I don't see the added value.

return "{num:3.1f}{size_q} {pb}".format(
num=num, size_q=size_qualifier, pb="PB"
)
Expand Down Expand Up @@ -3542,7 +3541,7 @@ def lookup(self, row_labels, col_labels):
if n != len(col_labels):
raise ValueError("Row labels must have same size as column labels")

thresh = 1000
thresh = 1_000
if not self._is_mixed_type or n > thresh:
values = self.values
ridx = self.index.get_indexer(row_labels)
Expand Down
9 changes: 4 additions & 5 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1931,7 +1931,7 @@ def empty(self) -> bool_t:

# This is also set in IndexOpsMixin
# GH#23114 Ensure ndarray.__op__(DataFrame) returns NotImplemented
__array_priority__ = 1000
__array_priority__ = 1_000

def __array__(self, dtype=None):
return com.values_from_object(self)
Expand Down Expand Up @@ -9165,7 +9165,6 @@ def truncate(
2016-01-10 23:59:58 1
2016-01-10 23:59:59 1
"""

if axis is None:
axis = self._stat_axis_number
axis = self._get_axis_number(axis)
Expand Down Expand Up @@ -9391,9 +9390,9 @@ def tz_localize(
nonexistent, timedelta
):
raise ValueError(
"The nonexistent argument must be one of 'raise',"
" 'NaT', 'shift_forward', 'shift_backward' or"
" a timedelta object"
"The nonexistent argument must be one of 'raise', "
"'NaT', 'shift_forward', 'shift_backward' or "
"a timedelta object"
)

axis = self._get_axis_number(axis)
Expand Down
12 changes: 5 additions & 7 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,9 @@ def __init__(
if is_empty_data(data) and dtype is None:
# gh-17261
warnings.warn(
"The default dtype for empty Series will be 'object' instead"
" of 'float64' in a future version. Specify a dtype explicitly"
" to silence this warning.",
"The default dtype for empty Series will be 'object' instead "
"of 'float64' in a future version. Specify a dtype explicitly "
"to silence this warning.",
DeprecationWarning,
stacklevel=2,
)
Expand Down Expand Up @@ -257,14 +257,13 @@ def __init__(
raise AssertionError(
"Cannot pass both SingleBlockManager "
"`data` argument and a different "
"`index` argument. `copy` must "
"be False."
"`index` argument. `copy` must be False."
)

elif is_extension_array_dtype(data):
pass
elif isinstance(data, (set, frozenset)):
raise TypeError(f"{repr(type(data).__name__)} type is unordered")
raise TypeError(f"'{type(data).__name__}' type is unordered")
elif isinstance(data, ABCSparseArray):
# handle sparse passed here (and force conversion)
data = data.to_dense()
Expand Down Expand Up @@ -3721,7 +3720,6 @@ def _reduce(
If we have an ndarray as a value, then simply perform the operation,
otherwise delegate to the object.
"""

delegate = self._values

if axis is not None:
Expand Down
17 changes: 7 additions & 10 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def should_cache(
if len(arg) <= 50:
return False

if len(arg) <= 5000:
if len(arg) <= 5_000:
check_count = int(len(arg) * 0.1)
else:
check_count = 500
Expand Down Expand Up @@ -832,25 +832,22 @@ def coerce(values):
return values

values = (
coerce(arg[unit_rev["year"]]) * 10000
coerce(arg[unit_rev["year"]]) * 10_000
+ coerce(arg[unit_rev["month"]]) * 100
+ coerce(arg[unit_rev["day"]])
)
try:
values = to_datetime(values, format="%Y%m%d", errors=errors, utc=tz)
except (TypeError, ValueError) as e:
raise ValueError("cannot assemble the datetimes: {error}".format(error=e))
except (TypeError, ValueError) as err:
raise ValueError(f"cannot assemble the datetimes: {err}")

for u in ["h", "m", "s", "ms", "us", "ns"]:
value = unit_rev.get(u)
if value is not None and value in arg:
try:
values += to_timedelta(coerce(arg[value]), unit=u, errors=errors)
except (TypeError, ValueError) as e:
raise ValueError(
"cannot assemble the datetimes [{value}]: "
"{error}".format(value=value, error=e)
)
except (TypeError, ValueError) as err:
raise ValueError(f"cannot assemble the datetimes [{value}]: {err}")
return values


Expand All @@ -870,7 +867,7 @@ def calc(carg):
# calculate the actual result
carg = carg.astype(object)
parsed = parsing.try_parse_year_month_day(
carg / 10000, carg / 100 % 100, carg % 100
carg / 10_000, carg / 100 % 100, carg % 100
)
return tslib.array_to_datetime(parsed, errors=errors)[0]

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/window/ewm.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ def _apply(self, func, **kwargs):
cfunc = getattr(window_aggregations, func, None)
if cfunc is None:
raise ValueError(
"we do not support this function "
f"in window_aggregations.{func}"
f"we do not support this function in window_aggregations.{func}"
)

def func(arg):
Expand Down