|
52 | 52 | )
|
53 | 53 | from pandas.core.dtypes.common import (
|
54 | 54 | is_array_like,
|
| 55 | + is_bool, |
55 | 56 | is_bool_dtype,
|
56 | 57 | is_datetime64_any_dtype,
|
57 | 58 | is_datetime64tz_dtype,
|
@@ -181,7 +182,6 @@ def _sparse_array_op(
|
181 | 182 | ltype = SparseDtype(subtype, left.fill_value)
|
182 | 183 | rtype = SparseDtype(subtype, right.fill_value)
|
183 | 184 |
|
184 |
| - # TODO(GH-23092): pass copy=False. Need to fix astype_nansafe |
185 | 185 | left = left.astype(ltype)
|
186 | 186 | right = right.astype(rtype)
|
187 | 187 | dtype = ltype.subtype
|
@@ -701,7 +701,11 @@ def isna(self):
|
701 | 701 | # If null fill value, we want SparseDtype[bool, true]
|
702 | 702 | # to preserve the same memory usage.
|
703 | 703 | dtype = SparseDtype(bool, self._null_fill_value)
|
704 |
| - return type(self)._simple_new(isna(self.sp_values), self.sp_index, dtype) |
| 704 | + if self._null_fill_value: |
| 705 | + return type(self)._simple_new(isna(self.sp_values), self.sp_index, dtype) |
| 706 | + mask = np.full(len(self), False, dtype=np.bool8) |
| 707 | + mask[self.sp_index.indices] = isna(self.sp_values) |
| 708 | + return type(self)(mask, fill_value=False, dtype=dtype) |
705 | 709 |
|
706 | 710 | def fillna(
|
707 | 711 | self: SparseArrayT,
|
@@ -945,13 +949,18 @@ def __getitem__(
|
945 | 949 | )
|
946 | 950 |
|
947 | 951 | else:
|
948 |
| - # TODO: I think we can avoid densifying when masking a |
949 |
| - # boolean SparseArray with another. Need to look at the |
950 |
| - # key's fill_value for True / False, and then do an intersection |
951 |
| - # on the indices of the sp_values. |
952 | 952 | if isinstance(key, SparseArray):
|
953 | 953 | if is_bool_dtype(key):
|
954 |
| - key = key.to_dense() |
| 954 | + if is_bool(key.fill_value): |
| 955 | + msk = np.full( |
| 956 | + shape=len(self), |
| 957 | + fill_value=key.fill_value, |
| 958 | + dtype=np.bool8, |
| 959 | + ) |
| 960 | + msk[key.sp_index.indices] = not key.fill_value |
| 961 | + return self.take(np.arange(len(self), dtype=np.int32)[msk]) |
| 962 | + else: |
| 963 | + key = key.to_dense() |
955 | 964 | else:
|
956 | 965 | key = np.asarray(key)
|
957 | 966 |
|
|
0 commit comments