Skip to content

Commit 8ae1b4c

Browse files
authored
TST: tests for nullable issues (#45167)
1 parent 74209d2 commit 8ae1b4c

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import numpy as np
2+
3+
from pandas import (
4+
Categorical,
5+
CategoricalDtype,
6+
array,
7+
)
8+
import pandas._testing as tm
9+
10+
11+
class TestAstype:
12+
def test_astype_str_int_categories_to_nullable_int(self):
13+
# GH#39616
14+
dtype = CategoricalDtype([str(i) for i in range(5)])
15+
arr = Categorical.from_codes(np.random.randint(5, size=20), dtype=dtype)
16+
17+
res = arr.astype("Int64")
18+
expected = array(arr.astype("int64"), dtype="Int64")
19+
tm.assert_extension_array_equal(res, expected)

pandas/tests/arrays/categorical/test_constructors.py

+9
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,15 @@ def test_construction_with_null(self, klass, nulls_fixture):
511511

512512
tm.assert_categorical_equal(result, expected)
513513

514+
def test_from_codes_nullable_int_categories(self, any_numeric_ea_dtype):
515+
# GH#39649
516+
cats = pd.array(range(5), dtype=any_numeric_ea_dtype)
517+
codes = np.random.randint(5, size=3)
518+
dtype = CategoricalDtype(cats)
519+
arr = Categorical.from_codes(codes, dtype=dtype)
520+
assert arr.categories.dtype == cats.dtype
521+
tm.assert_index_equal(arr.categories, Index(cats))
522+
514523
def test_from_codes_empty(self):
515524
cat = ["a", "b", "c"]
516525
result = Categorical.from_codes([], categories=cat)

pandas/tests/arrays/integer/test_arithmetic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_div(dtype):
5555

5656
@pytest.mark.parametrize("zero, negative", [(0, False), (0.0, False), (-0.0, True)])
5757
def test_divide_by_zero(zero, negative):
58-
# https://github.com/pandas-dev/pandas/issues/27398
58+
# https://github.com/pandas-dev/pandas/issues/27398, GH#22793
5959
a = pd.array([0, 1, -1, None], dtype="Int64")
6060
result = a / zero
6161
expected = FloatingArray(

pandas/tests/indexing/test_loc.py

+20
Original file line numberDiff line numberDiff line change
@@ -2714,6 +2714,26 @@ def test_loc_getitem_multiindex_tuple_level():
27142714
assert result2 == 6
27152715

27162716

2717+
def test_loc_getitem_nullable_index_with_duplicates():
2718+
# GH#34497
2719+
df = DataFrame(
2720+
data=np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, np.nan, np.nan]]).T,
2721+
columns=["a", "b", "c"],
2722+
dtype="Int64",
2723+
)
2724+
df2 = df.set_index("c")
2725+
assert df2.index.dtype == "Int64"
2726+
2727+
res = df2.loc[1]
2728+
expected = Series([1, 5], index=df2.columns, dtype="Int64", name=1)
2729+
tm.assert_series_equal(res, expected)
2730+
2731+
# pd.NA and duplicates in an object-dtype Index
2732+
df2.index = df2.index.astype(object)
2733+
res = df2.loc[1]
2734+
tm.assert_series_equal(res, expected)
2735+
2736+
27172737
class TestLocSeries:
27182738
@pytest.mark.parametrize("val,expected", [(2 ** 63 - 1, 3), (2 ** 63, 4)])
27192739
def test_loc_uint64(self, val, expected):

0 commit comments

Comments
 (0)