Skip to content

Commit a560754

Browse files
bashtageKevin Sheppard
and
Kevin Sheppard
authored
ENH: add from_dataframe and DataFrame (#331)
* ENH: add from_dataframe and DataFrame * ENH: Add __dataframe__ Co-authored-by: Kevin Sheppard <[email protected]>
1 parent f54d5ed commit a560754

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from pandas.core.interchange.dataframe_protocol import DataFrame as DataFrame
2+
from pandas.core.interchange.from_dataframe import from_dataframe as from_dataframe

pandas-stubs/core/frame.pyi

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ from pandas.core.indexing import (
3434
_IndexSliceTuple,
3535
_LocIndexer,
3636
)
37+
from pandas.core.interchange.dataframe_protocol import DataFrame as DataFrameXchg
3738
from pandas.core.resample import Resampler
3839
from pandas.core.series import Series
3940
from pandas.core.window import (
@@ -203,6 +204,9 @@ class DataFrame(NDFrame, OpsMixin):
203204
dtype=...,
204205
copy: _bool = ...,
205206
) -> DataFrame: ...
207+
def __dataframe__(
208+
self, nan_as_null: bool = ..., allow_copy: bool = ...
209+
) -> DataFrameXchg: ...
206210
@property
207211
def axes(self) -> list[Index]: ...
208212
@property

pandas-stubs/core/interchange/__init__.pyi

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import abc
2+
from abc import (
3+
ABC,
4+
abstractmethod,
5+
)
6+
import enum
7+
from typing import (
8+
Any,
9+
Iterable,
10+
Sequence,
11+
TypedDict,
12+
)
13+
14+
class DlpackDeviceType(enum.IntEnum):
15+
CPU: int
16+
CUDA: int
17+
CPU_PINNED: int
18+
OPENCL: int
19+
VULKAN: int
20+
METAL: int
21+
VPI: int
22+
ROCM: int
23+
24+
class DtypeKind(enum.IntEnum):
25+
INT: int
26+
UINT: int
27+
FLOAT: int
28+
BOOL: int
29+
STRING: int
30+
DATETIME: int
31+
CATEGORICAL: int
32+
33+
class ColumnNullType(enum.IntEnum):
34+
NON_NULLABLE: int
35+
USE_NAN: int
36+
USE_SENTINEL: int
37+
USE_BITMASK: int
38+
USE_BYTEMASK: int
39+
40+
class ColumnBuffers(TypedDict):
41+
data: tuple[Buffer, Any]
42+
validity: tuple[Buffer, Any] | None
43+
offsets: tuple[Buffer, Any] | None
44+
45+
class CategoricalDescription(TypedDict):
46+
is_ordered: bool
47+
is_dictionary: bool
48+
categories: Column | None
49+
50+
class Buffer(ABC, metaclass=abc.ABCMeta):
51+
@property
52+
@abstractmethod
53+
def bufsize(self) -> int: ...
54+
@property
55+
@abstractmethod
56+
def ptr(self) -> int: ...
57+
@abstractmethod
58+
def __dlpack__(self): ...
59+
@abstractmethod
60+
def __dlpack_device__(self) -> tuple[DlpackDeviceType, int | None]: ...
61+
62+
class Column(ABC, metaclass=abc.ABCMeta):
63+
@property
64+
@abstractmethod
65+
def size(self) -> int: ...
66+
@property
67+
@abstractmethod
68+
def offset(self) -> int: ...
69+
@property
70+
@abstractmethod
71+
def dtype(self) -> tuple[DtypeKind, int, str, str]: ...
72+
@property
73+
@abstractmethod
74+
def describe_categorical(self) -> CategoricalDescription: ...
75+
@property
76+
@abstractmethod
77+
def describe_null(self) -> tuple[ColumnNullType, Any]: ...
78+
@property
79+
@abstractmethod
80+
def null_count(self) -> int | None: ...
81+
@property
82+
@abstractmethod
83+
def metadata(self) -> dict[str, Any]: ...
84+
@abstractmethod
85+
def num_chunks(self) -> int: ...
86+
@abstractmethod
87+
def get_chunks(self, n_chunks: int | None = ...) -> Iterable[Column]: ...
88+
@abstractmethod
89+
def get_buffers(self) -> ColumnBuffers: ...
90+
91+
class DataFrame(ABC, metaclass=abc.ABCMeta):
92+
version: int
93+
@abstractmethod
94+
def __dataframe__(self, nan_as_null: bool = ..., allow_copy: bool = ...): ...
95+
@property
96+
@abstractmethod
97+
def metadata(self) -> dict[str, Any]: ...
98+
@abstractmethod
99+
def num_columns(self) -> int: ...
100+
@abstractmethod
101+
def num_rows(self) -> int | None: ...
102+
@abstractmethod
103+
def num_chunks(self) -> int: ...
104+
@abstractmethod
105+
def column_names(self) -> Iterable[str]: ...
106+
@abstractmethod
107+
def get_column(self, i: int) -> Column: ...
108+
@abstractmethod
109+
def get_column_by_name(self, name: str) -> Column: ...
110+
@abstractmethod
111+
def get_columns(self) -> Iterable[Column]: ...
112+
@abstractmethod
113+
def select_columns(self, indices: Sequence[int]) -> DataFrame: ...
114+
@abstractmethod
115+
def select_columns_by_name(self, names: Sequence[str]) -> DataFrame: ...
116+
@abstractmethod
117+
def get_chunks(self, n_chunks: int | None = ...) -> Iterable[DataFrame]: ...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import pandas as pd
2+
3+
def from_dataframe(df, allow_copy: bool = ...) -> pd.DataFrame: ...

0 commit comments

Comments
 (0)