Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions cpp/src/arrow/compute/kernels/base_arithmetic_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -834,11 +834,10 @@ template <>
struct Identity<Max> {
template <typename Value>
static constexpr Value value() {
// Note that `min()` returns the smallest positive value for
// floating-point types, and `lowest()` doesn't satisfy the identity
// property for -inf inputs, so use -infinity for those types.
// Max returns the other operand when one is NaN, so NaN is the identity
// for floating-point types. -infinity is not: Max(-inf, NaN) is -inf.
if constexpr (std::is_floating_point_v<Value> || std::is_same_v<Float16, Value>) {
return -std::numeric_limits<Value>::infinity();
return std::numeric_limits<Value>::quiet_NaN();
} else {
return std::numeric_limits<Value>::lowest();
}
Expand All @@ -849,9 +848,9 @@ template <>
struct Identity<Min> {
template <typename Value>
static constexpr Value value() {
// Mirror of Identity<Max>: use +infinity for floating-point types.
// Mirror of Identity<Max>: use NaN for floating-point types.
if constexpr (std::is_floating_point_v<Value> || std::is_same_v<Float16, Value>) {
return std::numeric_limits<Value>::infinity();
return std::numeric_limits<Value>::quiet_NaN();
} else {
return std::numeric_limits<Value>::max();
}
Expand Down
15 changes: 15 additions & 0 deletions cpp/src/arrow/compute/kernels/vector_cumulative_ops_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,21 @@ TEST(TestCumulative, NaN) {
CheckVectorUnary("cumulative_min", ArrayFromJSON(float64(), "[5, 4, NaN, 2, 1]"),
ArrayFromJSON(float64(), "[5, 4, 4, 2, 1]"));

// GH-55769: leading NaNs must not be replaced by the default start
for (const auto& ty : FloatingPointTypes()) {
CheckVectorUnary("cumulative_max", ArrayFromJSON(ty, "[NaN, NaN, 1, NaN, 2]"),
ArrayFromJSON(ty, "[NaN, NaN, 1, 1, 2]"));
CheckVectorUnary("cumulative_min", ArrayFromJSON(ty, "[NaN, NaN, 2, NaN, 1]"),
ArrayFromJSON(ty, "[NaN, NaN, 2, 2, 1]"));

// An explicit start is a real value and still applies from the first element
CumulativeOptions options(0.0);
CheckVectorUnary("cumulative_max", ArrayFromJSON(ty, "[NaN, -1, 1]"),
ArrayFromJSON(ty, "[0, 0, 1]"), &options);
CheckVectorUnary("cumulative_min", ArrayFromJSON(ty, "[NaN, 1, -1]"),
ArrayFromJSON(ty, "[0, 0, -1]"), &options);
}

// mean with NaN is always Nan
CheckVectorUnary("cumulative_mean", ArrayFromJSON(float64(), "[5, 4, NaN, 2, 1]"),
ArrayFromJSON(float64(), "[5, 4.5, NaN, NaN, NaN]"));
Expand Down
11 changes: 11 additions & 0 deletions python/pyarrow/tests/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -3746,6 +3746,17 @@ def test_cumulative_max_min_negative_default_start():
assert pc.cumulative_min(arr).to_pylist() == [np.inf, 2.5]


@pytest.mark.numpy
@pytest.mark.parametrize('ty', [pa.float32(), pa.float64()])
def test_cumulative_max_min_leading_nan(ty):
# GH-55769: leading NaNs were replaced by the default start (-inf / inf)
arr = pa.chunked_array([[np.nan], [np.nan, 1.0, np.nan, 2.0]], type=ty)
np.testing.assert_array_equal(pc.cumulative_max(arr),
[np.nan, np.nan, 1.0, 1.0, 2.0])
np.testing.assert_array_equal(pc.cumulative_min(arr),
[np.nan, np.nan, 1.0, 1.0, 1.0])


@pytest.mark.numpy
@pytest.mark.parametrize('start', (0.5, 3.5, 6.5))
@pytest.mark.parametrize('skip_nulls', (True, False))
Expand Down
Loading