test_fillna.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import pytest
  2. import pandas as pd
  3. import pandas._testing as tm
  4. class TestDatetimeIndexFillNA:
  5. @pytest.mark.parametrize("tz", ["US/Eastern", "Asia/Tokyo"])
  6. def test_fillna_datetime64(self, tz):
  7. # GH 11343
  8. idx = pd.DatetimeIndex(["2011-01-01 09:00", pd.NaT, "2011-01-01 11:00"])
  9. exp = pd.DatetimeIndex(
  10. ["2011-01-01 09:00", "2011-01-01 10:00", "2011-01-01 11:00"]
  11. )
  12. tm.assert_index_equal(idx.fillna(pd.Timestamp("2011-01-01 10:00")), exp)
  13. # tz mismatch
  14. exp = pd.Index(
  15. [
  16. pd.Timestamp("2011-01-01 09:00"),
  17. pd.Timestamp("2011-01-01 10:00", tz=tz),
  18. pd.Timestamp("2011-01-01 11:00"),
  19. ],
  20. dtype=object,
  21. )
  22. tm.assert_index_equal(idx.fillna(pd.Timestamp("2011-01-01 10:00", tz=tz)), exp)
  23. # object
  24. exp = pd.Index(
  25. [pd.Timestamp("2011-01-01 09:00"), "x", pd.Timestamp("2011-01-01 11:00")],
  26. dtype=object,
  27. )
  28. tm.assert_index_equal(idx.fillna("x"), exp)
  29. idx = pd.DatetimeIndex(["2011-01-01 09:00", pd.NaT, "2011-01-01 11:00"], tz=tz)
  30. exp = pd.DatetimeIndex(
  31. ["2011-01-01 09:00", "2011-01-01 10:00", "2011-01-01 11:00"], tz=tz
  32. )
  33. tm.assert_index_equal(idx.fillna(pd.Timestamp("2011-01-01 10:00", tz=tz)), exp)
  34. exp = pd.Index(
  35. [
  36. pd.Timestamp("2011-01-01 09:00", tz=tz),
  37. pd.Timestamp("2011-01-01 10:00"),
  38. pd.Timestamp("2011-01-01 11:00", tz=tz),
  39. ],
  40. dtype=object,
  41. )
  42. tm.assert_index_equal(idx.fillna(pd.Timestamp("2011-01-01 10:00")), exp)
  43. # object
  44. exp = pd.Index(
  45. [
  46. pd.Timestamp("2011-01-01 09:00", tz=tz),
  47. "x",
  48. pd.Timestamp("2011-01-01 11:00", tz=tz),
  49. ],
  50. dtype=object,
  51. )
  52. tm.assert_index_equal(idx.fillna("x"), exp)