Skip to content

Commit 201f877

Browse files
committed
Fix some warnings
1 parent 4e5fdb7 commit 201f877

File tree

7 files changed

+39
-17
lines changed

7 files changed

+39
-17
lines changed

pandas/compat/numpy/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
np_version_under1p21 = _nlv < Version("1.21")
1010
np_version_under1p22 = _nlv < Version("1.22")
1111
np_version_gte1p22 = _nlv >= Version("1.22")
12+
np_version_gte1p24 = _nlv >= Version("1.24")
1213
is_numpy_dev = _nlv.dev is not None
1314
_min_numpy_ver = "1.20.3"
1415

pandas/core/arrays/masked.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,14 @@ def to_numpy(
428428
"for this dtype."
429429
)
430430
# don't pass copy to astype -> always need a copy since we are mutating
431-
data = self._data.astype(dtype)
431+
with warnings.catch_warnings():
432+
warnings.filterwarnings("ignore")
433+
data = self._data.astype(dtype)
432434
data[self._mask] = na_value
433435
else:
434-
data = self._data.astype(dtype, copy=copy)
436+
with warnings.catch_warnings():
437+
warnings.filterwarnings("ignore")
438+
data = self._data.astype(dtype, copy=copy)
435439
return data
436440

437441
@doc(ExtensionArray.tolist)
@@ -464,7 +468,10 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
464468
# if we are astyping to another nullable masked dtype, we can fastpath
465469
if isinstance(dtype, BaseMaskedDtype):
466470
# TODO deal with NaNs for FloatingArray case
467-
data = self._data.astype(dtype.numpy_dtype, copy=copy)
471+
with warnings.catch_warnings():
472+
warnings.filterwarnings("ignore")
473+
# TODO: Is rounding what we want long term?
474+
data = self._data.astype(dtype.numpy_dtype, copy=copy)
468475
# mask is copied depending on whether the data was copied, and
469476
# not directly depending on the `copy` keyword
470477
mask = self._mask if data is self._data else self._mask.copy()

pandas/core/dtypes/astype.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
TYPE_CHECKING,
1010
overload,
1111
)
12+
import warnings
1213

1314
import numpy as np
1415

@@ -153,7 +154,9 @@ def _astype_float_to_int_nansafe(
153154
# GH#45151
154155
if not (values >= 0).all():
155156
raise ValueError(f"Cannot losslessly cast from {values.dtype} to {dtype}")
156-
return values.astype(dtype, copy=copy)
157+
with warnings.catch_warnings():
158+
warnings.filterwarnings("ignore")
159+
return values.astype(dtype, copy=copy)
157160

158161

159162
def astype_array(values: ArrayLike, dtype: DtypeObj, copy: bool = False) -> ArrayLike:

pandas/core/dtypes/cast.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1606,7 +1606,9 @@ def maybe_cast_to_integer_array(arr: list | np.ndarray, dtype: np.dtype) -> np.n
16061606
)
16071607
casted = np.array(arr, dtype=dtype, copy=False)
16081608
else:
1609-
casted = arr.astype(dtype, copy=False)
1609+
with warnings.catch_warnings():
1610+
warnings.filterwarnings("ignore")
1611+
casted = arr.astype(dtype, copy=False)
16101612
except OverflowError as err:
16111613
raise OverflowError(
16121614
"The elements provided in the data cannot all be "

pandas/core/indexes/base.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -3874,9 +3874,15 @@ def _get_fill_indexer(
38743874
# but that doesn't appear to be enforced
38753875
# error: "IndexEngine" has no attribute "get_indexer_with_fill"
38763876
engine = self._engine
3877-
return engine.get_indexer_with_fill( # type: ignore[union-attr]
3878-
target=target._values, values=self._values, method=method, limit=limit
3879-
)
3877+
with warnings.catch_warnings():
3878+
# TODO: We need to fix this. Casting to int64 in cython
3879+
warnings.filterwarnings("ignore")
3880+
return engine.get_indexer_with_fill( # type: ignore[union-attr]
3881+
target=target._values,
3882+
values=self._values,
3883+
method=method,
3884+
limit=limit,
3885+
)
38803886

38813887
if self.is_monotonic_increasing and target.is_monotonic_increasing:
38823888
target_values = target._get_engine_target()

pandas/tests/io/parser/test_c_parser_only.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
IS64,
2222
is_ci_environment,
2323
)
24+
from pandas.compat.numpy import np_version_gte1p24
2425
from pandas.errors import ParserError
2526
import pandas.util._test_decorators as td
2627

@@ -114,14 +115,16 @@ def test_dtype_and_names_error(c_parser_only):
114115
3.0 3
115116
"""
116117
# fallback casting, but not castable
118+
warning = RuntimeWarning if np_version_gte1p24 else None
117119
with pytest.raises(ValueError, match="cannot safely convert"):
118-
parser.read_csv(
119-
StringIO(data),
120-
sep=r"\s+",
121-
header=None,
122-
names=["a", "b"],
123-
dtype={"a": np.int32},
124-
)
120+
with tm.assert_produces_warning(warning, check_stacklevel=False):
121+
parser.read_csv(
122+
StringIO(data),
123+
sep=r"\s+",
124+
header=None,
125+
names=["a", "b"],
126+
dtype={"a": np.int32},
127+
)
125128

126129

127130
@pytest.mark.parametrize(

pandas/tests/series/test_constructors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ def test_constructor_floating_data_int_dtype(self, frame_or_series):
798798
# Long-standing behavior (for Series, new in 2.0 for DataFrame)
799799
# has been to ignore the dtype on these;
800800
# not clear if this is what we want long-term
801-
expected = frame_or_series(arr)
801+
# expected = frame_or_series(arr)
802802

803803
# GH#49599 as of 2.0 we raise instead of silently retaining float dtype
804804
msg = "Trying to coerce float values to integer"
@@ -810,7 +810,7 @@ def test_constructor_floating_data_int_dtype(self, frame_or_series):
810810

811811
# pre-2.0, when we had NaNs, we silently ignored the integer dtype
812812
arr[0] = np.nan
813-
expected = frame_or_series(arr)
813+
# expected = frame_or_series(arr)
814814

815815
msg = r"Cannot convert non-finite values \(NA or inf\) to integer"
816816
with pytest.raises(IntCastingNaNError, match=msg):

0 commit comments

Comments
 (0)