Skip to content

Commit 6cacdde

Browse files
Dr-IrvTomAugspurger
authored andcommitted
Change _can_hold_na to a class attribute and document that it shouldn't be changed (#20819)
1 parent e9190bf commit 6cacdde

File tree

4 files changed

+15
-21
lines changed

4 files changed

+15
-21
lines changed

pandas/core/arrays/base.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ class ExtensionArray(object):
3838
* copy
3939
* _concat_same_type
4040
41-
Some additional methods are available to satisfy pandas' internal, private
42-
block API:
41+
An additional method is available to satisfy pandas' internal,
42+
private block API.
4343
44-
* _can_hold_na
4544
* _formatting_values
4645
4746
Some methods require casting the ExtensionArray to an ndarray of Python
@@ -399,7 +398,8 @@ def _values_for_factorize(self):
399398
Returns
400399
-------
401400
values : ndarray
402-
An array suitable for factoraization. This should maintain order
401+
402+
An array suitable for factorization. This should maintain order
403403
and be a supported dtype (Float64, Int64, UInt64, String, Object).
404404
By default, the extension array is cast to object dtype.
405405
na_value : object
@@ -422,7 +422,7 @@ def factorize(self, na_sentinel=-1):
422422
Returns
423423
-------
424424
labels : ndarray
425-
An interger NumPy array that's an indexer into the original
425+
An integer NumPy array that's an indexer into the original
426426
ExtensionArray.
427427
uniques : ExtensionArray
428428
An ExtensionArray containing the unique values of `self`.
@@ -566,16 +566,12 @@ def _concat_same_type(cls, to_concat):
566566
"""
567567
raise AbstractMethodError(cls)
568568

569-
@property
570-
def _can_hold_na(self):
571-
# type: () -> bool
572-
"""Whether your array can hold missing values. True by default.
573-
574-
Notes
575-
-----
576-
Setting this to false will optimize some operations like fillna.
577-
"""
578-
return True
569+
# The _can_hold_na attribute is set to True so that pandas internals
570+
# will use the ExtensionDtype.na_value as the NA value in operations
571+
# such as take(), reindex(), shift(), etc. In addition, those results
572+
# will then be of the ExtensionArray subclass rather than an array
573+
# of objects
574+
_can_hold_na = True
579575

580576
@property
581577
def _ndarray_values(self):

pandas/tests/extension/base/interface.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def test_ndim(self, data):
2121
assert data.ndim == 1
2222

2323
def test_can_hold_na_valid(self, data):
24-
assert data._can_hold_na in {True, False}
24+
# GH-20761
25+
assert data._can_hold_na is True
2526

2627
def test_memory_usage(self, data):
2728
s = pd.Series(data)

pandas/tests/extension/base/missing.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99

1010
class BaseMissingTests(BaseExtensionTests):
1111
def test_isna(self, data_missing):
12-
if data_missing._can_hold_na:
13-
expected = np.array([True, False])
14-
else:
15-
expected = np.array([False, False])
12+
expected = np.array([True, False])
1613

1714
result = pd.isna(data_missing)
1815
tm.assert_numpy_array_equal(result, expected)

pandas/tests/extension/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def na_cmp():
5757
Should return a function of two arguments that returns
5858
True if both arguments are (scalar) NA for your type.
5959
60-
By default, uses ``operator.or``
60+
By default, uses ``operator.is_``
6161
"""
6262
return operator.is_
6363

0 commit comments

Comments
 (0)