Skip to content

BUG: Fix more interchange Column.size method #50565

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 5 commits into from
Jan 6, 2023
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
3 changes: 1 addition & 2 deletions pandas/core/interchange/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,10 @@ def __init__(self, column: pd.Series, allow_copy: bool = True) -> None:
self._col = column
self._allow_copy = allow_copy

def size(self) -> int: # type: ignore[override]
def size(self) -> int:
"""
Size of the column, in elements.
"""
# error: Signature of "size" incompatible with supertype "Column" [override]
return self._col.size

@property
Expand Down
1 change: 0 additions & 1 deletion pandas/core/interchange/dataframe_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ class Column(ABC):
doesn't need its own version or ``__column__`` protocol.
"""

@property
@abstractmethod
def size(self) -> int:
"""
Expand Down
22 changes: 11 additions & 11 deletions pandas/core/interchange/from_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def primitive_column_to_ndarray(col: Column) -> tuple[np.ndarray, Any]:
buffers = col.get_buffers()

data_buff, data_dtype = buffers["data"]
data = buffer_to_ndarray(data_buff, data_dtype, col.offset, col.size)
data = buffer_to_ndarray(data_buff, data_dtype, col.offset, col.size())

data = set_nulls(data, col, buffers["validity"])
return data, buffers
Expand Down Expand Up @@ -187,7 +187,7 @@ def categorical_column_to_series(col: Column) -> tuple[pd.Series, Any]:
buffers = col.get_buffers()

codes_buff, codes_dtype = buffers["data"]
codes = buffer_to_ndarray(codes_buff, codes_dtype, col.offset, col.size)
codes = buffer_to_ndarray(codes_buff, codes_dtype, col.offset, col.size())

# Doing module in order to not get ``IndexError`` for
# out-of-bounds sentinel values in `codes`
Expand Down Expand Up @@ -244,29 +244,29 @@ def string_column_to_ndarray(col: Column) -> tuple[np.ndarray, Any]:
Endianness.NATIVE,
)
# Specify zero offset as we don't want to chunk the string data
data = buffer_to_ndarray(data_buff, data_dtype, offset=0, length=col.size)
data = buffer_to_ndarray(data_buff, data_dtype, offset=0, length=col.size())

# Retrieve the offsets buffer containing the index offsets demarcating
# the beginning and the ending of each string
offset_buff, offset_dtype = buffers["offsets"]
# Offsets buffer contains start-stop positions of strings in the data buffer,
# meaning that it has more elements than in the data buffer, do `col.size + 1` here
# to pass a proper offsets buffer size
# meaning that it has more elements than in the data buffer, do `col.size() + 1`
# here to pass a proper offsets buffer size
offsets = buffer_to_ndarray(
offset_buff, offset_dtype, col.offset, length=col.size + 1
offset_buff, offset_dtype, col.offset, length=col.size() + 1
)

null_pos = None
if null_kind in (ColumnNullType.USE_BITMASK, ColumnNullType.USE_BYTEMASK):
assert buffers["validity"], "Validity buffers cannot be empty for masks"
valid_buff, valid_dtype = buffers["validity"]
null_pos = buffer_to_ndarray(valid_buff, valid_dtype, col.offset, col.size)
null_pos = buffer_to_ndarray(valid_buff, valid_dtype, col.offset, col.size())
if sentinel_val == 0:
null_pos = ~null_pos

# Assemble the strings from the code units
str_list: list[None | float | str] = [None] * col.size
for i in range(col.size):
str_list: list[None | float | str] = [None] * col.size()
for i in range(col.size()):
# Check for missing values
if null_pos is not None and null_pos[i]:
str_list[i] = np.nan
Expand Down Expand Up @@ -349,7 +349,7 @@ def datetime_column_to_ndarray(col: Column) -> tuple[np.ndarray, Any]:
Endianness.NATIVE,
),
col.offset,
col.size,
col.size(),
)

data = parse_datetime_format_str(format_str, data)
Expand Down Expand Up @@ -501,7 +501,7 @@ def set_nulls(
elif null_kind in (ColumnNullType.USE_BITMASK, ColumnNullType.USE_BYTEMASK):
assert validity, "Expected to have a validity buffer for the mask"
valid_buff, valid_dtype = validity
null_pos = buffer_to_ndarray(valid_buff, valid_dtype, col.offset, col.size)
null_pos = buffer_to_ndarray(valid_buff, valid_dtype, col.offset, col.size())
if sentinel_val == 0:
null_pos = ~null_pos
elif null_kind in (ColumnNullType.NON_NULLABLE, ColumnNullType.USE_NAN):
Expand Down