Skip to content

Commit f9ce9d6

Browse files
authored
Remove Python2 numeric relicts (#38916)
1 parent a5fbc41 commit f9ce9d6

34 files changed

+76
-81
lines changed

asv_bench/benchmarks/arithmetic.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,18 @@ def setup(self, op):
122122
n_rows = 500
123123

124124
# construct dataframe with 2 blocks
125-
arr1 = np.random.randn(n_rows, int(n_cols / 2)).astype("f8")
126-
arr2 = np.random.randn(n_rows, int(n_cols / 2)).astype("f4")
125+
arr1 = np.random.randn(n_rows, n_cols // 2).astype("f8")
126+
arr2 = np.random.randn(n_rows, n_cols // 2).astype("f4")
127127
df = pd.concat(
128128
[pd.DataFrame(arr1), pd.DataFrame(arr2)], axis=1, ignore_index=True
129129
)
130130
# should already be the case, but just to be sure
131131
df._consolidate_inplace()
132132

133133
# TODO: GH#33198 the setting here shoudlnt need two steps
134-
arr1 = np.random.randn(n_rows, int(n_cols / 4)).astype("f8")
135-
arr2 = np.random.randn(n_rows, int(n_cols / 2)).astype("i8")
136-
arr3 = np.random.randn(n_rows, int(n_cols / 4)).astype("f8")
134+
arr1 = np.random.randn(n_rows, n_cols // 4).astype("f8")
135+
arr2 = np.random.randn(n_rows, n_cols // 2).astype("i8")
136+
arr3 = np.random.randn(n_rows, n_cols // 4).astype("f8")
137137
df2 = pd.concat(
138138
[pd.DataFrame(arr1), pd.DataFrame(arr2), pd.DataFrame(arr3)],
139139
axis=1,

asv_bench/benchmarks/frame_methods.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ class Repr:
263263
def setup(self):
264264
nrows = 10000
265265
data = np.random.randn(nrows, 10)
266-
arrays = np.tile(np.random.randn(3, int(nrows / 100)), 100)
266+
arrays = np.tile(np.random.randn(3, nrows // 100), 100)
267267
idx = MultiIndex.from_arrays(arrays)
268268
self.df3 = DataFrame(data, index=idx)
269269
self.df4 = DataFrame(data, index=np.random.randn(nrows))
@@ -648,9 +648,9 @@ class Describe:
648648
def setup(self):
649649
self.df = DataFrame(
650650
{
651-
"a": np.random.randint(0, 100, int(1e6)),
652-
"b": np.random.randint(0, 100, int(1e6)),
653-
"c": np.random.randint(0, 100, int(1e6)),
651+
"a": np.random.randint(0, 100, 10 ** 6),
652+
"b": np.random.randint(0, 100, 10 ** 6),
653+
"c": np.random.randint(0, 100, 10 ** 6),
654654
}
655655
)
656656

asv_bench/benchmarks/hash_functions.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ class Float64GroupIndex:
103103
# GH28303
104104
def setup(self):
105105
self.df = pd.date_range(
106-
start="1/1/2018", end="1/2/2018", periods=1e6
106+
start="1/1/2018", end="1/2/2018", periods=10 ** 6
107107
).to_frame()
108-
self.group_index = np.round(self.df.index.astype(int) / 1e9)
108+
self.group_index = np.round(self.df.index.astype(int) / 10 ** 9)
109109

110110
def time_groupby(self):
111111
self.df.groupby(self.group_index).last()

asv_bench/benchmarks/inference.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class ToNumericDowncast:
4242
]
4343

4444
N = 500000
45-
N2 = int(N / 2)
45+
N2 = N // 2
4646

4747
data_dict = {
4848
"string-int": ["1"] * N2 + [2] * N2,

asv_bench/benchmarks/join_merge.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def setup(self):
158158
daily_dates = date_index.to_period("D").to_timestamp("S", "S")
159159
self.fracofday = date_index.values - daily_dates.values
160160
self.fracofday = self.fracofday.astype("timedelta64[ns]")
161-
self.fracofday = self.fracofday.astype(np.float64) / 86400000000000.0
161+
self.fracofday = self.fracofday.astype(np.float64) / 86_400_000_000_000
162162
self.fracofday = Series(self.fracofday, daily_dates)
163163
index = date_range(date_index.min(), date_index.max(), freq="D")
164164
self.temp = Series(1.0, index)[self.fracofday.index]

asv_bench/benchmarks/rolling.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class PeakMemFixedWindowMinMax:
171171
params = ["min", "max"]
172172

173173
def setup(self, operation):
174-
N = int(1e6)
174+
N = 10 ** 6
175175
arr = np.random.random(N)
176176
self.roll = pd.Series(arr).rolling(2)
177177

@@ -233,7 +233,7 @@ class GroupbyLargeGroups:
233233

234234
def setup(self):
235235
N = 100000
236-
self.df = pd.DataFrame({"A": [1, 2] * int(N / 2), "B": np.random.randn(N)})
236+
self.df = pd.DataFrame({"A": [1, 2] * (N // 2), "B": np.random.randn(N)})
237237

238238
def time_rolling_multiindex_creation(self):
239239
self.df.groupby("A").rolling(3).mean()

asv_bench/benchmarks/series_methods.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def time_dir_strings(self):
284284
class SeriesGetattr:
285285
# https://github.com/pandas-dev/pandas/issues/19764
286286
def setup(self):
287-
self.s = Series(1, index=date_range("2012-01-01", freq="s", periods=int(1e6)))
287+
self.s = Series(1, index=date_range("2012-01-01", freq="s", periods=10 ** 6))
288288

289289
def time_series_datetimeindex_repr(self):
290290
getattr(self.s, "a", None)

asv_bench/benchmarks/timeseries.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def time_iso8601_tz_spaceformat(self):
346346
class ToDatetimeNONISO8601:
347347
def setup(self):
348348
N = 10000
349-
half = int(N / 2)
349+
half = N // 2
350350
ts_string_1 = "March 1, 2018 12:00:00+0400"
351351
ts_string_2 = "March 1, 2018 12:00:00+0500"
352352
self.same_offset = [ts_string_1] * N
@@ -376,7 +376,7 @@ def setup(self):
376376
self.same_offset = ["10/11/2018 00:00:00.045-07:00"] * N
377377
self.diff_offset = [
378378
f"10/11/2018 00:00:00.045-0{offset}:00" for offset in range(10)
379-
] * int(N / 10)
379+
] * (N // 10)
380380

381381
def time_exact(self):
382382
to_datetime(self.s2, format="%d%b%y")

pandas/_testing/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ def _create_missing_idx(nrows, ncols, density, random_state=None):
901901
random_state = np.random.RandomState(random_state)
902902

903903
# below is cribbed from scipy.sparse
904-
size = int(np.round((1 - density) * nrows * ncols))
904+
size = round((1 - density) * nrows * ncols)
905905
# generate a few more to ensure unique values
906906
min_rows = 5
907907
fac = 1.02

pandas/core/algorithms.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ def value_counts(
835835
result = result.sort_values(ascending=ascending)
836836

837837
if normalize:
838-
result = result / float(counts.sum())
838+
result = result / counts.sum()
839839

840840
return result
841841

pandas/core/arrays/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2214,7 +2214,7 @@ def describe(self):
22142214
A dataframe with frequency and counts by category.
22152215
"""
22162216
counts = self.value_counts(dropna=False)
2217-
freqs = counts / float(counts.sum())
2217+
freqs = counts / counts.sum()
22182218

22192219
from pandas.core.reshape.concat import concat
22202220

pandas/core/arrays/datetimes.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ def __iter__(self):
572572
data = self.asi8
573573
length = len(self)
574574
chunksize = 10000
575-
chunks = int(length / chunksize) + 1
575+
chunks = (length // chunksize) + 1
576576
for i in range(chunks):
577577
start_i = i * chunksize
578578
end_i = min((i + 1) * chunksize, length)
@@ -1847,12 +1847,12 @@ def to_julian_date(self):
18471847
+ 1_721_118.5
18481848
+ (
18491849
self.hour
1850-
+ self.minute / 60.0
1851-
+ self.second / 3600.0
1852-
+ self.microsecond / 3600.0 / 1e6
1853-
+ self.nanosecond / 3600.0 / 1e9
1850+
+ self.minute / 60
1851+
+ self.second / 3600
1852+
+ self.microsecond / 3600 / 10 ** 6
1853+
+ self.nanosecond / 3600 / 10 ** 9
18541854
)
1855-
/ 24.0
1855+
/ 24
18561856
)
18571857

18581858
# -----------------------------------------------------------------

pandas/core/arrays/sparse/array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ def density(self):
578578
>>> s.density
579579
0.6
580580
"""
581-
return float(self.sp_index.npoints) / float(self.sp_index.length)
581+
return self.sp_index.npoints / self.sp_index.length
582582

583583
@property
584584
def npoints(self) -> int:

pandas/core/arrays/timedeltas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def __iter__(self):
338338
data = self.asi8
339339
length = len(self)
340340
chunksize = 10000
341-
chunks = int(length / chunksize) + 1
341+
chunks = (length // chunksize) + 1
342342
for i in range(chunks):
343343
start_i = i * chunksize
344344
end_i = min((i + 1) * chunksize, length)

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5336,7 +5336,7 @@ def sample(
53365336
elif n is not None and frac is None and n % 1 != 0:
53375337
raise ValueError("Only integers accepted as `n` values")
53385338
elif n is None and frac is not None:
5339-
n = int(round(frac * axis_length))
5339+
n = round(frac * axis_length)
53405340
elif n is not None and frac is not None:
53415341
raise ValueError("Please enter a value for `frac` OR `n`, not both")
53425342

pandas/core/tools/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def should_cache(
122122
return False
123123

124124
if len(arg) <= 5000:
125-
check_count = int(len(arg) * 0.1)
125+
check_count = len(arg) // 10
126126
else:
127127
check_count = 500
128128
else:

pandas/core/window/ewm.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def get_center_of_mass(
6161
elif span is not None:
6262
if span < 1:
6363
raise ValueError("span must satisfy: span >= 1")
64-
comass = (span - 1) / 2.0
64+
comass = (span - 1) / 2
6565
elif halflife is not None:
6666
if halflife <= 0:
6767
raise ValueError("halflife must satisfy: halflife > 0")
@@ -70,7 +70,7 @@ def get_center_of_mass(
7070
elif alpha is not None:
7171
if alpha <= 0 or alpha > 1:
7272
raise ValueError("alpha must satisfy: 0 < alpha <= 1")
73-
comass = (1.0 - alpha) / alpha
73+
comass = (1 - alpha) / alpha
7474
else:
7575
raise ValueError("Must pass one of comass, span, halflife, or alpha")
7676

pandas/io/excel/_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ def _value_with_fmt(self, val):
860860
elif isinstance(val, datetime.date):
861861
fmt = self.date_format
862862
elif isinstance(val, datetime.timedelta):
863-
val = val.total_seconds() / float(86400)
863+
val = val.total_seconds() / 86400
864864
fmt = "0"
865865
else:
866866
val = str(val)

pandas/io/formats/csvs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def _generate_multiindex_header_rows(self) -> Iterator[List[Label]]:
282282

283283
def _save_body(self) -> None:
284284
nrows = len(self.data_index)
285-
chunks = int(nrows / self.chunksize) + 1
285+
chunks = (nrows // self.chunksize) + 1
286286
for i in range(chunks):
287287
start_i = i * self.chunksize
288288
end_i = min(start_i + self.chunksize, nrows)

pandas/io/formats/format.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,7 @@ def is_dates_only(
16371637

16381638
values_int = values.asi8
16391639
consider_values = values_int != iNaT
1640-
one_day_nanos = 86400 * 1e9
1640+
one_day_nanos = 86400 * 10 ** 9
16411641
even_days = (
16421642
np.logical_and(consider_values, values_int % int(one_day_nanos) != 0).sum() == 0
16431643
)
@@ -1741,7 +1741,7 @@ def get_format_timedelta64(
17411741

17421742
consider_values = values_int != iNaT
17431743

1744-
one_day_nanos = 86400 * 1e9
1744+
one_day_nanos = 86400 * 10 ** 9
17451745
even_days = (
17461746
np.logical_and(consider_values, values_int % one_day_nanos != 0).sum() == 0
17471747
)

pandas/io/formats/string.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def _fit_strcols_to_terminal_width(self, strcols: List[List[str]]) -> str:
160160
counter = 0
161161
while adj_dif > 0 and n_cols > 1:
162162
counter += 1
163-
mid = int(round(n_cols / 2.0))
163+
mid = round(n_cols / 2)
164164
mid_ix = col_lens.index[mid]
165165
col_len = col_lens[mid_ix]
166166
# adjoin adds one

pandas/io/sql.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ def insert(self, chunksize: Optional[int] = None, method: Optional[str] = None):
905905
elif chunksize == 0:
906906
raise ValueError("chunksize argument should be non-zero")
907907

908-
chunks = int(nrows / chunksize) + 1
908+
chunks = (nrows // chunksize) + 1
909909

910910
with self.pd_sql.run_transaction() as conn:
911911
for i in range(chunks):

pandas/plotting/_matplotlib/converter.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
SEC_PER_HOUR = SEC_PER_MIN * MIN_PER_HOUR
3939
SEC_PER_DAY = SEC_PER_HOUR * HOURS_PER_DAY
4040

41-
MUSEC_PER_DAY = 1e6 * SEC_PER_DAY
41+
MUSEC_PER_DAY = 10 ** 6 * SEC_PER_DAY
4242

4343
_mpl_units = {} # Cache for units overwritten by us
4444

@@ -116,7 +116,7 @@ def deregister():
116116

117117

118118
def _to_ordinalf(tm: pydt.time) -> float:
119-
tot_sec = tm.hour * 3600 + tm.minute * 60 + tm.second + float(tm.microsecond / 1e6)
119+
tot_sec = tm.hour * 3600 + tm.minute * 60 + tm.second + tm.microsecond / 10 ** 6
120120
return tot_sec
121121

122122

@@ -182,7 +182,7 @@ def __call__(self, x, pos=0) -> str:
182182
"""
183183
fmt = "%H:%M:%S.%f"
184184
s = int(x)
185-
msus = int(round((x - s) * 1e6))
185+
msus = round((x - s) * 10 ** 6)
186186
ms = msus // 1000
187187
us = msus % 1000
188188
m, s = divmod(s, 60)
@@ -429,7 +429,7 @@ def _from_ordinal(x, tz: Optional[tzinfo] = None) -> datetime:
429429
hour, remainder = divmod(24 * remainder, 1)
430430
minute, remainder = divmod(60 * remainder, 1)
431431
second, remainder = divmod(60 * remainder, 1)
432-
microsecond = int(1e6 * remainder)
432+
microsecond = int(1_000_000 * remainder)
433433
if microsecond < 10:
434434
microsecond = 0 # compensate for rounding errors
435435
dt = datetime(
@@ -439,7 +439,7 @@ def _from_ordinal(x, tz: Optional[tzinfo] = None) -> datetime:
439439
dt = dt.astimezone(tz)
440440

441441
if microsecond > 999990: # compensate for rounding errors
442-
dt += timedelta(microseconds=1e6 - microsecond)
442+
dt += timedelta(microseconds=1_000_000 - microsecond)
443443

444444
return dt
445445

@@ -611,27 +611,27 @@ def _second_finder(label_interval):
611611
info_fmt[day_start] = "%H:%M:%S\n%d-%b"
612612
info_fmt[year_start] = "%H:%M:%S\n%d-%b\n%Y"
613613

614-
if span < periodsperday / 12000.0:
614+
if span < periodsperday / 12000:
615615
_second_finder(1)
616-
elif span < periodsperday / 6000.0:
616+
elif span < periodsperday / 6000:
617617
_second_finder(2)
618-
elif span < periodsperday / 2400.0:
618+
elif span < periodsperday / 2400:
619619
_second_finder(5)
620-
elif span < periodsperday / 1200.0:
620+
elif span < periodsperday / 1200:
621621
_second_finder(10)
622-
elif span < periodsperday / 800.0:
622+
elif span < periodsperday / 800:
623623
_second_finder(15)
624-
elif span < periodsperday / 400.0:
624+
elif span < periodsperday / 400:
625625
_second_finder(30)
626-
elif span < periodsperday / 150.0:
626+
elif span < periodsperday / 150:
627627
_minute_finder(1)
628-
elif span < periodsperday / 70.0:
628+
elif span < periodsperday / 70:
629629
_minute_finder(2)
630-
elif span < periodsperday / 24.0:
630+
elif span < periodsperday / 24:
631631
_minute_finder(5)
632-
elif span < periodsperday / 12.0:
632+
elif span < periodsperday / 12:
633633
_minute_finder(15)
634-
elif span < periodsperday / 6.0:
634+
elif span < periodsperday / 6:
635635
_minute_finder(30)
636636
elif span < periodsperday / 2.5:
637637
_hour_finder(1, False)
@@ -1058,7 +1058,7 @@ def format_timedelta_ticks(x, pos, n_decimals: int) -> str:
10581058
"""
10591059
Convert seconds to 'D days HH:MM:SS.F'
10601060
"""
1061-
s, ns = divmod(x, 1e9)
1061+
s, ns = divmod(x, 10 ** 9)
10621062
m, s = divmod(s, 60)
10631063
h, m = divmod(m, 60)
10641064
d, h = divmod(h, 24)
@@ -1072,7 +1072,7 @@ def format_timedelta_ticks(x, pos, n_decimals: int) -> str:
10721072

10731073
def __call__(self, x, pos=0) -> str:
10741074
(vmin, vmax) = tuple(self.axis.get_view_interval())
1075-
n_decimals = int(np.ceil(np.log10(100 * 1e9 / abs(vmax - vmin))))
1075+
n_decimals = int(np.ceil(np.log10(100 * 10 ** 9 / abs(vmax - vmin))))
10761076
if n_decimals > 9:
10771077
n_decimals = 9
10781078
return self.format_timedelta_ticks(x, pos, n_decimals)

0 commit comments

Comments
 (0)