test_missing.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from datetime import timedelta
  2. import numpy as np
  3. import pytest
  4. from pandas._libs import iNaT
  5. import pandas as pd
  6. from pandas import (
  7. Categorical,
  8. Index,
  9. NaT,
  10. Series,
  11. isna,
  12. )
  13. import pandas._testing as tm
  14. class TestSeriesMissingData:
  15. def test_categorical_nan_handling(self):
  16. # NaNs are represented as -1 in labels
  17. s = Series(Categorical(["a", "b", np.nan, "a"]))
  18. tm.assert_index_equal(s.cat.categories, Index(["a", "b"]))
  19. tm.assert_numpy_array_equal(
  20. s.values.codes, np.array([0, 1, -1, 0], dtype=np.int8)
  21. )
  22. def test_isna_for_inf(self):
  23. s = Series(["a", np.inf, np.nan, pd.NA, 1.0])
  24. with pd.option_context("mode.use_inf_as_na", True):
  25. r = s.isna()
  26. dr = s.dropna()
  27. e = Series([False, True, True, True, False])
  28. de = Series(["a", 1.0], index=[0, 4])
  29. tm.assert_series_equal(r, e)
  30. tm.assert_series_equal(dr, de)
  31. def test_timedelta64_nan(self):
  32. td = Series([timedelta(days=i) for i in range(10)])
  33. # nan ops on timedeltas
  34. td1 = td.copy()
  35. td1[0] = np.nan
  36. assert isna(td1[0])
  37. assert td1[0]._value == iNaT
  38. td1[0] = td[0]
  39. assert not isna(td1[0])
  40. # GH#16674 iNaT is treated as an integer when given by the user
  41. td1[1] = iNaT
  42. assert not isna(td1[1])
  43. assert td1.dtype == np.object_
  44. assert td1[1] == iNaT
  45. td1[1] = td[1]
  46. assert not isna(td1[1])
  47. td1[2] = NaT
  48. assert isna(td1[2])
  49. assert td1[2]._value == iNaT
  50. td1[2] = td[2]
  51. assert not isna(td1[2])
  52. # boolean setting
  53. # GH#2899 boolean setting
  54. td3 = np.timedelta64(timedelta(days=3))
  55. td7 = np.timedelta64(timedelta(days=7))
  56. td[(td > td3) & (td < td7)] = np.nan
  57. assert isna(td).sum() == 3
  58. @pytest.mark.xfail(
  59. reason="Chained inequality raises when trying to define 'selector'"
  60. )
  61. def test_logical_range_select(self, datetime_series):
  62. # NumPy limitation =(
  63. # https://github.com/pandas-dev/pandas/commit/9030dc021f07c76809848925cb34828f6c8484f3
  64. np.random.seed(12345)
  65. selector = -0.5 <= datetime_series <= 0.5
  66. expected = (datetime_series >= -0.5) & (datetime_series <= 0.5)
  67. tm.assert_series_equal(selector, expected)
  68. def test_valid(self, datetime_series):
  69. ts = datetime_series.copy()
  70. ts.index = ts.index._with_freq(None)
  71. ts[::2] = np.NaN
  72. result = ts.dropna()
  73. assert len(result) == ts.count()
  74. tm.assert_series_equal(result, ts[1::2])
  75. tm.assert_series_equal(result, ts[pd.notna(ts)])
  76. def test_hasnans_uncached_for_series():
  77. # GH#19700
  78. idx = Index([0, 1])
  79. assert idx.hasnans is False
  80. assert "hasnans" in idx._cache
  81. ser = idx.to_series()
  82. assert ser.hasnans is False
  83. assert not hasattr(ser, "_cache")
  84. ser.iloc[-1] = np.nan
  85. assert ser.hasnans is True
  86. assert Series.hasnans.__doc__ == Index.hasnans.__doc__