Skip to content

TST: nan->NA in non-construction tests #62021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pandas/tests/arrays/categorical/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from pandas import (
NA,
Categorical,
CategoricalDtype,
CategoricalIndex,
Expand Down Expand Up @@ -34,7 +35,7 @@ def test_astype_nan_to_int(self, cls, values):
array([0, 0], dtype="timedelta64[ns]"),
array([Period("2019"), Period("2020")], dtype="period[Y-DEC]"),
array([Interval(0, 1), Interval(1, 2)], dtype="interval"),
array([1, np.nan], dtype="Int64"),
array([1, NA], dtype="Int64"),
],
)
def test_astype_category_to_extension_dtype(self, expected):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/floating/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def data_missing(dtype):
Fixture returning array with missing data according to parametrized float
'dtype'.
"""
return pd.array([np.nan, 0.1], dtype=dtype)
return pd.array([pd.NA, 0.1], dtype=dtype)


@pytest.fixture(params=["data", "data_missing"])
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/arrays/floating/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ def test_pow_array(dtype):
def test_rpow_one_to_na():
# https://github.com/pandas-dev/pandas/issues/22022
# https://github.com/pandas-dev/pandas/issues/29997
arr = pd.array([np.nan, np.nan], dtype="Float64")
arr = pd.array([pd.NA, pd.NA], dtype="Float64")
result = np.array([1.0, 2.0]) ** arr
expected = pd.array([1.0, np.nan], dtype="Float64")
expected = pd.array([1.0, pd.NA], dtype="Float64")
tm.assert_extension_array_equal(result, expected)


Expand Down Expand Up @@ -187,22 +187,22 @@ def test_error_invalid_values(data, all_arithmetic_operators):
def test_cross_type_arithmetic():
df = pd.DataFrame(
{
"A": pd.array([1, 2, np.nan], dtype="Float64"),
"B": pd.array([1, np.nan, 3], dtype="Float32"),
"A": pd.array([1, 2, pd.NA], dtype="Float64"),
"B": pd.array([1, pd.NA, 3], dtype="Float32"),
"C": np.array([1, 2, 3], dtype="float64"),
}
)

result = df.A + df.C
expected = pd.Series([2, 4, np.nan], dtype="Float64")
expected = pd.Series([2, 4, pd.NA], dtype="Float64")
tm.assert_series_equal(result, expected)

result = (df.A + df.C) * 3 == 12
expected = pd.Series([False, True, None], dtype="boolean")
tm.assert_series_equal(result, expected)

result = df.A + df.B
expected = pd.Series([2, np.nan, np.nan], dtype="Float64")
expected = pd.Series([2, pd.NA, pd.NA], dtype="Float64")
tm.assert_series_equal(result, expected)


Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/floating/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_floating_array_constructor():
mask = np.array([False, False, False, True], dtype="bool")

result = FloatingArray(values, mask)
expected = pd.array([1, 2, 3, np.nan], dtype="Float64")
expected = pd.array([1, 2, 3, pd.NA], dtype="Float64")
tm.assert_extension_array_equal(result, expected)
tm.assert_numpy_array_equal(result._data, values)
tm.assert_numpy_array_equal(result._mask, mask)
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/arrays/floating/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# np.sign emits a warning with nans, <https://github.com/numpy/numpy/issues/15127>
@pytest.mark.filterwarnings("ignore:invalid value encountered in sign:RuntimeWarning")
def test_ufuncs_single(ufunc):
a = pd.array([1, 2, -3, np.nan], dtype="Float64")
a = pd.array([1, 2, -3, pd.NA], dtype="Float64")
result = ufunc(a)
expected = pd.array(ufunc(a.astype(float)), dtype="Float64")
tm.assert_extension_array_equal(result, expected)
Expand All @@ -24,7 +24,7 @@ def test_ufuncs_single(ufunc):

@pytest.mark.parametrize("ufunc", [np.log, np.exp, np.sin, np.cos, np.sqrt])
def test_ufuncs_single_float(ufunc):
a = pd.array([1.0, 0.2, 3.0, np.nan], dtype="Float64")
a = pd.array([1.0, 0.2, 3.0, pd.NA], dtype="Float64")
with np.errstate(invalid="ignore"):
result = ufunc(a)
expected = pd.array(ufunc(a.astype(float)), dtype="Float64")
Expand All @@ -40,7 +40,7 @@ def test_ufuncs_single_float(ufunc):
@pytest.mark.parametrize("ufunc", [np.add, np.subtract])
def test_ufuncs_binary_float(ufunc):
# two FloatingArrays
a = pd.array([1, 0.2, -3, np.nan], dtype="Float64")
a = pd.array([1, 0.2, -3, pd.NA], dtype="Float64")
result = ufunc(a, a)
expected = pd.array(ufunc(a.astype(float), a.astype(float)), dtype="Float64")
tm.assert_extension_array_equal(result, expected)
Expand Down Expand Up @@ -88,7 +88,7 @@ def test_ufunc_reduce_raises(values):
],
)
def test_stat_method(pandasmethname, kwargs):
s = pd.Series(data=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, np.nan, np.nan], dtype="Float64")
s = pd.Series(data=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, pd.NA, pd.NA], dtype="Float64")
pandasmeth = getattr(s, pandasmethname)
result = pandasmeth(**kwargs)
s2 = pd.Series(data=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6], dtype="float64")
Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/arrays/integer/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import numpy as np
import pytest

import pandas as pd
Expand Down Expand Up @@ -40,7 +39,7 @@ def data(dtype):
Used to test dtype conversion with and without missing values.
"""
return pd.array(
list(range(8)) + [np.nan] + list(range(10, 98)) + [np.nan] + [99, 100],
list(range(8)) + [pd.NA] + list(range(10, 98)) + [pd.NA] + [99, 100],
dtype=dtype,
)

