Skip to content

BUG: Series.__delitem__ converting EAs to ndarrays #40763

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 4 commits into from
Apr 5, 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ Indexing
- Bug in setting ``numpy.timedelta64`` values into an object-dtype :class:`Series` using a boolean indexer (:issue:`39488`)
- Bug in setting numeric values into a into a boolean-dtypes :class:`Series` using ``at`` or ``iat`` failing to cast to object-dtype (:issue:`39582`)
- Bug in :meth:`DataFrame.loc.__setitem__` when setting-with-expansion incorrectly raising when the index in the expanding axis contains duplicates (:issue:`40096`)
- Bug in :meth:`Series.__delitem__` with ``ExtensionDtype`` incorrectly casting to ``ndarray`` (:issue:`40386`)

Missing
^^^^^^^
Expand Down
38 changes: 23 additions & 15 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,28 @@ def quantile(
return new_block(result, placement=self._mgr_locs, ndim=2)


class ExtensionBlock(Block):
class EABackedBlock(Block):
"""
Mixin for Block subclasses backed by ExtensionArray.
"""

values: ExtensionArray

def delete(self, loc) -> None:
"""
Delete given loc(-s) from block in-place.
"""
# This will be unnecessary if/when __array_function__ is implemented
self.values = self.values.delete(loc)
self.mgr_locs = self._mgr_locs.delete(loc)
try:
self._cache.clear()
except AttributeError:
# _cache not yet initialized
pass


class ExtensionBlock(EABackedBlock):
"""
Block for holding extension types.

Expand Down Expand Up @@ -1647,7 +1668,7 @@ class NumericBlock(Block):
is_numeric = True


class NDArrayBackedExtensionBlock(Block):
class NDArrayBackedExtensionBlock(EABackedBlock):
"""
Block backed by an NDArrayBackedExtensionArray
"""
Expand Down Expand Up @@ -1754,19 +1775,6 @@ def fillna(
new_values = values.fillna(value=value, limit=limit)
return [self.make_block_same_class(values=new_values)]

def delete(self, loc) -> None:
"""
Delete given loc(-s) from block in-place.
"""
# This will be unnecessary if/when __array_function__ is implemented
self.values = self.values.delete(loc, axis=0)
self.mgr_locs = self._mgr_locs.delete(loc)
try:
self._cache.clear()
except AttributeError:
# _cache not yet initialized
pass


class DatetimeLikeBlock(NDArrayBackedExtensionBlock):
"""Mixin class for DatetimeLikeBlock, DatetimeTZBlock."""
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/series/indexing/test_delitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pandas import (
Index,
Series,
date_range,
)
import pandas._testing as tm

Expand Down Expand Up @@ -50,3 +51,23 @@ def test_delitem_missing_key(self):

with pytest.raises(KeyError, match=r"^0$"):
del s[0]

def test_delitem_extension_dtype(self):
# GH#40386
# DatetimeTZDtype
dti = date_range("2016-01-01", periods=3, tz="US/Pacific")
ser = Series(dti)

expected = ser[[0, 2]]
del ser[1]
assert ser.dtype == dti.dtype
tm.assert_series_equal(ser, expected)

# PeriodDtype
pi = dti.tz_localize(None).to_period("D")
ser = Series(pi)

expected = ser[:2]
del ser[2]
assert ser.dtype == pi.dtype
tm.assert_series_equal(ser, expected)