Skip to content

Commit 88fb114

Browse files
[ArrayManager] TST: Enable extension tests (#40348)
1 parent 7e0f5ca commit 88fb114

File tree

10 files changed

+55
-17
lines changed

10 files changed

+55
-17
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ jobs:
172172
pytest pandas/tests/computation/
173173
pytest pandas/tests/config/
174174
pytest pandas/tests/dtypes/
175+
pytest pandas/tests/extension/
175176
pytest pandas/tests/generic/
176177
pytest pandas/tests/indexes/
177178
pytest pandas/tests/io/test_* -m "not slow and not clipboard"

pandas/core/internals/array_manager.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,10 @@ def get_numeric_data(self, copy: bool = False) -> ArrayManager:
689689
copy : bool, default False
690690
Whether to copy the blocks
691691
"""
692-
return self._get_data_subset(lambda arr: is_numeric_dtype(arr.dtype))
692+
return self._get_data_subset(
693+
lambda arr: is_numeric_dtype(arr.dtype)
694+
or getattr(arr.dtype, "_is_numeric", False)
695+
)
693696

694697
def copy(self: T, deep=True) -> T:
695698
"""

pandas/tests/extension/base/casting.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ class BaseCastingTests(BaseExtensionTests):
1212
def test_astype_object_series(self, all_data):
1313
ser = pd.Series(all_data, name="A")
1414
result = ser.astype(object)
15-
assert isinstance(result._mgr.blocks[0], ObjectBlock)
15+
assert result.dtype == np.dtype(object)
16+
if hasattr(result._mgr, "blocks"):
17+
assert isinstance(result._mgr.blocks[0], ObjectBlock)
18+
assert isinstance(result._mgr.array, np.ndarray)
19+
assert result._mgr.array.dtype == np.dtype(object)
1620

1721
def test_astype_object_frame(self, all_data):
1822
df = pd.DataFrame({"A": all_data})
1923

2024
result = df.astype(object)
21-
blk = result._data.blocks[0]
22-
assert isinstance(blk, ObjectBlock), type(blk)
25+
if hasattr(result._mgr, "blocks"):
26+
blk = result._data.blocks[0]
27+
assert isinstance(blk, ObjectBlock), type(blk)
28+
assert isinstance(result._mgr.arrays[0], np.ndarray)
29+
assert result._mgr.arrays[0].dtype == np.dtype(object)
2330

2431
# FIXME: these currently fail; dont leave commented-out
2532
# check that we can compare the dtypes

pandas/tests/extension/base/constructors.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pytest
33

44
import pandas as pd
5+
from pandas.api.extensions import ExtensionArray
56
from pandas.core.internals import ExtensionBlock
67
from pandas.tests.extension.base.base import BaseExtensionTests
78

@@ -24,13 +25,15 @@ def test_series_constructor(self, data):
2425
result = pd.Series(data)
2526
assert result.dtype == data.dtype
2627
assert len(result) == len(data)
27-
assert isinstance(result._mgr.blocks[0], ExtensionBlock)
28-
assert result._mgr.blocks[0].values is data
28+
if hasattr(result._mgr, "blocks"):
29+
assert isinstance(result._mgr.blocks[0], ExtensionBlock)
30+
assert result._mgr.array is data
2931

3032
# Series[EA] is unboxed / boxed correctly
3133
result2 = pd.Series(result)
3234
assert result2.dtype == data.dtype
33-
assert isinstance(result2._mgr.blocks[0], ExtensionBlock)
35+
if hasattr(result._mgr, "blocks"):
36+
assert isinstance(result2._mgr.blocks[0], ExtensionBlock)
3437

3538
def test_series_constructor_no_data_with_index(self, dtype, na_value):
3639
result = pd.Series(index=[1, 2, 3], dtype=dtype)
@@ -64,13 +67,17 @@ def test_dataframe_constructor_from_dict(self, data, from_series):
6467
result = pd.DataFrame({"A": data})
6568
assert result.dtypes["A"] == data.dtype
6669
assert result.shape == (len(data), 1)
67-
assert isinstance(result._mgr.blocks[0], ExtensionBlock)
70+
if hasattr(result._mgr, "blocks"):
71+
assert isinstance(result._mgr.blocks[0], ExtensionBlock)
72+
assert isinstance(result._mgr.arrays[0], ExtensionArray)
6873

6974
def test_dataframe_from_series(self, data):
7075
result = pd.DataFrame(pd.Series(data))
7176
assert result.dtypes[0] == data.dtype
7277
assert result.shape == (len(data), 1)
73-
assert isinstance(result._mgr.blocks[0], ExtensionBlock)
78+
if hasattr(result._mgr, "blocks"):
79+
assert isinstance(result._mgr.blocks[0], ExtensionBlock)
80+
assert isinstance(result._mgr.arrays[0], ExtensionArray)
7481

7582
def test_series_given_mismatched_index_raises(self, data):
7683
msg = r"Length of values \(3\) does not match length of index \(5\)"

pandas/tests/extension/base/getitem.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,10 @@ def test_loc_len1(self, data):
408408
# see GH-27785 take_nd with indexer of len 1 resulting in wrong ndim
409409
df = pd.DataFrame({"A": data})
410410
res = df.loc[[0], "A"]
411-
assert res._mgr._block.ndim == 1
411+
assert res.ndim == 1
412+
assert res._mgr.arrays[0].ndim == 1
413+
if hasattr(res._mgr, "blocks"):
414+
assert res._mgr._block.ndim == 1
412415

413416
def test_item(self, data):
414417
# https://github.com/pandas-dev/pandas/pull/30175

pandas/tests/extension/base/interface.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def test_no_values_attribute(self, data):
8181

8282
def test_is_numeric_honored(self, data):
8383
result = pd.Series(data)
84-
assert result._mgr.blocks[0].is_numeric is data.dtype._is_numeric
84+
if hasattr(result._mgr, "blocks"):
85+
assert result._mgr.blocks[0].is_numeric is data.dtype._is_numeric
8586

8687
def test_isna_extension_array(self, data_missing):
8788
# If your `isna` returns an ExtensionArray, you must also implement

pandas/tests/extension/base/reshaping.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import numpy as np
44
import pytest
55

6+
import pandas.util._test_decorators as td
7+
68
import pandas as pd
9+
from pandas.api.extensions import ExtensionArray
710
from pandas.core.internals import ExtensionBlock
811
from pandas.tests.extension.base.base import BaseExtensionTests
912

@@ -26,7 +29,9 @@ def test_concat(self, data, in_frame):
2629
dtype = result.dtype
2730

2831
assert dtype == data.dtype
29-
assert isinstance(result._mgr.blocks[0], ExtensionBlock)
32+
if hasattr(result._mgr, "blocks"):
33+
assert isinstance(result._mgr.blocks[0], ExtensionBlock)
34+
assert isinstance(result._mgr.arrays[0], ExtensionArray)
3035

3136
@pytest.mark.parametrize("in_frame", [True, False])
3237
def test_concat_all_na_block(self, data_missing, in_frame):
@@ -106,6 +111,7 @@ def test_concat_extension_arrays_copy_false(self, data, na_value):
106111
result = pd.concat([df1, df2], axis=1, copy=False)
107112
self.assert_frame_equal(result, expected)
108113

114+
@td.skip_array_manager_not_yet_implemented # TODO(ArrayManager) concat reindex
109115
def test_concat_with_reindex(self, data):
110116
# GH-33027
111117
a = pd.DataFrame({"a": data[:5]})

pandas/tests/extension/test_external_block.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
import pytest
33

44
from pandas._libs.internals import BlockPlacement
5+
import pandas.util._test_decorators as td
56

67
import pandas as pd
78
from pandas.core.internals import BlockManager
89
from pandas.core.internals.blocks import ExtensionBlock
910

11+
pytestmark = td.skip_array_manager_invalid_test
12+
1013

1114
class CustomBlock(ExtensionBlock):
1215

pandas/tests/extension/test_numpy.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import numpy as np
1717
import pytest
1818

19+
import pandas.util._test_decorators as td
20+
1921
from pandas.core.dtypes.dtypes import (
2022
ExtensionDtype,
2123
PandasDtype,
@@ -28,6 +30,9 @@
2830
from pandas.core.internals import managers
2931
from pandas.tests.extension import base
3032

33+
# TODO(ArrayManager) PandasArray
34+
pytestmark = td.skip_array_manager_not_yet_implemented
35+
3136

3237
def _extract_array_patched(obj):
3338
if isinstance(obj, (pd.Index, pd.Series)):

pandas/tests/extension/test_sparse.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ def test_fillna_copy_frame(self, data_missing):
290290
filled_val = df.iloc[0, 0]
291291
result = df.fillna(filled_val)
292292

293-
assert df.values.base is not result.values.base
293+
if hasattr(df._mgr, "blocks"):
294+
assert df.values.base is not result.values.base
294295
assert df.A._values.to_dense() is arr.to_dense()
295296

296297
def test_fillna_copy_series(self, data_missing):
@@ -362,18 +363,19 @@ def test_equals(self, data, na_value, as_series, box):
362363
class TestCasting(BaseSparseTests, base.BaseCastingTests):
363364
def test_astype_object_series(self, all_data):
364365
# Unlike the base class, we do not expect the resulting Block
365-
# to be ObjectBlock
366+
# to be ObjectBlock / resulting array to be np.dtype("object")
366367
ser = pd.Series(all_data, name="A")
367368
result = ser.astype(object)
368-
assert is_object_dtype(result._data.blocks[0].dtype)
369+
assert is_object_dtype(result.dtype)
370+
assert is_object_dtype(result._mgr.array.dtype)
369371

370372
def test_astype_object_frame(self, all_data):
371373
# Unlike the base class, we do not expect the resulting Block
372-
# to be ObjectBlock
374+
# to be ObjectBlock / resulting array to be np.dtype("object")
373375
df = pd.DataFrame({"A": all_data})
374376

375377
result = df.astype(object)
376-
assert is_object_dtype(result._data.blocks[0].dtype)
378+
assert is_object_dtype(result._mgr.arrays[0].dtype)
377379

378380
# FIXME: these currently fail; dont leave commented-out
379381
# check that we can compare the dtypes

0 commit comments

Comments
 (0)