Skip to content

Commit 39ef89b

Browse files
author
Oleh Kozynets
committed
Fix int inference in select_dtypes.
1 parent 288efe9 commit 39ef89b

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

pandas/core/dtypes/common.py

-5
Original file line numberDiff line numberDiff line change
@@ -1796,11 +1796,6 @@ def pandas_dtype(dtype) -> DtypeObj:
17961796
# try a numpy dtype
17971797
# raise a consistent TypeError if failed
17981798
try:
1799-
# int is mapped to different types (int32, in64) on Windows and Linux
1800-
# see https://github.com/numpy/numpy/issues/9464
1801-
if (isinstance(dtype, str) and dtype == "int") or (dtype is int):
1802-
dtype = "int64"
1803-
18041799
npdtype = np.dtype(dtype)
18051800
except SyntaxError as err:
18061801
# np.dtype uses `eval` which can raise SyntaxError

pandas/core/frame.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -3633,8 +3633,21 @@ def select_dtypes(self, include=None, exclude=None) -> DataFrame:
36333633
raise ValueError("at least one of include or exclude must be nonempty")
36343634

36353635
# convert the myriad valid dtypes object to a single representation
3636-
include = frozenset(infer_dtype_from_object(x) for x in include)
3637-
exclude = frozenset(infer_dtype_from_object(x) for x in exclude)
3636+
def check_int_infer_dtype(dtypes):
3637+
converted_dtypes = []
3638+
for dtype in dtypes:
3639+
# Numpy maps int to different types (int32, in64) on Windows and Linux
3640+
# see https://github.com/numpy/numpy/issues/9464
3641+
if (isinstance(dtype, str) and dtype == "int") or (dtype is int):
3642+
converted_dtypes.append(np.int32)
3643+
converted_dtypes.append(np.int64)
3644+
else:
3645+
converted_dtypes.append(infer_dtype_from_object(dtype))
3646+
return frozenset(converted_dtypes)
3647+
3648+
include = check_int_infer_dtype(include)
3649+
exclude = check_int_infer_dtype(exclude)
3650+
36383651
for dtypes in (include, exclude):
36393652
invalidate_string_dtypes(dtypes)
36403653

pandas/tests/dtypes/test_common.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,20 @@ def test_invalid_dtype_error(self, box):
4343
com.pandas_dtype(box)
4444

4545
@pytest.mark.parametrize(
46-
"dtype_input,dtype_output",
46+
"dtype",
4747
[
48-
(object, object),
49-
("float64", np.float64),
50-
(np.object_, np.object_),
51-
(np.dtype("object"), np.object_),
52-
("O", object),
53-
(np.float64, np.float64),
54-
(float, float),
55-
(np.dtype("float64"), np.float64),
56-
("int", np.int64),
57-
(int, np.int64),
48+
object,
49+
"float64",
50+
np.object_,
51+
np.dtype("object"),
52+
"O",
53+
np.float64,
54+
float,
55+
np.dtype("float64"),
5856
],
5957
)
60-
def test_pandas_dtype_valid(self, dtype_input, dtype_output):
61-
assert com.pandas_dtype(dtype_input) == dtype_output
58+
def test_pandas_dtype_valid(self, dtype):
59+
assert com.pandas_dtype(dtype) == dtype
6260

6361
@pytest.mark.parametrize(
6462
"dtype", ["M8[ns]", "m8[ns]", "object", "float64", "int64"]

0 commit comments

Comments
 (0)