Skip to content

Commit dca809b

Browse files
authored
CLN: Remove long and integer_types from pandas.compat (#25862)
1 parent 72f4098 commit dca809b

29 files changed

+86
-127
lines changed

pandas/compat/__init__.py

-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
Key items to import for 2/3 compatible code:
88
* iterators: reduce()
99
* lists: lrange(), lmap(), lzip(), lfilter()
10-
* longs: long (int in Python 3)
1110
* iterable method compatibility: iteritems, iterkeys, itervalues
1211
* Uses the original method if available, otherwise uses items, keys, values.
1312
* types:
@@ -108,7 +107,6 @@ def get_range_parameters(data):
108107
# have to explicitly put builtins into the namespace
109108
intern = sys.intern
110109
reduce = functools.reduce
111-
long = int
112110
unichr = chr
113111

114112
# list-producing versions of the major Python iterating functions
@@ -172,7 +170,6 @@ def get_range_parameters(data):
172170
# import iterator versions of these functions
173171
intern = intern
174172
reduce = reduce
175-
long = long
176173
unichr = unichr
177174

178175
# Python 2-builtin ranges produce lists
@@ -250,7 +247,6 @@ class to receive bound method
250247

251248
if PY3:
252249
string_types = str,
253-
integer_types = int,
254250
class_types = type,
255251
text_type = str
256252
binary_type = bytes
@@ -293,7 +289,6 @@ def set_function_name(f, name, cls):
293289
return f
294290
else:
295291
string_types = basestring,
296-
integer_types = (int, long)
297292
class_types = (type, types.ClassType)
298293
text_type = unicode
299294
binary_type = str

pandas/core/internals/blocks.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1959,9 +1959,9 @@ def _can_hold_element(self, element):
19591959
not issubclass(tipo.type, (np.datetime64, np.timedelta64)))
19601960
return (
19611961
isinstance(
1962-
element, (float, int, np.floating, np.int_, compat.long))
1963-
and not isinstance(element, (bool, np.bool_, datetime, timedelta,
1964-
np.datetime64, np.timedelta64)))
1962+
element, (float, int, np.floating, np.int_)) and
1963+
not isinstance(element, (bool, np.bool_, datetime, timedelta,
1964+
np.datetime64, np.timedelta64)))
19651965

19661966
def to_native_types(self, slicer=None, na_rep='', float_format=None,
19671967
decimal='.', quoting=None, **kwargs):
@@ -2011,8 +2011,8 @@ def _can_hold_element(self, element):
20112011
return (
20122012
isinstance(
20132013
element,
2014-
(float, int, complex, np.float_, np.int_, compat.long))
2015-
and not isinstance(element, (bool, np.bool_)))
2014+
(float, int, complex, np.float_, np.int_)) and
2015+
not isinstance(element, (bool, np.bool_)))
20162016

20172017
def should_store(self, value):
20182018
return issubclass(value.dtype.type, np.complexfloating)

pandas/core/sorting.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from pandas._libs import algos, hashtable, lib
77
from pandas._libs.hashtable import unique_label_indices
8-
from pandas.compat import PY3, long, string_types
8+
from pandas.compat import PY3, string_types
99

