|
18 | 18 | is_object_dtype,
|
19 | 19 | needs_i8_conversion,
|
20 | 20 | )
|
| 21 | +from pandas.core.dtypes.generic import ABCMultiIndex |
21 | 22 |
|
22 | 23 | import pandas as pd
|
23 | 24 | from pandas import (
|
@@ -694,58 +695,65 @@ def test_drop_duplicates_series_vs_dataframe(self):
|
694 | 695 | dropped_series = df[column].drop_duplicates(keep=keep)
|
695 | 696 | tm.assert_frame_equal(dropped_frame, dropped_series.to_frame())
|
696 | 697 |
|
697 |
| - def test_fillna(self): |
| 698 | + def test_fillna(self, index_or_series_obj): |
698 | 699 | # # GH 11343
|
699 | 700 | # though Index.fillna and Series.fillna has separate impl,
|
700 | 701 | # test here to confirm these works as the same
|
701 | 702 |
|
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) |
720 | 714 |
|
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 |
723 | 720 |
|
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) |
725 | 728 |
|
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}'") |
733 | 735 |
|
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 |
735 | 745 |
|
736 |
| - expected = klass(expected, dtype=orig.dtype) |
737 |
| - o = klass(values) |
| 746 | + expected = klass(expected) |
| 747 | + obj = klass(values) |
738 | 748 |
|
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) |
741 | 754 |
|
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 |
749 | 757 |
|
750 | 758 | @pytest.mark.skipif(PYPY, reason="not relevant for PyPy")
|
751 | 759 | def test_memory_usage(self, index_or_series_obj):
|
|
0 commit comments