Skip to content

Commit 7cb8449

Browse files
jbrockmendelJulianWgs
authored andcommitted
BUG: DTBlock/TDBlock.delete casting to ndarray (pandas-dev#40741)
1 parent 1cc333b commit 7cb8449

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

pandas/core/internals/blocks.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ def make_block_same_class(
278278
""" Wrap given values in a block of same type as self. """
279279
if placement is None:
280280
placement = self._mgr_locs
281+
282+
if values.dtype.kind == "m":
283+
# TODO: remove this once fastparquet has stopped relying on it
284+
values = ensure_wrapped_if_datetimelike(values)
285+
281286
# We assume maybe_coerce_values has already been called
282287
return type(self)(values, placement=placement, ndim=self.ndim)
283288

@@ -369,7 +374,6 @@ def set_inplace(self, locs, values):
369374
"""
370375
self.values[locs] = values
371376

372-
@final
373377
def delete(self, loc) -> None:
374378
"""
375379
Delete given loc(-s) from block in-place.
@@ -1851,6 +1855,19 @@ def fillna(
18511855
new_values = values.fillna(value=value, limit=limit)
18521856
return [self.make_block_same_class(values=new_values)]
18531857

1858+
def delete(self, loc) -> None:
1859+
"""
1860+
Delete given loc(-s) from block in-place.
1861+
"""
1862+
# This will be unnecessary if/when __array_function__ is implemented
1863+
self.values = self.values.delete(loc, axis=0)
1864+
self.mgr_locs = self._mgr_locs.delete(loc)
1865+
try:
1866+
self._cache.clear()
1867+
except AttributeError:
1868+
# _cache not yet initialized
1869+
pass
1870+
18541871

18551872
class DatetimeLikeBlockMixin(NDArrayBackedExtensionBlock):
18561873
"""Mixin class for DatetimeBlock, DatetimeTZBlock, and TimedeltaBlock."""
@@ -2120,7 +2137,9 @@ def ensure_block_shape(values: ArrayLike, ndim: int = 1) -> ArrayLike:
21202137
# TODO(EA2D): https://github.com/pandas-dev/pandas/issues/23023
21212138
# block.shape is incorrect for "2D" ExtensionArrays
21222139
# We can't, and don't need to, reshape.
2123-
values = np.asarray(values).reshape(1, -1)
2140+
values = cast(Union[np.ndarray, DatetimeArray, TimedeltaArray], values)
2141+
values = values.reshape(1, -1)
2142+
21242143
return values
21252144

21262145

pandas/core/internals/concat.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
DatetimeArray,
4444
ExtensionArray,
4545
)
46+
from pandas.core.construction import ensure_wrapped_if_datetimelike
4647
from pandas.core.internals.array_manager import ArrayManager
4748
from pandas.core.internals.blocks import (
4849
ensure_block_shape,
@@ -142,8 +143,9 @@ def concatenate_managers(
142143
else:
143144
# TODO(EA2D): special-casing not needed with 2D EAs
144145
values = concat_compat(vals)
145-
if not isinstance(values, ExtensionArray):
146-
values = values.reshape(1, len(values))
146+
values = ensure_block_shape(values, ndim=2)
147+
148+
values = ensure_wrapped_if_datetimelike(values)
147149

148150
if blk.values.dtype == values.dtype:
149151
# Fast-path

pandas/tests/internals/test_internals.py

+18
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from pandas.core.arrays import (
3131
DatetimeArray,
3232
SparseArray,
33+
TimedeltaArray,
3334
)
3435
from pandas.core.internals import (
3536
BlockManager,
@@ -300,6 +301,23 @@ def test_delete(self):
300301
with pytest.raises(IndexError, match=None):
301302
newb.delete(3)
302303

304+
def test_delete_datetimelike(self):
305+
# dont use np.delete on values, as that will coerce from DTA/TDA to ndarray
306+
arr = np.arange(20, dtype="i8").reshape(5, 4).view("m8[ns]")
307+
df = DataFrame(arr)
308+
blk = df._mgr.blocks[0]
309+
assert isinstance(blk.values, TimedeltaArray)
310+
311+
blk.delete(1)
312+
assert isinstance(blk.values, TimedeltaArray)
313+
314+
df = DataFrame(arr.view("M8[ns]"))
315+
blk = df._mgr.blocks[0]
316+
assert isinstance(blk.values, DatetimeArray)
317+
318+
blk.delete([1, 3])
319+
assert isinstance(blk.values, DatetimeArray)
320+
303321
def test_split(self):
304322
# GH#37799
305323
values = np.random.randn(3, 4)

0 commit comments

Comments
 (0)