Skip to content

Commit 7fa8ee7

Browse files
CLN: Split and fixturized test_fillna in tests/base/test_ops.py (#32483)
1 parent 817d57d commit 7fa8ee7

File tree

1 file changed

+49
-41
lines changed

1 file changed

+49
-41
lines changed

pandas/tests/base/test_ops.py

+49-41
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
is_object_dtype,
1919
needs_i8_conversion,
2020
)
21+
from pandas.core.dtypes.generic import ABCMultiIndex
2122

2223
import pandas as pd
2324
from pandas import (
@@ -694,58 +695,65 @@ def test_drop_duplicates_series_vs_dataframe(self):
694695
dropped_series = df[column].drop_duplicates(keep=keep)
695696
tm.assert_frame_equal(dropped_frame, dropped_series.to_frame())
696697

697-
def test_fillna(self):
698+
def test_fillna(self, index_or_series_obj):
698699
# # GH 11343
699700
# though Index.fillna and Series.fillna has separate impl,
700701
# test here to confirm these works as the same
701702

702-
for orig in self.objs:
703-
704-
o = orig.copy()
705-
values = o.values
706-
707-
# values will not be changed
708-
result = o.fillna(o.astype(object).values[0])
709-
if isinstance(o, Index):
710-
tm.assert_index_equal(o, result)
711-
else:
712-
tm.assert_series_equal(o, result)
713-
# check shallow_copied
714-
assert o is not result
715-
716-
for null_obj in [np.nan, None]:
717-
for orig in self.objs:
718-
o = orig.copy()
719-
klass = type(o)
703+
obj = index_or_series_obj
704+
if isinstance(obj, ABCMultiIndex):
705+
pytest.skip("MultiIndex doesn't support isna")
706+
707+
# values will not be changed
708+
fill_value = obj.values[0] if len(obj) > 0 else 0
709+
result = obj.fillna(fill_value)
710+
if isinstance(obj, Index):
711+
tm.assert_index_equal(obj, result)
712+
else:
713+
tm.assert_series_equal(obj, result)
720714

721-
if not allow_na_ops(o):
722-
continue
715+
# check shallow_copied
716+
if isinstance(obj, Series) and len(obj) == 0:
717+
# TODO: GH-32543
718+
pytest.xfail("Shallow copy for empty Series is bugged")
719+
assert obj is not result
723720

724-
if needs_i8_conversion(o):
721+
@pytest.mark.parametrize("null_obj", [np.nan, None])
722+
def test_fillna_null(self, null_obj, index_or_series_obj):
723+
# # GH 11343
724+
# though Index.fillna and Series.fillna has separate impl,
725+
# test here to confirm these works as the same
726+
obj = index_or_series_obj
727+
klass = type(obj)
725728

726-
values = o.astype(object).values
727-
fill_value = values[0]
728-
values[0:2] = pd.NaT
729-
else:
730-
values = o.values.copy()
731-
fill_value = o.values[0]
732-
values[0:2] = null_obj
729+
if not allow_na_ops(obj):
730+
pytest.skip(f"{klass} doesn't allow for NA operations")
731+
elif len(obj) < 1:
732+
pytest.skip("Test doesn't make sense on empty data")
733+
elif isinstance(obj, ABCMultiIndex):
734+
pytest.skip(f"MultiIndex can't hold '{null_obj}'")
733735

734-
expected = [fill_value] * 2 + list(values[2:])
736+
values = obj.values
737+
fill_value = values[0]
738+
expected = values.copy()
739+
if needs_i8_conversion(obj):
740+
values[0:2] = iNaT
741+
expected[0:2] = fill_value
742+
else:
743+
values[0:2] = null_obj
744+
expected[0:2] = fill_value
735745

736-
expected = klass(expected, dtype=orig.dtype)
737-
o = klass(values)
746+
expected = klass(expected)
747+
obj = klass(values)
738748

739-
# check values has the same dtype as the original
740-
assert o.dtype == orig.dtype
749+
result = obj.fillna(fill_value)
750+
if isinstance(obj, Index):
751+
tm.assert_index_equal(result, expected)
752+
else:
753+
tm.assert_series_equal(result, expected)
741754

742-
result = o.fillna(fill_value)
743-
if isinstance(o, Index):
744-
tm.assert_index_equal(result, expected)
745-
else:
746-
tm.assert_series_equal(result, expected)
747-
# check shallow_copied
748-
assert o is not result
755+
# check shallow_copied
756+
assert obj is not result
749757

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

0 commit comments

Comments
 (0)