Skip to content

Commit cc3f3eb

Browse files
authored
REF: fillna tests (#33183)
1 parent e56d395 commit cc3f3eb

File tree

12 files changed

+460
-451
lines changed

12 files changed

+460
-451
lines changed

pandas/tests/indexes/categorical/test_category.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -442,18 +442,6 @@ def test_frame_repr(self):
442442
expected = " A\na 1\nb 2\nc 3"
443443
assert result == expected
444444

445-
def test_fillna_categorical(self):
446-
# GH 11343
447-
idx = CategoricalIndex([1.0, np.nan, 3.0, 1.0], name="x")
448-
# fill by value in categories
449-
exp = CategoricalIndex([1.0, 1.0, 3.0, 1.0], name="x")
450-
tm.assert_index_equal(idx.fillna(1.0), exp)
451-
452-
# fill by value not in categories raises ValueError
453-
msg = "fill value must be in categories"
454-
with pytest.raises(ValueError, match=msg):
455-
idx.fillna(2.0)
456-
457445
@pytest.mark.parametrize(
458446
"dtype, engine_type",
459447
[
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import CategoricalIndex
5+
import pandas._testing as tm
6+
7+
8+
class TestFillNA:
9+
def test_fillna_categorical(self):
10+
# GH#11343
11+
idx = CategoricalIndex([1.0, np.nan, 3.0, 1.0], name="x")
12+
# fill by value in categories
13+
exp = CategoricalIndex([1.0, 1.0, 3.0, 1.0], name="x")
14+
tm.assert_index_equal(idx.fillna(1.0), exp)
15+
16+
# fill by value not in categories raises ValueError
17+
msg = "fill value must be in categories"
18+
with pytest.raises(ValueError, match=msg):
19+
idx.fillna(2.0)

pandas/tests/indexes/datetimes/test_missing.py renamed to pandas/tests/indexes/datetimes/test_fillna.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pandas._testing as tm
55

66

7-
class TestDatetimeIndex:
7+
class TestDatetimeIndexFillNA:
88
@pytest.mark.parametrize("tz", ["US/Eastern", "Asia/Tokyo"])
99
def test_fillna_datetime64(self, tz):
1010
# GH 11343

pandas/tests/indexes/multi/test_missing.py

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,16 @@
11
import numpy as np
22
import pytest
33

4-
from pandas._libs.tslib import iNaT
5-
64
import pandas as pd
7-
from pandas import Int64Index, MultiIndex, PeriodIndex, UInt64Index
5+
from pandas import MultiIndex
86
import pandas._testing as tm
9-
from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin
107

118

129
def test_fillna(idx):
1310
# GH 11343
14-
15-
# TODO: Remove or Refactor. Not Implemented for MultiIndex
16-
for name, index in [("idx", idx)]:
17-
if len(index) == 0:
18-
pass
19-
elif isinstance(index, MultiIndex):
20-
idx = index.copy()
21-
msg = "isna is not defined for MultiIndex"
22-
with pytest.raises(NotImplementedError, match=msg):
23-
idx.fillna(idx[0])
24-
else:
25-
idx = index.copy()
26-
result = idx.fillna(idx[0])
27-
tm.assert_index_equal(result, idx)
28-
assert result is not idx
29-
30-
msg = "'value' must be a scalar, passed: "
31-
with pytest.raises(TypeError, match=msg):
32-
idx.fillna([idx[0]])
33-
34-
idx = index.copy()
35-
values = idx.values
36-
37-
if isinstance(index, DatetimeIndexOpsMixin):
38-
values[1] = iNaT
39-
elif isinstance(index, (Int64Index, UInt64Index)):
40-
continue
41-
else:
42-
values[1] = np.nan
43-
44-
if isinstance(index, PeriodIndex):
45-
idx = type(index)(values, freq=index.freq)
46-
else:
47-
idx = type(index)(values)
48-
49-
expected = np.array([False] * len(idx), dtype=bool)
50-
expected[1] = True
51-
tm.assert_numpy_array_equal(idx._isnan, expected)
52-
assert idx.hasnans is True
11+
msg = "isna is not defined for MultiIndex"
12+
with pytest.raises(NotImplementedError, match=msg):
13+
idx.fillna(idx[0])
5314

5415

5516
def test_dropna():
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from pandas import Index, NaT, Period, PeriodIndex
2+
import pandas._testing as tm
3+
4+
5+
class TestFillNA:
6+
def test_fillna_period(self):
7+
# GH#11343
8+
idx = PeriodIndex(["2011-01-01 09:00", NaT, "2011-01-01 11:00"], freq="H")
9+
10+
exp = PeriodIndex(
11+
["2011-01-01 09:00", "2011-01-01 10:00", "2011-01-01 11:00"], freq="H"
12+
)
13+
result = idx.fillna(Period("2011-01-01 10:00", freq="H"))
14+
tm.assert_index_equal(result, exp)
15+
16+
exp = Index(
17+
[
18+
Period("2011-01-01 09:00", freq="H"),
19+
"x",
20+
Period("2011-01-01 11:00", freq="H"),
21+
],
22+
dtype=object,
23+
)
24+
result = idx.fillna("x")
25+
tm.assert_index_equal(result, exp)
26+
27+
exp = Index(
28+
[
29+
Period("2011-01-01 09:00", freq="H"),
30+
Period("2011-01-01", freq="D"),
31+
Period("2011-01-01 11:00", freq="H"),
32+
],
33+
dtype=object,
34+
)
35+
result = idx.fillna(Period("2011-01-01", freq="D"))
36+
tm.assert_index_equal(result, exp)

pandas/tests/indexes/period/test_period.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,35 +67,6 @@ def test_repeat_freqstr(self, index, use_numpy):
6767
tm.assert_index_equal(result, expected)
6868
assert result.freqstr == index.freqstr
6969

70-
def test_fillna_period(self):
71-
# GH 11343
72-
idx = PeriodIndex(["2011-01-01 09:00", NaT, "2011-01-01 11:00"], freq="H")
73-
74-
exp = PeriodIndex(
75-
["2011-01-01 09:00", "2011-01-01 10:00", "2011-01-01 11:00"], freq="H"
76-
)
77-
tm.assert_index_equal(idx.fillna(Period("2011-01-01 10:00", freq="H")), exp)
78-
79-
exp = Index(
80-
[
81-
Period("2011-01-01 09:00", freq="H"),
82-
"x",
83-
Period("2011-01-01 11:00", freq="H"),
84-
],
85-
dtype=object,
86-
)
87-
tm.assert_index_equal(idx.fillna("x"), exp)
88-
89-
exp = Index(
90-
[
91-
Period("2011-01-01 09:00", freq="H"),
92-
Period("2011-01-01", freq="D"),
93-
Period("2011-01-01 11:00", freq="H"),
94-
],
95-
dtype=object,
96-
)
97-
tm.assert_index_equal(idx.fillna(Period("2011-01-01", freq="D")), exp)
98-
9970
def test_no_millisecond_field(self):
10071
msg = "type object 'DatetimeIndex' has no attribute 'millisecond'"
10172
with pytest.raises(AttributeError, match=msg):
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from pandas import Index, NaT, Timedelta, TimedeltaIndex
2+
import pandas._testing as tm
3+
4+
5+
class TestFillNA:
6+
def test_fillna_timedelta(self):
7+
# GH#11343
8+
idx = TimedeltaIndex(["1 day", NaT, "3 day"])
9+
10+
exp = TimedeltaIndex(["1 day", "2 day", "3 day"])
11+
tm.assert_index_equal(idx.fillna(Timedelta("2 day")), exp)
12+
13+
exp = TimedeltaIndex(["1 day", "3 hour", "3 day"])
14+
idx.fillna(Timedelta("3 hour"))
15+
16+
exp = Index([Timedelta("1 day"), "x", Timedelta("3 day")], dtype=object)
17+
tm.assert_index_equal(idx.fillna("x"), exp)

pandas/tests/indexes/timedeltas/test_timedelta.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,6 @@ def test_shift(self):
4343
def test_pickle_compat_construction(self):
4444
pass
4545

46-
def test_fillna_timedelta(self):
47-
# GH 11343
48-
idx = pd.TimedeltaIndex(["1 day", pd.NaT, "3 day"])
49-
50-
exp = pd.TimedeltaIndex(["1 day", "2 day", "3 day"])
51-
tm.assert_index_equal(idx.fillna(pd.Timedelta("2 day")), exp)
52-
53-
exp = pd.TimedeltaIndex(["1 day", "3 hour", "3 day"])
54-
idx.fillna(pd.Timedelta("3 hour"))
55-
56-
exp = pd.Index(
57-
[pd.Timedelta("1 day"), "x", pd.Timedelta("3 day")], dtype=object
58-
)
59-
tm.assert_index_equal(idx.fillna("x"), exp)
60-
6146
def test_isin(self):
6247

6348
index = tm.makeTimedeltaIndex(4)

pandas/tests/series/indexing/test_indexing.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -629,11 +629,8 @@ def test_timedelta_assignment():
629629
s = s.reindex(s.index.insert(0, "A"))
630630
tm.assert_series_equal(s, Series([np.nan, Timedelta("1 days")], index=["A", "B"]))
631631

632-
result = s.fillna(timedelta(1))
633-
expected = Series(Timedelta("1 days"), index=["A", "B"])
634-
tm.assert_series_equal(result, expected)
635-
636632
s.loc["A"] = timedelta(1)
633+
expected = Series(Timedelta("1 days"), index=["A", "B"])
637634
tm.assert_series_equal(s, expected)
638635

639636
# GH 14155

0 commit comments

Comments
 (0)