Skip to content
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

EA: support basic 2D operations #27142

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d673008
un-xfail tests, xfail instead of skip, minor cleanup
jbrockmendel Jun 28, 2019
b2a837b
Merge branch 'master' of https://github.com/pandas-dev/pandas into xf…
jbrockmendel Jun 28, 2019
c2fd1b1
Merge branch 'master' of https://github.com/pandas-dev/pandas into xf…
jbrockmendel Jun 29, 2019
bed5563
REF: derive __len__ from shape instead of vice-versa
jbrockmendel Jun 30, 2019
29d9dc2
Merge branch 'master' of https://github.com/pandas-dev/pandas into ar…
jbrockmendel Jun 30, 2019
f3ce13c
update docstring
jbrockmendel Jun 30, 2019
4fd24c1
remove duplicated methods
jbrockmendel Jun 30, 2019
c0505ee
Merge branch 'master' of https://github.com/pandas-dev/pandas into ar…
jbrockmendel Jul 1, 2019
c81daeb
implement shape in terms of size, with implement_2d decorator
jbrockmendel Jul 1, 2019
4d77dbe
move implement_2d, implement view
jbrockmendel Jul 1, 2019
d43ef30
port tests
jbrockmendel Jul 2, 2019
91c979b
Merge branch 'master' of https://github.com/pandas-dev/pandas into ar…
jbrockmendel Jul 2, 2019
bc220c3
shape patching, tests
jbrockmendel Jul 2, 2019
421b5a3
Merge branch 'master' of https://github.com/pandas-dev/pandas into ar…
jbrockmendel Jul 2, 2019
7c6df89
Merge branch 'master' of https://github.com/pandas-dev/pandas into ar…
jbrockmendel Jul 5, 2019
203504c
blackify
jbrockmendel Jul 5, 2019
bc12f01
Merge branch 'master' of https://github.com/pandas-dev/pandas into ar…
jbrockmendel Jul 8, 2019
2f18dae
shape fixups
jbrockmendel Jul 8, 2019
eb0645d
blackify+isort
jbrockmendel Jul 8, 2019
2540933
property read-write
jbrockmendel Jul 8, 2019
5b60fb5
add docstring
jbrockmendel Jul 9, 2019
96f3ae2
Merge branch 'master' of https://github.com/pandas-dev/pandas into ar…
jbrockmendel Jul 22, 2019
92a0a56
implement base class view
jbrockmendel Jul 22, 2019
81191bf
Merge branch 'master' of https://github.com/pandas-dev/pandas into ar…
jbrockmendel Jul 22, 2019
91639dd
use base view
jbrockmendel Jul 22, 2019
34cc9e9
patch take, getitem
jbrockmendel Jul 22, 2019
444f9f7
blackify
jbrockmendel Jul 22, 2019
7c15b74
isort fixup
jbrockmendel Jul 22, 2019
768d75d
patch iter
jbrockmendel Jul 22, 2019
3b7b2b2
slice handling cleanup
jbrockmendel Jul 22, 2019
177bfb0
Merge branch 'master' of https://github.com/pandas-dev/pandas into ar…
jbrockmendel Jul 29, 2019
f5cba22
Merge branch 'master' of https://github.com/pandas-dev/pandas into ar…
jbrockmendel Aug 2, 2019
174a1da
Merge branch 'master' of https://github.com/pandas-dev/pandas into ar…
jbrockmendel Aug 3, 2019
1d78fbe
Merge branch 'master' of https://github.com/pandas-dev/pandas into ar…
jbrockmendel Aug 5, 2019
41b49d9
Merge branch 'master' of https://github.com/pandas-dev/pandas into ar…
jbrockmendel Aug 9, 2019
fc331b8
dummy to force CI
jbrockmendel Aug 9, 2019
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
15 changes: 11 additions & 4 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ExtensionArray:
* _from_sequence
* _from_factorized
* __getitem__
* __len__
* shape
* dtype
* nbytes
* isna
Expand Down Expand Up @@ -272,7 +272,7 @@ def __len__(self) -> int:
-------
length : int
"""
raise AbstractMethodError(self)
return self.shape[0]

def __iter__(self):
"""
Expand Down Expand Up @@ -300,14 +300,21 @@ def shape(self) -> Tuple[int, ...]:
"""
Return a tuple of the array dimensions.
"""
return (len(self),)
raise AbstractMethodError(self)

