Skip to content

ENH: Categorical.empty #40602

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 13 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 22 additions & 0 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from pandas.util._validators import validate_fillna_kwargs

from pandas.core.dtypes.common import is_dtype_equal
from pandas.core.dtypes.dtypes import ExtensionDtype
from pandas.core.dtypes.missing import array_equivalent

from pandas.core import missing
Expand Down Expand Up @@ -447,3 +448,24 @@ def value_counts(self, dropna: bool = True):
index_arr = self._from_backing_data(np.asarray(result.index._data))
index = Index(index_arr, name=result.index.name)
return Series(result._values, index=index, name=result.name)

# ------------------------------------------------------------------------
# numpy-like methods

@classmethod
def empty(
cls: Type[NDArrayBackedExtensionArrayT], shape: Shape, dtype: ExtensionDtype
) -> NDArrayBackedExtensionArrayT:
"""
Analogous to np.empty(shape, dtype=dtype)

Parameters
----------
shape : tuple[int]
dtype : ExtensionDtype
"""
# The base implementation uses a naive approach to find the dtype
# for the backing ndarray
arr = cls._from_sequence([], dtype=dtype)
backing = np.empty(shape, dtype=arr._ndarray.dtype)
return arr._from_backing_data(backing)
25 changes: 25 additions & 0 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
NpDtype,
Ordered,
Scalar,
Shape,
)
from pandas.compat.numpy import function as nv
from pandas.util._decorators import (
Expand Down Expand Up @@ -1527,6 +1528,30 @@ def value_counts(self, dropna: bool = True):

return Series(count, index=CategoricalIndex(ix), dtype="int64")

# error: Argument 2 of "empty" is incompatible with supertype
# "NDArrayBackedExtensionArray"; supertype defines the argument type as
# "ExtensionDtype"
@classmethod
def empty( # type: ignore[override]
cls: Type[Categorical], shape: Shape, dtype: CategoricalDtype
) -> Categorical:
"""
Analogous to np.empty(shape, dtype=dtype)

Parameters
----------
shape : tuple[int]
dtype : CategoricalDtype
"""
arr = cls._from_sequence([], dtype=dtype)

# We have to use np.zeros instead of np.empty otherwise the resulting
# ndarray may contain codes not supported by this dtype, in which
# case repr(result) could segfault.
backing = np.zeros(shape, dtype=arr._ndarray.dtype)

return arr._from_backing_data(backing)

def _internal_get_values(self):
"""
Return the values.
Expand Down
89 changes: 89 additions & 0 deletions pandas/tests/arrays/test_ndarray_backed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""
Tests for subclasses of NDArrayBackedExtensionArray
"""
import numpy as np
import pytest

from pandas import (
CategoricalIndex,
date_range,
)
from pandas.core.arrays import (
Categorical,
DatetimeArray,
PandasArray,
PeriodArray,
TimedeltaArray,
)


@pytest.fixture(
params=[Categorical, DatetimeArray, TimedeltaArray, PeriodArray, PandasArray]
)
def ea_subclass(request):
"""
Fixture for subclasses of NDArrayBackedExtensionArray.
"""
return request.param


class TestEmpty:
# def test_empty(self, ea_subclass):

def test_empty_categorical(self):
ci = CategoricalIndex(["a", "b", "c"], ordered=True)
dtype = ci.dtype

# case with int8 codes
shape = (4,)
result = Categorical.empty(shape, dtype=dtype)
assert isinstance(result, Categorical)
assert result.shape == shape
assert result._ndarray.dtype == np.int8

# case where repr would segfault if we didn't override base implementation
result = Categorical.empty((4096,), dtype=dtype)
assert isinstance(result, Categorical)
assert result.shape == (4096,)
assert result._ndarray.dtype == np.int8
repr(result)

# case with int16 codes
ci = CategoricalIndex(list(range(512)) * 4, ordered=False)
dtype = ci.dtype
result = Categorical.empty(shape, dtype=dtype)
assert isinstance(result, Categorical)
assert result.shape == shape
assert result._ndarray.dtype == np.int16

def test_empty_dt64tz(self):
dti = date_range("2016-01-01", periods=2, tz="Asia/Tokyo")
dtype = dti.dtype

shape = (0,)
result = DatetimeArray.empty(shape, dtype=dtype)
assert result.dtype == dtype
assert isinstance(result, DatetimeArray)
assert result.shape == shape

def test_empty_dt64(self):
shape = (3, 9)
result = DatetimeArray.empty(shape, dtype="datetime64[ns]")
assert isinstance(result, DatetimeArray)
assert result.shape == shape

def test_empty_td64(self):
shape = (3, 9)
result = TimedeltaArray.empty(shape, dtype="m8[ns]")
assert isinstance(result, TimedeltaArray)
assert result.shape == shape

def test_empty_pandas_array(self):
arr = PandasArray(np.array([1, 2]))
dtype = arr.dtype

shape = (3, 9)
result = PandasArray.empty(shape, dtype=dtype)
assert isinstance(result, PandasArray)
assert result.dtype == dtype
assert result.shape == shape