Expand All @@ -53,7 +52,7 @@ def data_missing(dtype):

Used to test dtype conversion with and without missing values.
"""
return pd.array([np.nan, 1], dtype=dtype)
return pd.array([pd.NA, 1], dtype=dtype)


@pytest.fixture(params=["data", "data_missing"])
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/arrays/integer/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ def test_pow_array():
def test_rpow_one_to_na():
# https://github.com/pandas-dev/pandas/issues/22022
# https://github.com/pandas-dev/pandas/issues/29997
arr = pd.array([np.nan, np.nan], dtype="Int64")
arr = pd.array([pd.NA, pd.NA], dtype="Int64")
result = np.array([1.0, 2.0]) ** arr
expected = pd.array([1.0, np.nan], dtype="Float64")
expected = pd.array([1.0, pd.NA], dtype="Float64")
tm.assert_extension_array_equal(result, expected)


Expand Down Expand Up @@ -243,22 +243,22 @@ def test_arithmetic_conversion(all_arithmetic_operators, other):
def test_cross_type_arithmetic():
df = pd.DataFrame(
{
"A": pd.Series([1, 2, np.nan], dtype="Int64"),
"B": pd.Series([1, np.nan, 3], dtype="UInt8"),
"A": pd.Series([1, 2, pd.NA], dtype="Int64"),
"B": pd.Series([1, pd.NA, 3], dtype="UInt8"),
"C": [1, 2, 3],
}
)

result = df.A + df.C
expected = pd.Series([2, 4, np.nan], dtype="Int64")
expected = pd.Series([2, 4, pd.NA], dtype="Int64")
tm.assert_series_equal(result, expected)

result = (df.A + df.C) * 3 == 12
expected = pd.Series([False, True, None], dtype="boolean")
tm.assert_series_equal(result, expected)

result = df.A + df.B
expected = pd.Series([2, np.nan, np.nan], dtype="Int64")
expected = pd.Series([2, pd.NA, pd.NA], dtype="Int64")
tm.assert_series_equal(result, expected)


Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/arrays/integer/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_integer_array_constructor():
mask = np.array([False, False, False, True], dtype="bool")

result = IntegerArray(values, mask)
expected = pd.array([1, 2, 3, np.nan], dtype="Int64")
expected = pd.array([1, 2, 3, pd.NA], dtype="Int64")
tm.assert_extension_array_equal(result, expected)

msg = r".* should be .* numpy array. Use the 'pd.array' function instead"
Expand Down Expand Up @@ -191,7 +191,7 @@ def test_to_integer_array_float():

def test_to_integer_array_str():
result = IntegerArray._from_sequence(["1", "2", None], dtype="Int64")
expected = pd.array([1, 2, np.nan], dtype="Int64")
expected = pd.array([1, 2, pd.NA], dtype="Int64")
tm.assert_extension_array_equal(result, expected)

with pytest.raises(
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/arrays/integer/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_preserve_dtypes(op):

def test_astype_nansafe():
# see gh-22343
arr = pd.array([np.nan, 1, 2], dtype="Int8")
arr = pd.array([pd.NA, 1, 2], dtype="Int8")
msg = "cannot convert NA to integer"

with pytest.raises(ValueError, match=msg):
Expand Down Expand Up @@ -230,7 +230,7 @@ def test_construct_cast_invalid(dtype):
with pytest.raises(TypeError, match=msg):
pd.Series(arr).astype(dtype)

arr = [1.2, 2.3, 3.7, np.nan]
arr = [1.2, 2.3, 3.7, pd.NA]
with pytest.raises(TypeError, match=msg):
pd.array(arr, dtype=dtype)

Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/arrays/integer/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# np.sign emits a warning with nans, <https://github.com/numpy/numpy/issues/15127>
@pytest.mark.filterwarnings("ignore:invalid value encountered in sign:RuntimeWarning")
def test_ufuncs_single_int(ufunc):
a = pd.array([1, 2, -3, np.nan])
a = pd.array([1, 2, -3, pd.NA], dtype="Int64")
result = ufunc(a)
expected = pd.array(ufunc(a.astype(float)), dtype="Int64")
tm.assert_extension_array_equal(result, expected)
Expand All @@ -23,7 +23,7 @@ def test_ufuncs_single_int(ufunc):

@pytest.mark.parametrize("ufunc", [np.log, np.exp, np.sin, np.cos, np.sqrt])
def test_ufuncs_single_float(ufunc):
a = pd.array([1, 2, -3, np.nan])
a = pd.array([1, 2, -3, pd.NA], dtype="Int64")
with np.errstate(invalid="ignore"):
result = ufunc(a)
expected = FloatingArray(ufunc(a.astype(float)), mask=a._mask)
Expand All @@ -39,7 +39,7 @@ def test_ufuncs_single_float(ufunc):
@pytest.mark.parametrize("ufunc", [np.add, np.subtract])
def test_ufuncs_binary_int(ufunc):
# two IntegerArrays
a = pd.array([1, 2, -3, np.nan])
a = pd.array([1, 2, -3, pd.NA], dtype="Int64")
result = ufunc(a, a)
expected = pd.array(ufunc(a.astype(float), a.astype(float)), dtype="Int64")
tm.assert_extension_array_equal(result, expected)
Expand Down Expand Up @@ -99,7 +99,7 @@ def test_ufunc_reduce_raises(values):
],
)
def test_stat_method(pandasmethname, kwargs):
s = pd.Series(data=[1, 2, 3, 4, 5, 6, np.nan, np.nan], dtype="Int64")
s = pd.Series(data=[1, 2, 3, 4, 5, 6, pd.NA, pd.NA], dtype="Int64")
pandasmeth = getattr(s, pandasmethname)
result = pandasmeth(**kwargs)
s2 = pd.Series(data=[1, 2, 3, 4, 5, 6], dtype="Int64")
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def test_array_copy():
([1, 2], IntegerArray._from_sequence([1, 2], dtype="Int64")),
([1, None], IntegerArray._from_sequence([1, None], dtype="Int64")),
([1, pd.NA], IntegerArray._from_sequence([1, pd.NA], dtype="Int64")),
([1, np.nan], IntegerArray._from_sequence([1, np.nan], dtype="Int64")),
([1, np.nan], IntegerArray._from_sequence([1, pd.NA], dtype="Int64")),
# float
([0.1, 0.2], FloatingArray._from_sequence([0.1, 0.2], dtype="Float64")),
([0.1, None], FloatingArray._from_sequence([0.1, pd.NA], dtype="Float64")),
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/base/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def test_numpy_array_all_dtypes(any_numpy_dtype):
[
(pd.Categorical(["a", "b"]), "_codes"),
(PeriodArray._from_sequence(["2000", "2001"], dtype="period[D]"), "_ndarray"),
(pd.array([0, np.nan], dtype="Int64"), "_data"),
(pd.array([0, pd.NA], dtype="Int64"), "_data"),
(IntervalArray.from_breaks([0, 1]), "_left"),
(SparseArray([0, 1]), "_sparse_values"),
(
Expand Down Expand Up @@ -305,7 +305,7 @@ def test_array_multiindex_raises():
np.array([pd.Period("2000", freq="D"), pd.Period("2001", freq="D")]),
False,
),
(pd.array([0, np.nan], dtype="Int64"), np.array([0, np.nan]), False),
(pd.array([0, pd.NA], dtype="Int64"), np.array([0, np.nan]), False),
(
IntervalArray.from_breaks([0, 1, 2]),
np.array([pd.Interval(0, 1), pd.Interval(1, 2)], dtype=object),
Expand Down
7 changes: 5 additions & 2 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2868,7 +2868,7 @@ def test_dt_components():
)
result = ser.dt.components
expected = pd.DataFrame(
[[1, 0, 0, 2, 0, 3, 4], [None, None, None, None, None, None, None]],
[[1, 0, 0, 2, 0, 3, 4], [pd.NA, pd.NA, pd.NA, pd.NA, pd.NA, pd.NA, pd.NA]],
columns=[
"days",
"hours",
Expand All @@ -2893,7 +2893,10 @@ def test_dt_components_large_values():
)
result = ser.dt.components
expected = pd.DataFrame(
[[365, 23, 59, 59, 999, 0, 0], [None, None, None, None, None, None, None]],
[
[365, 23, 59, 59, 999, 0, 0],
[pd.NA, pd.NA, pd.NA, pd.NA, pd.NA, pd.NA, pd.NA],
],
columns=[
"days",
"hours",
Expand Down
37 changes: 12 additions & 25 deletions pandas/tests/extension/test_masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,32 +380,19 @@ def check_accumulate(self, ser: pd.Series, op_name: str, skipna: bool):
)

if op_name == "cumsum":
result = getattr(ser, op_name)(skipna=skipna)
expected = pd.Series(
pd.array(
getattr(ser.astype("float64"), op_name)(skipna=skipna),
dtype=expected_dtype,
)
)
tm.assert_series_equal(result, expected)
pass
elif op_name in ["cummax", "cummin"]:
result = getattr(ser, op_name)(skipna=skipna)
expected = pd.Series(
pd.array(
getattr(ser.astype("float64"), op_name)(skipna=skipna),
dtype=ser.dtype,
)
)
tm.assert_series_equal(result, expected)
expected_dtype = ser.dtype # type: ignore[assignment]
elif op_name == "cumprod":
result = getattr(ser[:12], op_name)(skipna=skipna)
expected = pd.Series(
pd.array(
getattr(ser[:12].astype("float64"), op_name)(skipna=skipna),
dtype=expected_dtype,
)
)
tm.assert_series_equal(result, expected)

ser = ser[:12]
else:
raise NotImplementedError(f"{op_name} not supported")

result = getattr(ser, op_name)(skipna=skipna)
expected = pd.Series(
pd.array(
getattr(ser.astype("float64"), op_name)(skipna=skipna),
dtype=expected_dtype,
)
)
tm.assert_series_equal(result, expected)
22 changes: 11 additions & 11 deletions pandas/tests/frame/methods/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ def test_diff_sparse(self):
0,
DataFrame(
{
"a": [np.nan, 0, 1, 0, np.nan, np.nan, np.nan, 0],
"b": [np.nan, 1, np.nan, np.nan, -2, 1, np.nan, np.nan],
"c": np.repeat(np.nan, 8),
"d": [np.nan, 3, 5, 7, 9, 11, 13, 15],
"a": [pd.NA, 0, 1, 0, pd.NA, pd.NA, pd.NA, 0],
"b": [pd.NA, 1, pd.NA, pd.NA, -2, 1, pd.NA, pd.NA],
"c": np.repeat(pd.NA, 8), # type: ignore[call-overload]
"d": [pd.NA, 3, 5, 7, 9, 11, 13, 15],
},
dtype="Int64",
),
Expand All @@ -261,10 +261,10 @@ def test_diff_sparse(self):
1,
DataFrame(
{
"a": np.repeat(np.nan, 8),
"b": [0, 1, np.nan, 1, np.nan, np.nan, np.nan, 0],
"c": np.repeat(np.nan, 8),
"d": np.repeat(np.nan, 8),
"a": np.repeat(pd.NA, 8), # type: ignore[call-overload]
"b": [0, 1, pd.NA, 1, pd.NA, pd.NA, pd.NA, 0],
"c": np.repeat(pd.NA, 8), # type: ignore[call-overload]
"d": np.repeat(pd.NA, 8), # type: ignore[call-overload]
},
dtype="Int64",
),
Expand All @@ -275,9 +275,9 @@ def test_diff_integer_na(self, axis, expected):
# GH#24171 IntegerNA Support for DataFrame.diff()
df = DataFrame(
{
"a": np.repeat([0, 1, np.nan, 2], 2),
"b": np.tile([0, 1, np.nan, 2], 2),
"c": np.repeat(np.nan, 8),
"a": np.repeat([0, 1, pd.NA, 2], 2),
"b": np.tile([0, 1, pd.NA, 2], 2),
"c": np.repeat(pd.NA, 8),
"d": np.arange(1, 9) ** 2,
},
dtype="Int64",
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/frame/methods/test_get_numeric_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ def test_get_numeric_data_extension_dtype(self):
# GH#22290
df = DataFrame(
{
"A": pd.array([-10, np.nan, 0, 10, 20, 30], dtype="Int64"),
"A": pd.array([-10, pd.NA, 0, 10, 20, 30], dtype="Int64"),
"B": Categorical(list("abcabc")),
"C": pd.array([0, 1, 2, 3, np.nan, 5], dtype="UInt8"),
"C": pd.array([0, 1, 2, 3, pd.NA, 5], dtype="UInt8"),
"D": IntervalArray.from_breaks(range(7)),
}
)
Expand Down
Loading
Loading