@property
def ndim(self) -> int:
"""
Extension Arrays are only allowed to be 1-dimensional.
"""
return 1
return len(self.shape)

@property
def size(self) -> int:
"""
The number of elements in this array.
"""
return np.prod(self.shape)

@property
def nbytes(self) -> int:
Expand Down
22 changes: 3 additions & 19 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,20 +489,6 @@ def astype(self, dtype, copy=True):
return self._set_dtype(dtype)
return np.array(self, dtype=dtype, copy=copy)

@cache_readonly
def ndim(self):
"""
Number of dimensions of the Categorical
"""
return self._codes.ndim

@cache_readonly
def size(self):
"""
return the len of myself
"""
return len(self)

@cache_readonly
def itemsize(self):
"""
Expand Down Expand Up @@ -1913,11 +1899,9 @@ def _slice(self, slicer):
codes = self._codes[slicer]
return self._constructor(values=codes, dtype=self.dtype, fastpath=True)

def __len__(self):
"""
The length of this Categorical.
"""
return len(self._codes)
@property
def shape(self):
return self._codes.shape

def __iter__(self):
"""
Expand Down
8 changes: 2 additions & 6 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,12 +402,8 @@ def __array__(self, dtype=None):
return self._data

@property
def size(self) -> int:
"""The number of elements in this array."""
return np.prod(self.shape)

def __len__(self):
return len(self._data)
def shape(self):
jbrockmendel marked this conversation as resolved.
Show resolved Hide resolved
return self._data.shape

def __getitem__(self, key):
"""
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,9 @@ def __setitem__(self, key, value):
self._data[key] = value
self._mask[key] = mask

def __len__(self):
return len(self._data)
@property
def shape(self):
return self._data.shape

@property
def nbytes(self):
Expand Down
10 changes: 3 additions & 7 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,9 @@ def _validate(self):
def __iter__(self):
return iter(np.asarray(self))

def __len__(self):
return len(self.left)
@property
def shape(self):
return self.left.shape

def __getitem__(self, value):
left = self.left[value]
Expand Down Expand Up @@ -701,11 +702,6 @@ def isna(self):
def nbytes(self):
return self.left.nbytes + self.right.nbytes

@property
def size(self):
# Avoid materializing self.values
return self.left.size

@property
def shape(self):
return self.left.shape
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,9 @@ def __setitem__(self, key, value):
else:
self._ndarray[key] = value

def __len__(self):
return len(self._ndarray)
@property
def shape(self):
return self._ndarray.shape

@property
def nbytes(self):
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,8 +849,9 @@ def _valid_sp_values(self):
mask = notna(sp_vals)
return sp_vals[mask]

def __len__(self):
return self.sp_index.length
@property
def shape(self):
return (self.sp_index.length,)

@property
def _null_fill_value(self):
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/extension/arrow/bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ def __getitem__(self, item):
vals = self._data.to_pandas()[item]
return type(self).from_scalars(vals)

def __len__(self):
return len(self._data)
@property
def shape(self):
return (len(self._data),)

def astype(self, dtype, copy=True):
# needed to fix this astype for the Series constructor.
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/extension/arrow/test_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ def dtype():
return ArrowBoolDtype()


@pytest.fixture
def data():
values = np.random.randint(0, 2, size=100, dtype=bool)
values[1] = ~values[0]
return ArrowBoolArray.from_scalars(values)


@pytest.fixture(name="data")
def data_fixture():
return data()


@pytest.fixture
def data_missing():
return ArrowBoolArray.from_scalars([None, True])
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/extension/decimal/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ def __setitem__(self, key, value):
value = decimal.Decimal(value)
self._data[key] = value

def __len__(self):
return len(self._data)
@property
def shape(self):
return self._data.shape

@property
def nbytes(self):
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/extension/json/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ def __setitem__(self, key, value):
assert isinstance(v, self.dtype.type)
self.data[k] = v

def __len__(self):
return len(self.data)
@property
def shape(self):
return (len(self.data),)

@property
def nbytes(self):
Expand Down