Skip to content

Commit abc6b1e

Browse files
jbrockmendelJulianWgs
authored andcommitted
REF: share methods between ExtensionBlock, NDArrayBackedExtensionBlock (pandas-dev#40787)
1 parent c22c9b8 commit abc6b1e

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

pandas/core/internals/blocks.py

+25-32
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,31 @@ def delete(self, loc) -> None:
13451345
# _cache not yet initialized
13461346
pass
13471347

1348+
@cache_readonly
1349+
def array_values(self) -> ExtensionArray:
1350+
return self.values
1351+
1352+
def get_values(self, dtype: DtypeObj | None = None) -> np.ndarray:
1353+
"""
1354+
return object dtype as boxed values, such as Timestamps/Timedelta
1355+
"""
1356+
values = self.values
1357+
if dtype == _dtype_obj:
1358+
values = values.astype(object)
1359+
# TODO(EA2D): reshape not needed with 2D EAs
1360+
return np.asarray(values).reshape(self.shape)
1361+
1362+
def interpolate(
1363+
self, method="pad", axis=0, inplace=False, limit=None, fill_value=None, **kwargs
1364+
):
1365+
values = self.values
1366+
if values.ndim == 2 and axis == 0:
1367+
# NDArrayBackedExtensionArray.fillna assumes axis=1
1368+
new_values = values.T.fillna(value=fill_value, method=method, limit=limit).T
1369+
else:
1370+
new_values = values.fillna(value=fill_value, method=method, limit=limit)
1371+
return self.make_block_same_class(new_values)
1372+
13481373

13491374
class ExtensionBlock(EABackedBlock):
13501375
"""
@@ -1469,15 +1494,6 @@ def setitem(self, indexer, value):
14691494
self.values[indexer] = value
14701495
return self
14711496

1472-
def get_values(self, dtype: DtypeObj | None = None) -> np.ndarray:
1473-
# ExtensionArrays must be iterable, so this works.
1474-
# TODO(EA2D): reshape not needed with 2D EAs
1475-
return np.asarray(self.values).reshape(self.shape)
1476-
1477-
@cache_readonly
1478-
def array_values(self) -> ExtensionArray:
1479-
return self.values
1480-
14811497
def take_nd(
14821498
self,
14831499
indexer,
@@ -1549,12 +1565,6 @@ def fillna(
15491565
values = self.values.fillna(value=value, limit=limit)
15501566
return [self.make_block_same_class(values=values)]
15511567

1552-
def interpolate(
1553-
self, method="pad", axis=0, inplace=False, limit=None, fill_value=None, **kwargs
1554-
):
1555-
new_values = self.values.fillna(value=fill_value, method=method, limit=limit)
1556-
return self.make_block_same_class(new_values)
1557-
15581568
def diff(self, n: int, axis: int = 1) -> list[Block]:
15591569
if axis == 0 and n != 0:
15601570
# n==0 case will be a no-op so let is fall through
@@ -1662,27 +1672,12 @@ class NDArrayBackedExtensionBlock(EABackedBlock):
16621672

16631673
values: NDArrayBackedExtensionArray
16641674

1665-
@property
1666-
def array_values(self) -> NDArrayBackedExtensionArray:
1667-
return self.values
1668-
16691675
@property
16701676
def is_view(self) -> bool:
16711677
""" return a boolean if I am possibly a view """
16721678
# check the ndarray values of the DatetimeIndex values
16731679
return self.values._ndarray.base is not None
16741680

1675-
def get_values(self, dtype: DtypeObj | None = None) -> np.ndarray:
1676-
"""
1677-
return object dtype as boxed values, such as Timestamps/Timedelta
1678-
"""
1679-
values = self.values
1680-
if dtype == _dtype_obj:
1681-
# DTA/TDA constructor and astype can handle 2D
1682-
values = values.astype(object)
1683-
# TODO(EA2D): reshape not needed with 2D EAs
1684-
return np.asarray(values).reshape(self.shape)
1685-
16861681
def iget(self, key):
16871682
# GH#31649 we need to wrap scalars in Timestamp/Timedelta
16881683
# TODO(EA2D): this can be removed if we ever have 2D EA
@@ -1799,8 +1794,6 @@ class DatetimeTZBlock(ExtensionBlock, DatetimeLikeBlock):
17991794
putmask = NDArrayBackedExtensionBlock.putmask
18001795
fillna = NDArrayBackedExtensionBlock.fillna
18011796

1802-
get_values = NDArrayBackedExtensionBlock.get_values
1803-
18041797
# error: Incompatible types in assignment (expression has type
18051798
# "Callable[[NDArrayBackedExtensionBlock], bool]", base class "ExtensionBlock"
18061799
# defined the type as "bool") [assignment]

pandas/tests/extension/base/setitem.py

+11
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,14 @@ def test_setitem_series(self, data, full_indexer):
356356
data.astype(object), index=ser.index, name="data", dtype=object
357357
)
358358
self.assert_series_equal(result, expected)
359+
360+
def test_delitem_series(self, data):
361+
# GH#40763
362+
ser = pd.Series(data, name="data")
363+
364+
taker = np.arange(len(ser))
365+
taker = np.delete(taker, 1)
366+
367+
expected = ser[taker]
368+
del ser[1]
369+
self.assert_series_equal(ser, expected)

0 commit comments

Comments
 (0)