Skip to content

Commit

Permalink
Make (Indexed)Frame.__init__ require data (and index) (#16430)
Browse files Browse the repository at this point in the history
This PR makes `data` and `Index` required arguments of `Frame` and `IndexedFrame` where relevant so we can gradually move towards ensuring `data` and `index` are valid mapping of columns and a cudf Index respectively

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

Approvers:
  - Vyas Ramasubramani (https://github.com/vyasr)

URL: #16430
  • Loading branch information
mroeschke authored Aug 16, 2024
1 parent 623dfce commit e16c2f2
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 16 deletions.
2 changes: 1 addition & 1 deletion python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ def __init__(
):
if copy is not None:
raise NotImplementedError("copy is not currently implemented.")
super().__init__()
super().__init__({}, index=cudf.Index([]))
if nan_as_null is no_default:
nan_as_null = not cudf.get_option("mode.pandas_compatible")

Expand Down
8 changes: 2 additions & 6 deletions python/cudf/cudf/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,10 @@ class Frame(BinaryOperand, Scannable):
A Frame representing the (optional) index columns.
"""

_data: "ColumnAccessor"

_VALID_BINARY_OPERATIONS = BinaryOperand._SUPPORTED_BINARY_OPERATIONS

def __init__(self, data=None):
if data is None:
data = {}
self._data = cudf.core.column_accessor.ColumnAccessor(data)
def __init__(self, data: ColumnAccessor | MutableMapping[Any, ColumnBase]):
self._data = ColumnAccessor(data)

@property
def _num_columns(self) -> int:
Expand Down
16 changes: 9 additions & 7 deletions python/cudf/cudf/core/indexed_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ class IndexedFrame(Frame):
# mypy can't handle bound type variables as class members
_loc_indexer_type: type[_LocIndexerClass] # type: ignore
_iloc_indexer_type: type[_IlocIndexerClass] # type: ignore
_index: cudf.core.index.BaseIndex
_groupby = GroupBy
_resampler = _Resampler

Expand All @@ -284,18 +283,21 @@ class IndexedFrame(Frame):
"cummax": {"op_name": "cumulative max"},
}

def __init__(self, data=None, index=None):
def __init__(
self,
data: ColumnAccessor | MutableMapping[Any, ColumnBase],
index: BaseIndex,
):
super().__init__(data=data)
# TODO: Right now it is possible to initialize an IndexedFrame without
# an index. The code's correctness relies on the subclass constructors
# assigning the attribute after the fact. We should restructure those
# to ensure that this constructor is always invoked with an index.
if not isinstance(index, cudf.core._base_index.BaseIndex):
raise ValueError(
f"index must be a cudf index not {type(index).__name__}"
)
self._index = index

@property
def _num_rows(self) -> int:
# Important to use the index because the data may be empty.
# TODO: Remove once DataFrame.__init__ is cleaned up
return len(self.index)

@property
Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/core/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ def concat(
elif len(objs) == 1:
obj = objs[0]
result = cudf.DataFrame._from_data(
data=None if join == "inner" else obj._data.copy(deep=True),
data={} if join == "inner" else obj._data.copy(deep=True),
index=cudf.RangeIndex(len(obj))
if ignore_index
else obj.index.copy(deep=True),
Expand Down
2 changes: 1 addition & 1 deletion python/cudf/cudf/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ def from_categorical(cls, categorical, codes=None):

@classmethod
@_performance_tracking
def from_arrow(cls, array: pa.Array):
def from_arrow(cls, array: pa.Array) -> Self:
"""Create from PyArrow Array/ChunkedArray.
Parameters
Expand Down

0 comments on commit e16c2f2

Please sign in to comment.