|
| 1 | +""" |
| 2 | +Tests for subclasses of NDArrayBackedExtensionArray |
| 3 | +""" |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from pandas import ( |
| 7 | + CategoricalIndex, |
| 8 | + date_range, |
| 9 | +) |
| 10 | +from pandas.core.arrays import ( |
| 11 | + Categorical, |
| 12 | + DatetimeArray, |
| 13 | + PandasArray, |
| 14 | + TimedeltaArray, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +class TestEmpty: |
| 19 | + def test_empty_categorical(self): |
| 20 | + ci = CategoricalIndex(["a", "b", "c"], ordered=True) |
| 21 | + dtype = ci.dtype |
| 22 | + |
| 23 | + # case with int8 codes |
| 24 | + shape = (4,) |
| 25 | + result = Categorical._empty(shape, dtype=dtype) |
| 26 | + assert isinstance(result, Categorical) |
| 27 | + assert result.shape == shape |
| 28 | + assert result._ndarray.dtype == np.int8 |
| 29 | + |
| 30 | + # case where repr would segfault if we didn't override base implementation |
| 31 | + result = Categorical._empty((4096,), dtype=dtype) |
| 32 | + assert isinstance(result, Categorical) |
| 33 | + assert result.shape == (4096,) |
| 34 | + assert result._ndarray.dtype == np.int8 |
| 35 | + repr(result) |
| 36 | + |
| 37 | + # case with int16 codes |
| 38 | + ci = CategoricalIndex(list(range(512)) * 4, ordered=False) |
| 39 | + dtype = ci.dtype |
| 40 | + result = Categorical._empty(shape, dtype=dtype) |
| 41 | + assert isinstance(result, Categorical) |
| 42 | + assert result.shape == shape |
| 43 | + assert result._ndarray.dtype == np.int16 |
| 44 | + |
| 45 | + def test_empty_dt64tz(self): |
| 46 | + dti = date_range("2016-01-01", periods=2, tz="Asia/Tokyo") |
| 47 | + dtype = dti.dtype |
| 48 | + |
| 49 | + shape = (0,) |
| 50 | + result = DatetimeArray._empty(shape, dtype=dtype) |
| 51 | + assert result.dtype == dtype |
| 52 | + assert isinstance(result, DatetimeArray) |
| 53 | + assert result.shape == shape |
| 54 | + |
| 55 | + def test_empty_dt64(self): |
| 56 | + shape = (3, 9) |
| 57 | + result = DatetimeArray._empty(shape, dtype="datetime64[ns]") |
| 58 | + assert isinstance(result, DatetimeArray) |
| 59 | + assert result.shape == shape |
| 60 | + |
| 61 | + def test_empty_td64(self): |
| 62 | + shape = (3, 9) |
| 63 | + result = TimedeltaArray._empty(shape, dtype="m8[ns]") |
| 64 | + assert isinstance(result, TimedeltaArray) |
| 65 | + assert result.shape == shape |
| 66 | + |
| 67 | + def test_empty_pandas_array(self): |
| 68 | + arr = PandasArray(np.array([1, 2])) |
| 69 | + dtype = arr.dtype |
| 70 | + |
| 71 | + shape = (3, 9) |
| 72 | + result = PandasArray._empty(shape, dtype=dtype) |
| 73 | + assert isinstance(result, PandasArray) |
| 74 | + assert result.dtype == dtype |
| 75 | + assert result.shape == shape |
0 commit comments