Skip to content

REF: share methods between ExtensionBlock, NDArrayBackedExtensionBlock #40787

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 3 commits into from
Apr 7, 2021
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
57 changes: 25 additions & 32 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,31 @@ def delete(self, loc) -> None:
# _cache not yet initialized
pass

@cache_readonly
def array_values(self) -> ExtensionArray:
return self.values

def get_values(self, dtype: DtypeObj | None = None) -> np.ndarray:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we not use Optional anymore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think a code check was added in the last few days, not sure of the specifics

"""
return object dtype as boxed values, such as Timestamps/Timedelta
"""
values = self.values
if dtype == _dtype_obj:
values = values.astype(object)
# TODO(EA2D): reshape not needed with 2D EAs
return np.asarray(values).reshape(self.shape)

def interpolate(
self, method="pad", axis=0, inplace=False, limit=None, fill_value=None, **kwargs
):
values = self.values
if values.ndim == 2 and axis == 0:
# NDArrayBackedExtensionArray.fillna assumes axis=1
new_values = values.T.fillna(value=fill_value, method=method, limit=limit).T
else:
new_values = values.fillna(value=fill_value, method=method, limit=limit)
return self.make_block_same_class(new_values)


class ExtensionBlock(EABackedBlock):
"""
Expand Down Expand Up @@ -1478,15 +1503,6 @@ def setitem(self, indexer, value):
self.values[indexer] = value
return self

def get_values(self, dtype: DtypeObj | None = None) -> np.ndarray:
# ExtensionArrays must be iterable, so this works.
# TODO(EA2D): reshape not needed with 2D EAs
return np.asarray(self.values).reshape(self.shape)

@cache_readonly
def array_values(self) -> ExtensionArray:
return self.values

def take_nd(
self,
indexer,
Expand Down Expand Up @@ -1558,12 +1574,6 @@ def fillna(
values = self.values.fillna(value=value, limit=limit)
return [self.make_block_same_class(values=values)]

def interpolate(
self, method="pad", axis=0, inplace=False, limit=None, fill_value=None, **kwargs
):
new_values = self.values.fillna(value=fill_value, method=method, limit=limit)
return self.make_block_same_class(new_values)

def diff(self, n: int, axis: int = 1) -> list[Block]:
if axis == 0 and n != 0:
# n==0 case will be a no-op so let is fall through
Expand Down Expand Up @@ -1671,27 +1681,12 @@ class NDArrayBackedExtensionBlock(EABackedBlock):

values: NDArrayBackedExtensionArray

@property
def array_values(self) -> NDArrayBackedExtensionArray:
return self.values

@property
def is_view(self) -> bool:
""" return a boolean if I am possibly a view """
# check the ndarray values of the DatetimeIndex values
return self.values._ndarray.base is not None

def get_values(self, dtype: DtypeObj | None = None) -> np.ndarray:
"""
return object dtype as boxed values, such as Timestamps/Timedelta
"""
values = self.values
if dtype == _dtype_obj:
# DTA/TDA constructor and astype can handle 2D
values = values.astype(object)
# TODO(EA2D): reshape not needed with 2D EAs
return np.asarray(values).reshape(self.shape)

def iget(self, key):
# GH#31649 we need to wrap scalars in Timestamp/Timedelta
# TODO(EA2D): this can be removed if we ever have 2D EA
Expand Down Expand Up @@ -1795,8 +1790,6 @@ class DatetimeTZBlock(ExtensionBlock, DatetimeLikeBlock):
putmask = NDArrayBackedExtensionBlock.putmask
fillna = NDArrayBackedExtensionBlock.fillna

get_values = NDArrayBackedExtensionBlock.get_values

# error: Incompatible types in assignment (expression has type
# "Callable[[NDArrayBackedExtensionBlock], bool]", base class "ExtensionBlock"
# defined the type as "bool") [assignment]
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/extension/base/setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,14 @@ def test_setitem_series(self, data, full_indexer):
data.astype(object), index=ser.index, name="data", dtype=object
)
self.assert_series_equal(result, expected)

def test_delitem_series(self, data):
# GH#40763
ser = pd.Series(data, name="data")

taker = np.arange(len(ser))
taker = np.delete(taker, 1)

expected = ser[taker]
del ser[1]
self.assert_series_equal(ser, expected)