1010
from pandas.core.dtypes.cast import infer_dtype_from_array
1111
from pandas.core.dtypes.common import (
@@ -45,9 +45,9 @@ def get_group_index(labels, shape, sort, xnull):
4545
labels are equal at all location.
4646
"""
4747
def _int64_cut_off(shape):
48-
acc = long(1)
48+
acc = 1
4949
for i, mul in enumerate(shape):
50-
acc *= long(mul)
50+
acc *= int(mul)
5151
if not acc < _INT64_MAX:
5252
return i
5353
return len(shape)
@@ -122,9 +122,9 @@ def get_compressed_ids(labels, sizes):
122122

123123

124124
def is_int64_overflow_possible(shape):
125-
the_prod = long(1)
125+
the_prod = 1
126126
for x in shape:
127-
the_prod *= long(x)
127+
the_prod *= int(x)
128128

129129
return the_prod >= _INT64_MAX
130130

pandas/io/json/json.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pandas._libs.json as json
88
from pandas._libs.tslibs import iNaT
9-
from pandas.compat import StringIO, long, to_str
9+
from pandas.compat import StringIO, to_str
1010
from pandas.errors import AbstractMethodError
1111

1212
from pandas.core.dtypes.common import is_period_dtype
@@ -619,10 +619,10 @@ class Parser(object):
619619

620620
_STAMP_UNITS = ('s', 'ms', 'us', 'ns')
621621
_MIN_STAMPS = {
622-
's': long(31536000),
623-
'ms': long(31536000000),
624-
'us': long(31536000000000),
625-
'ns': long(31536000000000000)}
622+
's': 31536000,
623+
'ms': 31536000000,
624+
'us': 31536000000000,
625+
'ns': 31536000000000000}
626626

627627
def __init__(self, json, orient, dtype=None, convert_axes=True,
628628
convert_dates=True, keep_default_dates=False, numpy=False,

pandas/io/stata.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -765,9 +765,9 @@ class StataMissingValue(StringMixin):
765765
bases = (101, 32741, 2147483621)
766766
for b in bases:
767767
# Conversion to long to avoid hash issues on 32 bit platforms #8968
768-
MISSING_VALUES[compat.long(b)] = '.'
768+
MISSING_VALUES[b] = '.'
769769
for i in range(1, 27):
770-
MISSING_VALUES[compat.long(i + b)] = '.' + chr(96 + i)
770+
MISSING_VALUES[i + b] = '.' + chr(96 + i)
771771

772772
float32_base = b'\x00\x00\x00\x7f'
773773
increment = struct.unpack('<i', b'\x00\x08\x00\x00')[0]
@@ -798,8 +798,8 @@ class StataMissingValue(StringMixin):
798798

799799
def __init__(self, value):
800800
self._value = value
801-
# Conversion to long to avoid hash issues on 32 bit platforms #8968
802-
value = compat.long(value) if value < 2147483648 else float(value)
801+
# Conversion to int to avoid hash issues on 32 bit platforms #8968
802+
value = int(value) if value < 2147483648 else float(value)
803803
self._str = self.MISSING_VALUES[value]
804804

805805
string = property(lambda self: self._str,

pandas/tests/arithmetic/conftest.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import numpy as np
33
import pytest
44

5-
from pandas.compat import long
6-
75
import pandas as pd
86
import pandas.util.testing as tm
97

@@ -32,7 +30,7 @@ def one(request):
3230
for dtype in [np.int64, np.uint64, np.float64]]
3331
zeros.extend([np.array(0, dtype=dtype)
3432
for dtype in [np.int64, np.uint64, np.float64]])
35-
zeros.extend([0, 0.0, long(0)])
33+
zeros.extend([0, 0.0])
3634

3735

3836
@pytest.fixture(params=zeros)

pandas/tests/arithmetic/test_period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ def test_pi_add_iadd_int(self, one):
674674
def test_pi_sub_isub_int(self, one):
675675
"""
676676
PeriodIndex.__sub__ and __isub__ with several representations of
677-
the integer 1, e.g. int, long, np.int64, np.uint8, ...
677+
the integer 1, e.g. int, np.int64, np.uint8, ...
678678
"""
679679
rng = pd.period_range('2000-01-01 09:00', freq='H', periods=10)
680680
result = rng - one

pandas/tests/arrays/categorical/test_dtypes.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import numpy as np
33
import pytest
44

5-
from pandas.compat import long
6-
75
from pandas.core.dtypes.dtypes import CategoricalDtype
86

97
from pandas import Categorical, CategoricalIndex, Index, Series, Timestamp
@@ -165,10 +163,9 @@ def test_astype_category(self, dtype_ordered, cat_ordered):
165163

166164
def test_iter_python_types(self):
167165
# GH-19909
168-
# TODO(Py2): Remove long
169166
cat = Categorical([1, 2])
170-
assert isinstance(list(cat)[0], (int, long))
171-
assert isinstance(cat.tolist()[0], (int, long))
167+
assert isinstance(list(cat)[0], int)
168+
assert isinstance(cat.tolist()[0], int)
172169

173170
def test_iter_python_types_datetime(self):
174171
cat = Categorical([Timestamp('2017-01-01'),

pandas/tests/arrays/test_datetimelike.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import numpy as np
33
import pytest
44

5-
import pandas.compat as compat
6-
75
import pandas as pd
86
from pandas.core.arrays import DatetimeArray, PeriodArray, TimedeltaArray
97
import pandas.util.testing as tm
@@ -133,10 +131,10 @@ def test_unbox_scalar(self):
133131
data = np.arange(10, dtype='i8') * 24 * 3600 * 10**9
134132
arr = self.array_cls(data, freq='D')
135133
result = arr._unbox_scalar(arr[0])
136-
assert isinstance(result, (int, compat.long))
134+
assert isinstance(result, int)
137135

138136
result = arr._unbox_scalar(pd.NaT)
139-
assert isinstance(result, (int, compat.long))
137+
assert isinstance(result, int)
140138

141139
with pytest.raises(ValueError):
142140
arr._unbox_scalar('foo')

pandas/tests/arrays/test_integer.py

-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,6 @@ def test_conversions(data_missing):
509509
if pd.isnull(r):
510510
assert pd.isnull(e)
511511
elif is_integer(r):
512-
# PY2 can be int or long
513512
assert r == e
514513
assert is_integer(e)
515514
else:

pandas/tests/frame/test_api.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import numpy as np
1010
import pytest
1111

12-
from pandas.compat import long, lrange
12+
from pandas.compat import lrange
1313

1414
import pandas as pd
1515
from pandas import (
@@ -242,15 +242,15 @@ def test_itertuples(self, float_frame):
242242
'ints': lrange(5)}, columns=['floats', 'ints'])
243243

244244
for tup in df.itertuples(index=False):
245-
assert isinstance(tup[1], (int, long))
245+
assert isinstance(tup[1], int)
246246

247247
df = self.klass(data={"a": [1, 2, 3], "b": [4, 5, 6]})
248248
dfaa = df[['a', 'a']]
249249

250250
assert (list(dfaa.itertuples()) ==
251251
[(0, 1, 1), (1, 2, 2), (2, 3, 3)])
252252

253-
# repr with be int/long on 32-bit/windows
253+
# repr with int on 32-bit/windows
254254
if not (compat.is_platform_windows() or compat.is_platform_32bit()):
255255
assert (repr(list(df.itertuples(name=None))) ==
256256
'[(0, 1, 4), (1, 2, 5), (2, 3, 6)]')

pandas/tests/frame/test_constructors.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
import numpy.ma as ma
1212
import pytest
1313

14-
from pandas.compat import (
15-
PY36, is_platform_little_endian, lmap, long, lrange, lzip)
14+
from pandas.compat import PY36, is_platform_little_endian, lmap, lrange, lzip
1615

1716
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
1817
from pandas.core.dtypes.common import is_integer_dtype
@@ -193,9 +192,8 @@ def test_constructor_overflow_int64(self):
193192

194193
# see gh-2355
195194
data_scores = [(6311132704823138710, 273), (2685045978526272070, 23),
196-
(8921811264899370420, 45),
197-
(long(17019687244989530680), 270),
198-
(long(9930107427299601010), 273)]
195+
(8921811264899370420, 45), (17019687244989530680, 270),
196+
(9930107427299601010, 273)]
199197
dtype = [('uid', 'u8'), ('score', 'u8')]
200198
data = np.zeros((len(data_scores),), dtype=dtype)
201199
data[:] = data_scores

pandas/tests/frame/test_convert_to.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import pytest
99
import pytz
1010

11-
from pandas.compat import long
12-
1311
from pandas import (
1412
CategoricalDtype, DataFrame, MultiIndex, Series, Timestamp, compat,
1513
date_range)
@@ -472,7 +470,7 @@ def test_to_dict_box_scalars(self, orient, item_getter):
472470
# make sure that we are boxing properly
473471
df = DataFrame({'a': [1, 2], 'b': [.1, .2]})
474472
result = df.to_dict(orient=orient)
475-
assert isinstance(item_getter(result, 'a', 0), (int, long))
473+
assert isinstance(item_getter(result, 'a', 0), int)
476474
assert isinstance(item_getter(result, 'b', 0), float)
477475

478476
def test_frame_to_dict_tz(self):

pandas/tests/frame/test_indexing.py

+9-14
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pytest
1010

1111
from pandas._libs.tslib import iNaT
12-
from pandas.compat import long, lrange, lzip
12+
from pandas.compat import lrange, lzip
1313

1414
from pandas.core.dtypes.common import is_float_dtype, is_integer, is_scalar
1515
from pandas.core.dtypes.dtypes import CategoricalDtype
@@ -2560,19 +2560,14 @@ def test_boolean_indexing(self):
25602560

25612561
def test_boolean_indexing_mixed(self):
25622562
df = DataFrame({
2563-
long(0): {35: np.nan, 40: np.nan, 43: np.nan,
2564-
49: np.nan, 50: np.nan},
2565-
long(1): {35: np.nan,
2566-
40: 0.32632316859446198,
2567-
43: np.nan,
2568-
49: 0.32632316859446198,
2569-
50: 0.39114724480578139},
2570-
long(2): {35: np.nan, 40: np.nan, 43: 0.29012581014105987,
2571-
49: np.nan, 50: np.nan},
2572-
long(3): {35: np.nan, 40: np.nan, 43: np.nan, 49: np.nan,
2573-
50: np.nan},
2574-
long(4): {35: 0.34215328467153283, 40: np.nan, 43: np.nan,
2575-
49: np.nan, 50: np.nan},
2563+
0: {35: np.nan, 40: np.nan, 43: np.nan, 49: np.nan, 50: np.nan},
2564+
1: {35: np.nan, 40: 0.32632316859446198, 43: np.nan,
2565+
49: 0.32632316859446198, 50: 0.39114724480578139},
2566+
2: {35: np.nan, 40: np.nan, 43: 0.29012581014105987, 49: np.nan,
2567+
50: np.nan},
2568+
3: {35: np.nan, 40: np.nan, 43: np.nan, 49: np.nan, 50: np.nan},
2569+
4: {35: 0.34215328467153283, 40: np.nan, 43: np.nan, 49: np.nan,
2570+
50: np.nan},
25762571
'y': {35: 0, 40: 0, 43: 0, 49: 0, 50: 1}})
25772572

25782573
# mixed int/float ok

pandas/tests/groupby/test_groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ def test_empty_groups_corner(mframe):
770770

771771
def test_nonsense_func():
772772
df = DataFrame([0])
773-
msg = r"unsupported operand type\(s\) for \+: '(int|long)' and 'str'"
773+
msg = r"unsupported operand type\(s\) for \+: 'int' and 'str'"
774774
with pytest.raises(TypeError, match=msg):
775775
df.groupby(lambda x: x + 'foo')
776776

pandas/tests/groupby/test_grouping.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66
import pytest
77

8-
from pandas.compat import long, lrange
8+
from pandas.compat import lrange
99

1010
import pandas as pd
1111
from pandas import (
@@ -396,21 +396,21 @@ def test_multiindex_columns_empty_level(self):
396396
lst = [['count', 'values'], ['to filter', '']]
397397
midx = MultiIndex.from_tuples(lst)
398398

399-
df = DataFrame([[long(1), 'A']], columns=midx)
399+
df = DataFrame([[1, 'A']], columns=midx)
400400

401401
grouped = df.groupby('to filter').groups
402402
assert grouped['A'] == [0]
403403

404404
grouped = df.groupby([('to filter', '')]).groups
405405
assert grouped['A'] == [0]
406406

407-
df = DataFrame([[long(1), 'A'], [long(2), 'B']], columns=midx)
407+
df = DataFrame([[1, 'A'], [2, 'B']], columns=midx)
408408

409409
expected = df.groupby('to filter').groups
410410
result = df.groupby([('to filter', '')]).groups
411411
assert result == expected
412412

413-
df = DataFrame([[long(1), 'A'], [long(2), 'A']], columns=midx)
413+
df = DataFrame([[1, 'A'], [2, 'A']], columns=midx)
414414

415415
expected = df.groupby('to filter').groups
416416
result = df.groupby([('to filter', '')]).groups

pandas/tests/indexes/conftest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
import pytest
33

4-
from pandas.compat import long, lzip
4+
from pandas.compat import lzip
55

66
import pandas as pd
77
from pandas.core.indexes.api import Index, MultiIndex
@@ -39,7 +39,7 @@ def one(request):
3939
for dtype in [np.int64, np.uint64, np.float64]]
4040
zeros.extend([np.array(0, dtype=dtype)
4141
for dtype in [np.int64, np.uint64, np.float64]])
42-
zeros.extend([0, 0.0, long(0)])
42+
zeros.extend([0, 0.0])
4343

4444

4545
@pytest.fixture(params=zeros)

0 commit comments

Comments
 (0)