Skip to content

TYP: Column.null_count is a Python int #47804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2022
Merged
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
10 changes: 5 additions & 5 deletions pandas/core/exchange/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def offset(self) -> int:
return 0

@cache_readonly
def dtype(self):
def dtype(self) -> tuple[DtypeKind, int, str, str]:
dtype = self._col.dtype

if is_categorical_dtype(dtype):
Expand Down Expand Up @@ -138,7 +138,7 @@ def _dtype_from_pandasdtype(self, dtype) -> tuple[DtypeKind, int, str, str]:
# Not a NumPy dtype. Check if it's a categorical maybe
raise ValueError(f"Data type {dtype} not supported by exchange protocol")

return (kind, dtype.itemsize * 8, dtype_to_arrow_c_fmt(dtype), dtype.byteorder)
return kind, dtype.itemsize * 8, dtype_to_arrow_c_fmt(dtype), dtype.byteorder

@property
def describe_categorical(self):
Expand Down Expand Up @@ -181,10 +181,10 @@ def null_count(self) -> int:
"""
Number of null elements. Should always be known.
"""
return self._col.isna().sum()
return self._col.isna().sum().item()

@property
def metadata(self):
def metadata(self) -> dict[str, pd.Index]:
"""
Store specific metadata of the column.
"""
Expand All @@ -196,7 +196,7 @@ def num_chunks(self) -> int:
"""
return 1

def get_chunks(self, n_chunks=None):
def get_chunks(self, n_chunks: int | None = None):
"""
Return an iterator yielding the chunks.
See `DataFrame.get_chunks` for details on ``n_chunks``.
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/exchange/test_spec_conformance.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def test_only_one_dtype(test_data, df_from_dict):

column_size = len(test_data[columns[0]])
for column in columns:
assert dfX.get_column_by_name(column).null_count == 0
null_count = dfX.get_column_by_name(column).null_count
assert null_count == 0
assert isinstance(null_count, int)
assert dfX.get_column_by_name(column).size == column_size
assert dfX.get_column_by_name(column).offset == 0

Expand All @@ -49,6 +51,7 @@ def test_mixed_dtypes(df_from_dict):
for column, kind in columns.items():
colX = dfX.get_column_by_name(column)
assert colX.null_count == 0
assert isinstance(colX.null_count, int)
assert colX.size == 3
assert colX.offset == 0

Expand All @@ -62,6 +65,7 @@ def test_na_float(df_from_dict):
dfX = df.__dataframe__()
colX = dfX.get_column_by_name("a")
assert colX.null_count == 1
assert isinstance(colX.null_count, int)


def test_noncategorical(df_from_dict):
Expand Down