Skip to content

Commit

Permalink
Use cached_property for NumericColumn.nan_count instead of ._nan_coun…
Browse files Browse the repository at this point in the history
…t variable (#15466)

Small cleanup that results in the same functionality

Authors:
  - Matthew Roeschke (https://github.com/mroeschke)

Approvers:
  - GALI PREM SAGAR (https://github.com/galipremsagar)

URL: #15466
  • Loading branch information
mroeschke authored Apr 5, 2024
1 parent 9ae32fe commit 363db50
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions python/cudf/cudf/core/column/numerical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import functools
from typing import (
Any,
Callable,
Expand Down Expand Up @@ -75,7 +76,6 @@ class NumericalColumn(NumericalBaseColumn):
mask : Buffer, optional
"""

_nan_count: Optional[int]
_VALID_BINARY_OPERATIONS = BinaryOperand._SUPPORTED_BINARY_OPERATIONS

def __init__(
Expand All @@ -93,7 +93,6 @@ def __init__(
raise ValueError("Buffer size must be divisible by element size")
if size is None:
size = (data.size // dtype.itemsize) - offset
self._nan_count = None
super().__init__(
data,
size=size,
Expand All @@ -105,7 +104,10 @@ def __init__(

def _clear_cache(self):
super()._clear_cache()
self._nan_count = None
try:
del self.nan_count
except AttributeError:
pass

def __contains__(self, item: ScalarLike) -> bool:
"""
Expand Down Expand Up @@ -424,14 +426,12 @@ def any(self, skipna: bool = True) -> bool:

return libcudf.reduce.reduce("any", result_col, dtype=np.bool_)

@property
@functools.cached_property
def nan_count(self) -> int:
if self.dtype.kind != "f":
self._nan_count = 0
elif self._nan_count is None:
nan_col = libcudf.unary.is_nan(self)
self._nan_count = nan_col.sum()
return self._nan_count
return 0
nan_col = libcudf.unary.is_nan(self)
return nan_col.sum()

def _process_values_for_isin(
self, values: Sequence
Expand Down

0 comments on commit 363db50

Please sign in to comment.