Skip to content

Commit 4c04e30

Browse files
Support uncertainties as EA Dtype
In parallel with Pint and Pint-Pandas, changes to support uncertainties as an EA Dtype. See hgrecco/pint#1615 and hgrecco/pint-pandas#140 Signed-off-by: Michael Tiemann <[email protected]>
1 parent 1186ee0 commit 4c04e30

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

pandas/core/arrays/masked.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,8 @@ def take(
870870
# we only fill where the indexer is null
871871
# not existing missing values
872872
# TODO(jreback) what if we have a non-na float as a fill value?
873-
if allow_fill and notna(fill_value):
873+
# NaN with uncertainties is scalar but does not register as `isna`, so use fact that NaN != NaN
874+
if allow_fill and notna(fill_value) and fill_value==fill_value:
874875
fill_mask = np.asarray(indexer) == -1
875876
result[fill_mask] = fill_value
876877
mask = mask ^ fill_mask

pandas/core/groupby/groupby.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -3200,7 +3200,10 @@ def first(x: Series):
32003200
"""Helper function for first item that isn't NA."""
32013201
arr = x.array[notna(x.array)]
32023202
if not len(arr):
3203-
return np.nan
3203+
nan_arr = x.array[isna(x.array)]
3204+
if not len(nan_arr):
3205+
return np.nan
3206+
return nan_arr[0]
32043207
return arr[0]
32053208

32063209
if isinstance(obj, DataFrame):

pandas/core/groupby/ops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ def nkeys(self) -> int:
587587

588588
def get_iterator(
589589
self, data: NDFrameT, axis: AxisInt = 0
590-
) -> Iterator[tuple[Hashable, NDFrameT]]:
590+
) -> Iterator[tuple[Hashable, NDFrameT]]: # Doesn't work with non-hashable EA types
591591
"""
592592
Groupby iterator
593593

pandas/tests/extension/test_boolean.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929

3030
def make_data():
31-
return [True, False] * 4 + [np.nan] + [True, False] * 44 + [np.nan] + [True, False]
31+
return [True, False] * 4 + [pd.NA] + [True, False] * 44 + [pd.NA] + [True, False]
3232

3333

3434
@pytest.fixture
@@ -48,7 +48,7 @@ def data_for_twos(dtype):
4848

4949
@pytest.fixture
5050
def data_missing(dtype):
51-
return pd.array([np.nan, True], dtype=dtype)
51+
return pd.array([pd.NA, True], dtype=dtype)
5252

5353

5454
@pytest.fixture
@@ -58,7 +58,7 @@ def data_for_sorting(dtype):
5858

5959
@pytest.fixture
6060
def data_missing_for_sorting(dtype):
61-
return pd.array([True, np.nan, False], dtype=dtype)
61+
return pd.array([True, pd.NA, False], dtype=dtype)
6262

6363

6464
@pytest.fixture
@@ -76,7 +76,7 @@ def na_value():
7676
def data_for_grouping(dtype):
7777
b = True
7878
a = False
79-
na = np.nan
79+
na = pd.NA
8080
return pd.array([b, b, na, na, a, a, b], dtype=dtype)
8181

8282

@@ -147,7 +147,7 @@ def _check_op(self, obj, op, other, op_name, exc=NotImplementedError):
147147
expected = expected.astype("Float64")
148148
if op_name == "__rpow__":
149149
# for rpow, combine does not propagate NaN
150-
expected[result.isna()] = np.nan
150+
expected[result.isna()] = pd.NA
151151
self.assert_equal(result, expected)
152152
else:
153153
with pytest.raises(exc):

0 commit comments

Comments
 (0)