Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 9ccb06e

Browse files
authored
Merge pull request #214 from xhochy/allow-numpy-outputs-where-clear
Allow NumPy ufuncs to work with `np.ndarray` outputs where operations are clearly defined
2 parents 45d999f + f913dd1 commit 9ccb06e

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ Starting with 0.5, we will follow the following versioning scheme:
77
* We bump MINOR on breaking changes.
88
* We increase PATCH otherwise.
99

10+
0.7.2 (2021-01-17)
11+
------------------
12+
13+
* Allow NumPy ufuncs to work with `np.ndarray` outputs where operations are clearly defined (i.e. the fletcher array has no nulls).
14+
1015
0.7.1 (2020-12-29)
1116
------------------
1217

fletcher/base.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ def name(self) -> str:
209209
"""
210210
return str(self)
211211

212+
@property
213+
def itemsize(self) -> int:
214+
return self.arrow_dtype.bit_width
215+
212216
@property
213217
def _is_boolean(self):
214218
return pa.types.is_boolean(self.arrow_dtype)
@@ -547,6 +551,24 @@ def __array_ufunc__(self, ufunc, method: str, *inputs, **kwargs):
547551
)
548552
if len(inputs) != 2:
549553
raise NotImplementedError("Only ufuncs with a second input are supported")
554+
if "out" in kwargs:
555+
out = kwargs.pop("out")
556+
if len(out) == 1:
557+
out_array = out[0]
558+
if isinstance(out_array, np.ndarray) and self.data.null_count == 0:
559+
self_as_np = np.asarray(self)
560+
mapped_inputs = [self_as_np if i is self else i for i in inputs]
561+
return self_as_np.__array_ufunc__(
562+
ufunc, method, *mapped_inputs, **kwargs
563+
)
564+
else:
565+
raise NotImplementedError(
566+
"Currently ufuncs with outputs are only supported for arrays without missings"
567+
)
568+
else:
569+
raise NotImplementedError(
570+
"Currently ufuncs only support a single output"
571+
)
550572
if len(kwargs) > 0:
551573
raise NotImplementedError("ufuncs with kwargs aren't supported")
552574
if isinstance(inputs[0], FletcherBaseArray):

tests/test_pandas_integration.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,15 @@ def test_setitem_chunked_int_index(indices, test_array_chunked):
278278
ser[integer_index] = ["int", "index"]
279279
assert ser[indices[0]] == "int"
280280
assert ser[indices[1]] == "index"
281+
282+
283+
def test_numpy_ufunc_with_keyword(fletcher_array):
284+
# https://github.com/xhochy/fletcher/issues/213
285+
286+
x = fletcher_array([0])
287+
y = np.array([False])
288+
289+
# Test without in-place storage
290+
y | (x == 1)
291+
# Test with in-place storage
292+
y |= x == 1

0 commit comments

Comments
 (0)