Skip to content

Commit

Permalink
Safer is dtype (#22975)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAugspurger authored and jreback committed Oct 4, 2018
1 parent c19c805 commit b12e5ba
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 15 deletions.
19 changes: 7 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,20 @@ matrix:
- dist: trusty
env:
- JOB="3.6, coverage" ENV_FILE="ci/travis-36.yaml" TEST_ARGS="--skip-slow --skip-network" PANDAS_TESTING_MODE="deprecate" COVERAGE=true DOCTEST=true
# In allow_failures
- dist: trusty
env:
- JOB="3.6, slow" ENV_FILE="ci/travis-36-slow.yaml" SLOW=true
# In allow_failures

- dist: trusty
env:
- JOB="3.7, NumPy dev" ENV_FILE="ci/travis-37-numpydev.yaml" TEST_ARGS="--skip-slow --skip-network -W error" PANDAS_TESTING_MODE="deprecate"
addons:
apt:
packages:
- xsel

# In allow_failures
- dist: trusty
env:
- JOB="3.6, slow" ENV_FILE="ci/travis-36-slow.yaml" SLOW=true

# In allow_failures
- dist: trusty
env:
Expand All @@ -73,13 +75,6 @@ matrix:
- dist: trusty
env:
- JOB="3.6, slow" ENV_FILE="ci/travis-36-slow.yaml" SLOW=true
- dist: trusty
env:
- JOB="3.7, NumPy dev" ENV_FILE="ci/travis-37-numpydev.yaml" TEST_ARGS="--skip-slow --skip-network -W error" PANDAS_TESTING_MODE="deprecate"
addons:
apt:
packages:
- xsel
- dist: trusty
env:
- JOB="3.6, doc" ENV_FILE="ci/travis-36-doc.yaml" DOC=true
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/dtypes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import numpy as np

from pandas import compat
from pandas.core.dtypes.generic import ABCSeries, ABCIndexClass, ABCDataFrame
from pandas.errors import AbstractMethodError


Expand Down Expand Up @@ -83,7 +84,12 @@ def is_dtype(cls, dtype):
"""
dtype = getattr(dtype, 'dtype', dtype)

if isinstance(dtype, np.dtype):
if isinstance(dtype, (ABCSeries, ABCIndexClass,
ABCDataFrame, np.dtype)):
# https://github.com/pandas-dev/pandas/issues/22960
# avoid passing data to `construct_from_string`. This could
# cause a FutureWarning from numpy about failing elementwise
# comparison from, e.g., comparing DataFrame == 'category'.
return False
elif dtype is None:
return False
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4908,7 +4908,8 @@ def _combine_match_index(self, other, func, level=None):
return ops.dispatch_to_series(left, right, func)
else:
# fastpath --> operate directly on values
new_data = func(left.values.T, right.values).T
with np.errstate(all="ignore"):
new_data = func(left.values.T, right.values).T
return self._constructor(new_data,
index=left.index, columns=self.columns,
copy=False)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4228,7 +4228,7 @@ def _try_cast(arr, take_fast_path):
try:
# gh-15832: Check if we are requesting a numeric dype and
# that we can convert the data to the requested dtype.
if is_float_dtype(dtype) or is_integer_dtype(dtype):
if is_integer_dtype(dtype):
subarr = maybe_cast_to_integer_array(arr, dtype)

subarr = maybe_cast_to_datetime(arr, dtype)
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,3 +815,23 @@ def test_registry_find(dtype, expected):
('datetime64[ns, US/Eastern]', DatetimeTZDtype('ns', 'US/Eastern'))])
def test_pandas_registry_find(dtype, expected):
assert _pandas_registry.find(dtype) == expected


@pytest.mark.parametrize("check", [
is_categorical_dtype,
is_datetime64tz_dtype,
is_period_dtype,
is_datetime64_ns_dtype,
is_datetime64_dtype,
is_interval_dtype,
is_datetime64_any_dtype,
is_string_dtype,
is_bool_dtype,
])
def test_is_dtype_no_warning(check):
data = pd.DataFrame({"A": [1, 2]})
with tm.assert_produces_warning(None):
check(data)

with tm.assert_produces_warning(None):
check(data["A"])
6 changes: 6 additions & 0 deletions pandas/tests/frame/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,3 +1030,9 @@ def test_alignment_non_pandas(self):
align(df, val, 'index')
with pytest.raises(ValueError):
align(df, val, 'columns')

def test_no_warning(self, all_arithmetic_operators):
df = pd.DataFrame({"A": [0., 0.], "B": [0., None]})
b = df['B']
with tm.assert_produces_warning(None):
getattr(df, all_arithmetic_operators)(b, 0)

0 comments on commit b12e5ba

Please sign in to comment.