test_fillna.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """
  2. Though Index.fillna and Series.fillna has separate impl,
  3. test here to confirm these works as the same
  4. """
  5. import numpy as np
  6. import pytest
  7. from pandas import MultiIndex
  8. import pandas._testing as tm
  9. from pandas.tests.base.common import allow_na_ops
  10. def test_fillna(index_or_series_obj):
  11. # GH 11343
  12. obj = index_or_series_obj
  13. if isinstance(obj, MultiIndex):
  14. msg = "isna is not defined for MultiIndex"
  15. with pytest.raises(NotImplementedError, match=msg):
  16. obj.fillna(0)
  17. return
  18. # values will not be changed
  19. fill_value = obj.values[0] if len(obj) > 0 else 0
  20. result = obj.fillna(fill_value)
  21. tm.assert_equal(obj, result)
  22. # check shallow_copied
  23. assert obj is not result
  24. @pytest.mark.parametrize("null_obj", [np.nan, None])
  25. def test_fillna_null(null_obj, index_or_series_obj):
  26. # GH 11343
  27. obj = index_or_series_obj
  28. klass = type(obj)
  29. if not allow_na_ops(obj):
  30. pytest.skip(f"{klass} doesn't allow for NA operations")
  31. elif len(obj) < 1:
  32. pytest.skip("Test doesn't make sense on empty data")
  33. elif isinstance(obj, MultiIndex):
  34. pytest.skip(f"MultiIndex can't hold '{null_obj}'")
  35. values = obj._values
  36. fill_value = values[0]
  37. expected = values.copy()
  38. values[0:2] = null_obj
  39. expected[0:2] = fill_value
  40. expected = klass(expected)
  41. obj = klass(values)
  42. result = obj.fillna(fill_value)
  43. tm.assert_equal(result, expected)
  44. # check shallow_copied
  45. assert obj is not result