Skip to content

BUG: DTBlock/TDBlock.delete casting to ndarray #40741

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 2 commits into from
Apr 2, 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
23 changes: 21 additions & 2 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ def make_block_same_class(
""" Wrap given values in a block of same type as self. """
if placement is None:
placement = self._mgr_locs

if values.dtype.kind == "m":
# TODO: remove this once fastparquet has stopped relying on it
values = ensure_wrapped_if_datetimelike(values)

# We assume maybe_coerce_values has already been called
return type(self)(values, placement=placement, ndim=self.ndim)

Expand Down Expand Up @@ -371,7 +376,6 @@ def set_inplace(self, locs, values):
"""
self.values[locs] = values

@final
def delete(self, loc) -> None:
"""
Delete given loc(-s) from block in-place.
Expand Down Expand Up @@ -1893,6 +1897,19 @@ 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 DatetimeLikeBlockMixin(NDArrayBackedExtensionBlock):
"""Mixin class for DatetimeBlock, DatetimeTZBlock, and TimedeltaBlock."""
Expand Down Expand Up @@ -2173,7 +2190,9 @@ def ensure_block_shape(values: ArrayLike, ndim: int = 1) -> ArrayLike:
# TODO(EA2D): https://github.com/pandas-dev/pandas/issues/23023
# block.shape is incorrect for "2D" ExtensionArrays
# We can't, and don't need to, reshape.
values = np.asarray(values).reshape(1, -1)
values = cast(Union[np.ndarray, DatetimeArray, TimedeltaArray], values)
values = values.reshape(1, -1)

return values


Expand Down
6 changes: 4 additions & 2 deletions pandas/core/internals/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
DatetimeArray,
ExtensionArray,
)
from pandas.core.construction import ensure_wrapped_if_datetimelike
from pandas.core.internals.array_manager import ArrayManager
from pandas.core.internals.blocks import (
ensure_block_shape,
Expand Down Expand Up @@ -142,8 +143,9 @@ def concatenate_managers(
else:
# TODO(EA2D): special-casing not needed with 2D EAs
values = concat_compat(vals)
if not isinstance(values, ExtensionArray):
values = values.reshape(1, len(values))
values = ensure_block_shape(values, ndim=2)

values = ensure_wrapped_if_datetimelike(values)

if blk.values.dtype == values.dtype:
# Fast-path
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/internals/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from pandas.core.arrays import (
DatetimeArray,
SparseArray,
TimedeltaArray,
)
from pandas.core.internals import (
BlockManager,
Expand Down Expand Up @@ -300,6 +301,23 @@ def test_delete(self):
with pytest.raises(IndexError, match=None):
newb.delete(3)

def test_delete_datetimelike(self):
# dont use np.delete on values, as that will coerce from DTA/TDA to ndarray
arr = np.arange(20, dtype="i8").reshape(5, 4).view("m8[ns]")
df = DataFrame(arr)
blk = df._mgr.blocks[0]
assert isinstance(blk.values, TimedeltaArray)

blk.delete(1)
assert isinstance(blk.values, TimedeltaArray)

df = DataFrame(arr.view("M8[ns]"))
blk = df._mgr.blocks[0]
assert isinstance(blk.values, DatetimeArray)

blk.delete([1, 3])
assert isinstance(blk.values, DatetimeArray)

def test_split(self):
# GH#37799
values = np.random.randn(3, 4)
Expand Down