Skip to content

Commit e83a8a9

Browse files
committed
update
1 parent bf12ba8 commit e83a8a9

File tree

6 files changed

+27
-18
lines changed

6 files changed

+27
-18
lines changed

pandas/_libs/lib.pyx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ cdef extern from "src/parse_helper.h":
4949
int floatify(object, float64_t *result, int *maybe_int) except -1
5050

5151
cimport pandas._libs.util as util
52-
from pandas.core.na_scalar import NA
5352
from pandas._libs.util cimport is_nan, UINT64_MAX, INT64_MAX, INT64_MIN
5453

5554
from pandas._libs.tslib import array_to_datetime
@@ -156,7 +155,6 @@ def is_scalar(val: object) -> bool:
156155

157156
return (cnp.PyArray_IsAnyScalar(val)
158157
# PyArray_IsAnyScalar is always False for bytearrays on Py3
159-
or val is NA
160158
or PyDate_Check(val)
161159
or PyDelta_Check(val)
162160
or PyTime_Check(val)
@@ -1502,7 +1500,7 @@ cdef class Validator:
15021500
f'must define is_value_typed')
15031501

15041502
cdef bint is_valid_null(self, object value) except -1:
1505-
return value is None or value is NA or util.is_nan(value)
1503+
return value is None or value is C_NA or util.is_nan(value)
15061504

15071505
cdef bint is_array_typed(self) except -1:
15081506
return False

pandas/_libs/testing.pyx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,15 @@ cpdef assert_almost_equal(a, b,
180180
# classes can't be the same, to raise error
181181
assert_class_equal(a, b, obj=obj)
182182

183-
if a == b:
184-
# object comparison
185-
return True
186183
if isna(a) and isna(b):
187184
# TODO: Should require same-dtype NA?
188185
# nan / None comparison
189186
return True
187+
188+
if a == b:
189+
# object comparison
190+
return True
191+
190192
if is_comparable_as_number(a) and is_comparable_as_number(b):
191193
if array_equivalent(a, b, strict_nan=True):
192194
# inf comparison

pandas/_libs/tslibs/nattype.pyx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ cimport numpy as cnp
1212
from numpy cimport int64_t
1313
cnp.import_array()
1414

15-
from pandas.core.na_scalar import NA
1615
from pandas._libs.tslibs.np_datetime cimport (
1716
get_datetime64_value, get_timedelta64_value)
1817
cimport pandas._libs.tslibs.util as util
@@ -800,8 +799,6 @@ cpdef bint is_null_datetimelike(object val, bint inat_is_null=True):
800799
"""
801800
if val is None:
802801
return True
803-
elif val is NA:
804-
return True
805802
elif val is c_NaT:
806803
return True
807804
elif util.is_float_object(val) or util.is_complex_object(val):

pandas/core/arrays/string_.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import numpy as np
55

6-
from pandas._libs import lib
6+
from pandas._libs import lib, missing as libmissing
77

88
from pandas.core.dtypes.base import ExtensionDtype
99
from pandas.core.dtypes.common import is_float_dtype, pandas_dtype
@@ -16,7 +16,6 @@
1616
from pandas.core.arrays import PandasArray
1717
from pandas.core.construction import extract_array
1818
from pandas.core.missing import isna
19-
from pandas.core.na_scalar import NA
2019

2120

2221
@register_extension_dtype
@@ -49,7 +48,7 @@ class StringDtype(ExtensionDtype):
4948
"""
5049

5150
#: StringDtype.na_value uses pandas.NA
52-
na_value = NA
51+
na_value = libmissing.NA
5352

5453
@property
5554
def type(self) -> Type:
@@ -202,13 +201,16 @@ def __setitem__(self, key, value):
202201
# validate new items
203202
if scalar_value:
204203
if (
205-
value is NA
204+
value is libmissing.NA
206205
or value is None
207-
or is_float_dtype(value)
208-
and np.isnan(value)
206+
or (
207+
is_float_dtype(value)
208+
and not isinstance(value, str)
209+
and np.isnan(value)
210+
)
209211
):
210212
value = StringDtype.na_value
211-
elif not (isinstance(value, str) or (value is NA)):
213+
elif not (isinstance(value, str) or (value is libmissing.NA)):
212214
raise ValueError(
213215
"Cannot set non-string value '{}' into a StringArray.".format(value)
214216
)

pandas/core/dtypes/missing.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import pandas._libs.missing as libmissing
1010
from pandas._libs.tslibs import NaT, iNaT
1111

12-
from ..na_scalar import NA
1312
from .common import (
1413
_NS_DTYPE,
1514
_TD_DTYPE,
@@ -441,7 +440,7 @@ def array_equivalent(left, right, strict_nan=False):
441440
if left_value is NaT and right_value is not NaT:
442441
return False
443442

444-
elif left_value is NA and right_value is not NA:
443+
elif left_value is libmissing.NA and right_value is not libmissing.NA:
445444
return False
446445

447446
elif isinstance(left_value, float) and np.isnan(left_value):
@@ -455,6 +454,8 @@ def array_equivalent(left, right, strict_nan=False):
455454
if "Cannot compare tz-naive" in str(err):
456455
# tzawareness compat failure, see GH#28507
457456
return False
457+
elif "boolean value of NA is ambiguous" in str(err):
458+
return False
458459
raise
459460
return True
460461

pandas/tests/arrays/string_/test_string.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ def test_setitem_validates():
2424
a[:] = np.array([1, 2])
2525

2626

27+
def test_setitem_with_scalar_string():
28+
# is_float_dtype considers some strings, like 'd', to be floats
29+
# which can cause issues.
30+
arr = pd.array(["a", "c"], dtype="string")
31+
arr[0] = "d"
32+
expected = pd.array(["d", "c"], dtype="string")
33+
tm.assert_extension_array_equal(arr, expected)
34+
35+
2736
@pytest.mark.parametrize(
2837
"input, method",
2938
[

0 commit comments

Comments
 (0)