Skip to content

Commit 54630a8

Browse files
split and fixturized test_fillna in tests/base/test_ops.py
1 parent 8914c01 commit 54630a8

File tree

1 file changed

+45
-40
lines changed

1 file changed

+45
-40
lines changed

pandas/tests/base/test_ops.py

+45-40
Original file line numberDiff line numberDiff line change
@@ -719,58 +719,63 @@ def test_drop_duplicates_series_vs_dataframe(self):
719719
dropped_series = df[column].drop_duplicates(keep=keep)
720720
tm.assert_frame_equal(dropped_frame, dropped_series.to_frame())
721721

722-
def test_fillna(self):
722+
def test_fillna(self, index_or_series_obj):
723723
# # GH 11343
724724
# though Index.fillna and Series.fillna has separate impl,
725725
# test here to confirm these works as the same
726726

727-
for orig in self.objs:
728-
729-
o = orig.copy()
730-
values = o.values
731-
732-
# values will not be changed
733-
result = o.fillna(o.astype(object).values[0])
734-
if isinstance(o, Index):
735-
tm.assert_index_equal(o, result)
736-
else:
737-
tm.assert_series_equal(o, result)
738-
# check shallow_copied
739-
assert o is not result
727+
obj = index_or_series_obj
728+
if len(obj) == 0:
729+
pytest.skip("Test doesn't make sense on empty data")
730+
elif isinstance(obj, pd.MultiIndex):
731+
pytest.skip("MultiIndex doesn't support isna")
740732

741-
for null_obj in [np.nan, None]:
742-
for orig in self.objs:
743-
o = orig.copy()
744-
klass = type(o)
733+
# values will not be changed
734+
result = obj.fillna(obj.values[0])
735+
if isinstance(obj, Index):
736+
tm.assert_index_equal(obj, result)
737+
else:
738+
tm.assert_series_equal(obj, result)
745739

746-
if not allow_na_ops(o):
747-
continue
740+
# check shallow_copied
741+
assert obj is not result
748742

749-
if needs_i8_conversion(o):
743+
@pytest.mark.parametrize("null_obj", [np.nan, None])
744+
def test_fillna_null(self, null_obj, index_or_series_obj):
745+
# # GH 11343
746+
# though Index.fillna and Series.fillna has separate impl,
747+
# test here to confirm these works as the same
748+
obj = index_or_series_obj
749+
klass = type(obj)
750750

751-
values = o.astype(object).values
752-
fill_value = values[0]
753-
values[0:2] = pd.NaT
754-
else:
755-
values = o.values.copy()
756-
fill_value = o.values[0]
757-
values[0:2] = null_obj
751+
if not allow_na_ops(obj):
752+
pytest.skip(f"{klass} doesn't allow for NA operations")
753+
elif len(obj) < 1:
754+
pytest.skip("Test doesn't make sense on empty data")
755+
elif isinstance(obj, pd.MultiIndex):
756+
pytest.skip(f"MultiIndex can't hold '{null_obj}'")
758757

759-
expected = [fill_value] * 2 + list(values[2:])
758+
values = obj.values
759+
fill_value = values[0]
760+
expected = values.copy()
761+
if needs_i8_conversion(obj):
762+
values[0:2] = iNaT
763+
expected[0:2] = fill_value
764+
else:
765+
values[0:2] = null_obj
766+
expected[0:2] = fill_value
760767

761-
expected = klass(expected, dtype=orig.dtype)
762-
o = klass(values)
768+
expected = klass(expected)
769+
obj = klass(values)
763770

764-
# check values has the same dtype as the original
765-
assert o.dtype == orig.dtype
771+
result = obj.fillna(fill_value)
772+
if isinstance(obj, Index):
773+
tm.assert_index_equal(result, expected)
774+
else:
775+
tm.assert_series_equal(result, expected)
766776

767-
result = o.fillna(fill_value)
768-
if isinstance(o, Index):
769-
tm.assert_index_equal(result, expected)
770-
else:
771-
tm.assert_series_equal(result, expected)
772-
# check shallow_copied
773-
assert o is not result
777+
# check shallow_copied
778+
assert obj is not result
774779

775780
@pytest.mark.skipif(PYPY, reason="not relevant for PyPy")
776781
def test_memory_usage(self, index_or_series_obj):

0 commit comments

Comments
 (0)