Skip to content

Commit 43073cc

Browse files
Backport PR #50565 on branch 1.5.x (BUG: Fix more interchange Column.size method) (#50610)
Backport PR #50565: BUG: Fix more interchange Column.size method Co-authored-by: Matthew Roeschke <[email protected]>
1 parent af97958 commit 43073cc

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

pandas/core/interchange/dataframe_protocol.py

-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ class Column(ABC):
213213
doesn't need its own version or ``__column__`` protocol.
214214
"""
215215

216-
@property
217216
@abstractmethod
218217
def size(self) -> int:
219218
"""

pandas/core/interchange/from_dataframe.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def primitive_column_to_ndarray(col: Column) -> tuple[np.ndarray, Any]:
155155
buffers = col.get_buffers()
156156

157157
data_buff, data_dtype = buffers["data"]
158-
data = buffer_to_ndarray(data_buff, data_dtype, col.offset, col.size)
158+
data = buffer_to_ndarray(data_buff, data_dtype, col.offset, col.size())
159159

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

189189
codes_buff, codes_dtype = buffers["data"]
190-
codes = buffer_to_ndarray(codes_buff, codes_dtype, col.offset, col.size)
190+
codes = buffer_to_ndarray(codes_buff, codes_dtype, col.offset, col.size())
191191

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

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

259259
null_pos = None
260260
if null_kind in (ColumnNullType.USE_BITMASK, ColumnNullType.USE_BYTEMASK):
261261
assert buffers["validity"], "Validity buffers cannot be empty for masks"
262262
valid_buff, valid_dtype = buffers["validity"]
263-
null_pos = buffer_to_ndarray(valid_buff, valid_dtype, col.offset, col.size)
263+
null_pos = buffer_to_ndarray(valid_buff, valid_dtype, col.offset, col.size())
264264
if sentinel_val == 0:
265265
null_pos = ~null_pos
266266

267267
# Assemble the strings from the code units
268-
str_list: list[None | float | str] = [None] * col.size
269-
for i in range(col.size):
268+
str_list: list[None | float | str] = [None] * col.size()
269+
for i in range(col.size()):
270270
# Check for missing values
271271
if null_pos is not None and null_pos[i]:
272272
str_list[i] = np.nan
@@ -349,7 +349,7 @@ def datetime_column_to_ndarray(col: Column) -> tuple[np.ndarray, Any]:
349349
Endianness.NATIVE,
350350
),
351351
col.offset,
352-
col.size,
352+
col.size(),
353353
)
354354

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

0 commit comments

Comments
 (0)