Skip to content

Commit b12e5ba

Browse files
TomAugspurgerjreback
authored andcommitted
Safer is dtype (pandas-dev#22975)
1 parent c19c805 commit b12e5ba

File tree

6 files changed

+43
-15
lines changed

6 files changed

+43
-15
lines changed

.travis.yml

+7-12
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,20 @@ matrix:
5353
- dist: trusty
5454
env:
5555
- JOB="3.6, coverage" ENV_FILE="ci/travis-36.yaml" TEST_ARGS="--skip-slow --skip-network" PANDAS_TESTING_MODE="deprecate" COVERAGE=true DOCTEST=true
56-
# In allow_failures
57-
- dist: trusty
58-
env:
59-
- JOB="3.6, slow" ENV_FILE="ci/travis-36-slow.yaml" SLOW=true
60-
# In allow_failures
56+
6157
- dist: trusty
6258
env:
6359
- JOB="3.7, NumPy dev" ENV_FILE="ci/travis-37-numpydev.yaml" TEST_ARGS="--skip-slow --skip-network -W error" PANDAS_TESTING_MODE="deprecate"
6460
addons:
6561
apt:
6662
packages:
6763
- xsel
64+
65+
# In allow_failures
66+
- dist: trusty
67+
env:
68+
- JOB="3.6, slow" ENV_FILE="ci/travis-36-slow.yaml" SLOW=true
69+
6870
# In allow_failures
6971
- dist: trusty
7072
env:
@@ -73,13 +75,6 @@ matrix:
7375
- dist: trusty
7476
env:
7577
- JOB="3.6, slow" ENV_FILE="ci/travis-36-slow.yaml" SLOW=true
76-
- dist: trusty
77-
env:
78-
- JOB="3.7, NumPy dev" ENV_FILE="ci/travis-37-numpydev.yaml" TEST_ARGS="--skip-slow --skip-network -W error" PANDAS_TESTING_MODE="deprecate"
79-
addons:
80-
apt:
81-
packages:
82-
- xsel
8378
- dist: trusty
8479
env:
8580
- JOB="3.6, doc" ENV_FILE="ci/travis-36-doc.yaml" DOC=true

pandas/core/dtypes/base.py

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

44
from pandas import compat
5+
from pandas.core.dtypes.generic import ABCSeries, ABCIndexClass, ABCDataFrame
56
from pandas.errors import AbstractMethodError
67

78

@@ -83,7 +84,12 @@ def is_dtype(cls, dtype):
8384
"""
8485
dtype = getattr(dtype, 'dtype', dtype)
8586

86-
if isinstance(dtype, np.dtype):
87+
if isinstance(dtype, (ABCSeries, ABCIndexClass,
88+
ABCDataFrame, np.dtype)):
89+
# https://github.com/pandas-dev/pandas/issues/22960
90+
# avoid passing data to `construct_from_string`. This could
91+
# cause a FutureWarning from numpy about failing elementwise
92+
# comparison from, e.g., comparing DataFrame == 'category'.
8793
return False
8894
elif dtype is None:
8995
return False

pandas/core/frame.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4908,7 +4908,8 @@ def _combine_match_index(self, other, func, level=None):
49084908
return ops.dispatch_to_series(left, right, func)
49094909
else:
49104910
# fastpath --> operate directly on values
4911-
new_data = func(left.values.T, right.values).T
4911+
with np.errstate(all="ignore"):
4912+
new_data = func(left.values.T, right.values).T
49124913
return self._constructor(new_data,
49134914
index=left.index, columns=self.columns,
49144915
copy=False)

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4228,7 +4228,7 @@ def _try_cast(arr, take_fast_path):
42284228
try:
42294229
# gh-15832: Check if we are requesting a numeric dype and
42304230
# that we can convert the data to the requested dtype.
4231-
if is_float_dtype(dtype) or is_integer_dtype(dtype):
4231+
if is_integer_dtype(dtype):
42324232
subarr = maybe_cast_to_integer_array(arr, dtype)
42334233

42344234
subarr = maybe_cast_to_datetime(arr, dtype)

pandas/tests/dtypes/test_dtypes.py

+20
Original file line numberDiff line numberDiff line change
@@ -815,3 +815,23 @@ def test_registry_find(dtype, expected):
815815
('datetime64[ns, US/Eastern]', DatetimeTZDtype('ns', 'US/Eastern'))])
816816
def test_pandas_registry_find(dtype, expected):
817817
assert _pandas_registry.find(dtype) == expected
818+
819+
820+
@pytest.mark.parametrize("check", [
821+
is_categorical_dtype,
822+
is_datetime64tz_dtype,
823+
is_period_dtype,
824+
is_datetime64_ns_dtype,
825+
is_datetime64_dtype,
826+
is_interval_dtype,
827+
is_datetime64_any_dtype,
828+
is_string_dtype,
829+
is_bool_dtype,
830+
])
831+
def test_is_dtype_no_warning(check):
832+
data = pd.DataFrame({"A": [1, 2]})
833+
with tm.assert_produces_warning(None):
834+
check(data)
835+
836+
with tm.assert_produces_warning(None):
837+
check(data["A"])

pandas/tests/frame/test_operators.py

+6
Original file line numberDiff line numberDiff line change
@@ -1030,3 +1030,9 @@ def test_alignment_non_pandas(self):
10301030
align(df, val, 'index')
10311031
with pytest.raises(ValueError):
10321032
align(df, val, 'columns')
1033+
1034+
def test_no_warning(self, all_arithmetic_operators):
1035+
df = pd.DataFrame({"A": [0., 0.], "B": [0., None]})
1036+
b = df['B']
1037+
with tm.assert_produces_warning(None):
1038+
getattr(df, all_arithmetic_operators)(b, 0)

0 commit comments

Comments
 (0)