Skip to content

REF: fillna tests #33183

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 5 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 0 additions & 12 deletions pandas/tests/indexes/categorical/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,18 +442,6 @@ def test_frame_repr(self):
expected = " A\na 1\nb 2\nc 3"
assert result == expected

def test_fillna_categorical(self):
# GH 11343
idx = CategoricalIndex([1.0, np.nan, 3.0, 1.0], name="x")
# fill by value in categories
exp = CategoricalIndex([1.0, 1.0, 3.0, 1.0], name="x")
tm.assert_index_equal(idx.fillna(1.0), exp)

# fill by value not in categories raises ValueError
msg = "fill value must be in categories"
with pytest.raises(ValueError, match=msg):
idx.fillna(2.0)

@pytest.mark.parametrize(
"dtype, engine_type",
[
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/indexes/categorical/test_fillna.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import numpy as np
import pytest

from pandas import CategoricalIndex
import pandas._testing as tm


class TestFillNA:
def test_fillna_categorical(self):
# GH#11343
idx = CategoricalIndex([1.0, np.nan, 3.0, 1.0], name="x")
# fill by value in categories
exp = CategoricalIndex([1.0, 1.0, 3.0, 1.0], name="x")
tm.assert_index_equal(idx.fillna(1.0), exp)

# fill by value not in categories raises ValueError
msg = "fill value must be in categories"
with pytest.raises(ValueError, match=msg):
idx.fillna(2.0)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pandas._testing as tm


class TestDatetimeIndex:
class TestDatetimeIndexFillNA:
@pytest.mark.parametrize("tz", ["US/Eastern", "Asia/Tokyo"])
def test_fillna_datetime64(self, tz):
# GH 11343
Expand Down
47 changes: 4 additions & 43 deletions pandas/tests/indexes/multi/test_missing.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,16 @@
import numpy as np
import pytest

from pandas._libs.tslib import iNaT

import pandas as pd
from pandas import Int64Index, MultiIndex, PeriodIndex, UInt64Index
from pandas import MultiIndex
import pandas._testing as tm
from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin


def test_fillna(idx):
# GH 11343

# TODO: Remove or Refactor. Not Implemented for MultiIndex
for name, index in [("idx", idx)]:
if len(index) == 0:
pass
elif isinstance(index, MultiIndex):
idx = index.copy()
msg = "isna is not defined for MultiIndex"
with pytest.raises(NotImplementedError, match=msg):
idx.fillna(idx[0])
else:
idx = index.copy()
result = idx.fillna(idx[0])
tm.assert_index_equal(result, idx)
assert result is not idx

msg = "'value' must be a scalar, passed: "
with pytest.raises(TypeError, match=msg):
idx.fillna([idx[0]])

idx = index.copy()
values = idx.values

if isinstance(index, DatetimeIndexOpsMixin):
values[1] = iNaT
elif isinstance(index, (Int64Index, UInt64Index)):
continue
else:
values[1] = np.nan

if isinstance(index, PeriodIndex):
idx = type(index)(values, freq=index.freq)
else:
idx = type(index)(values)

expected = np.array([False] * len(idx), dtype=bool)
expected[1] = True
tm.assert_numpy_array_equal(idx._isnan, expected)
assert idx.hasnans is True
msg = "isna is not defined for MultiIndex"
with pytest.raises(NotImplementedError, match=msg):
idx.fillna(idx[0])


def test_dropna():
Expand Down
36 changes: 36 additions & 0 deletions pandas/tests/indexes/period/test_fillna.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from pandas import Index, NaT, Period, PeriodIndex
import pandas._testing as tm


class TestFillNA:
def test_fillna_period(self):
# GH#11343
idx = PeriodIndex(["2011-01-01 09:00", NaT, "2011-01-01 11:00"], freq="H")

exp = PeriodIndex(
["2011-01-01 09:00", "2011-01-01 10:00", "2011-01-01 11:00"], freq="H"
)
result = idx.fillna(Period("2011-01-01 10:00", freq="H"))
tm.assert_index_equal(result, exp)

exp = Index(
[
Period("2011-01-01 09:00", freq="H"),
"x",
Period("2011-01-01 11:00", freq="H"),
],
dtype=object,
)
result = idx.fillna("x")
tm.assert_index_equal(result, exp)

exp = Index(
[
Period("2011-01-01 09:00", freq="H"),
Period("2011-01-01", freq="D"),
Period("2011-01-01 11:00", freq="H"),
],
dtype=object,
)
result = idx.fillna(Period("2011-01-01", freq="D"))
tm.assert_index_equal(result, exp)
29 changes: 0 additions & 29 deletions pandas/tests/indexes/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,35 +67,6 @@ def test_repeat_freqstr(self, index, use_numpy):
tm.assert_index_equal(result, expected)
assert result.freqstr == index.freqstr

def test_fillna_period(self):
# GH 11343
idx = PeriodIndex(["2011-01-01 09:00", NaT, "2011-01-01 11:00"], freq="H")

exp = PeriodIndex(
["2011-01-01 09:00", "2011-01-01 10:00", "2011-01-01 11:00"], freq="H"
)
tm.assert_index_equal(idx.fillna(Period("2011-01-01 10:00", freq="H")), exp)

exp = Index(
[
Period("2011-01-01 09:00", freq="H"),
"x",
Period("2011-01-01 11:00", freq="H"),
],
dtype=object,
)
tm.assert_index_equal(idx.fillna("x"), exp)

exp = Index(
[
Period("2011-01-01 09:00", freq="H"),
Period("2011-01-01", freq="D"),
Period("2011-01-01 11:00", freq="H"),
],
dtype=object,
)
tm.assert_index_equal(idx.fillna(Period("2011-01-01", freq="D")), exp)

def test_no_millisecond_field(self):
msg = "type object 'DatetimeIndex' has no attribute 'millisecond'"
with pytest.raises(AttributeError, match=msg):
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/indexes/timedeltas/test_fillna.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from pandas import Index, NaT, Timedelta, TimedeltaIndex
import pandas._testing as tm


class TestFillNA:
def test_fillna_timedelta(self):
# GH#11343
idx = TimedeltaIndex(["1 day", NaT, "3 day"])

exp = TimedeltaIndex(["1 day", "2 day", "3 day"])
tm.assert_index_equal(idx.fillna(Timedelta("2 day")), exp)

exp = TimedeltaIndex(["1 day", "3 hour", "3 day"])
idx.fillna(Timedelta("3 hour"))

exp = Index([Timedelta("1 day"), "x", Timedelta("3 day")], dtype=object)
tm.assert_index_equal(idx.fillna("x"), exp)
15 changes: 0 additions & 15 deletions pandas/tests/indexes/timedeltas/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,6 @@ def test_shift(self):
def test_pickle_compat_construction(self):
pass

def test_fillna_timedelta(self):
# GH 11343
idx = pd.TimedeltaIndex(["1 day", pd.NaT, "3 day"])

exp = pd.TimedeltaIndex(["1 day", "2 day", "3 day"])
tm.assert_index_equal(idx.fillna(pd.Timedelta("2 day")), exp)

exp = pd.TimedeltaIndex(["1 day", "3 hour", "3 day"])
idx.fillna(pd.Timedelta("3 hour"))

exp = pd.Index(
[pd.Timedelta("1 day"), "x", pd.Timedelta("3 day")], dtype=object
)
tm.assert_index_equal(idx.fillna("x"), exp)

def test_isin(self):

index = tm.makeTimedeltaIndex(4)
Expand Down
5 changes: 1 addition & 4 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,11 +629,8 @@ def test_timedelta_assignment():
s = s.reindex(s.index.insert(0, "A"))
tm.assert_series_equal(s, Series([np.nan, Timedelta("1 days")], index=["A", "B"]))

result = s.fillna(timedelta(1))
expected = Series(Timedelta("1 days"), index=["A", "B"])
tm.assert_series_equal(result, expected)

s.loc["A"] = timedelta(1)
expected = Series(Timedelta("1 days"), index=["A", "B"])
tm.assert_series_equal(s, expected)

# GH 14155
Expand Down
Loading