datetimelike.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """ generic datetimelike tests """
  2. import numpy as np
  3. import pytest
  4. import pandas as pd
  5. import pandas._testing as tm
  6. from .common import Base
  7. class DatetimeLike(Base):
  8. def test_argmax_axis_invalid(self):
  9. # GH#23081
  10. rng = self.create_index()
  11. with pytest.raises(ValueError):
  12. rng.argmax(axis=1)
  13. with pytest.raises(ValueError):
  14. rng.argmin(axis=2)
  15. with pytest.raises(ValueError):
  16. rng.min(axis=-2)
  17. with pytest.raises(ValueError):
  18. rng.max(axis=-3)
  19. def test_can_hold_identifiers(self):
  20. idx = self.create_index()
  21. key = idx[0]
  22. assert idx._can_hold_identifiers_and_holds_name(key) is False
  23. def test_shift_identity(self):
  24. idx = self.create_index()
  25. tm.assert_index_equal(idx, idx.shift(0))
  26. def test_str(self):
  27. # test the string repr
  28. idx = self.create_index()
  29. idx.name = "foo"
  30. assert not "length={}".format(len(idx)) in str(idx)
  31. assert "'foo'" in str(idx)
  32. assert type(idx).__name__ in str(idx)
  33. if hasattr(idx, "tz"):
  34. if idx.tz is not None:
  35. assert idx.tz in str(idx)
  36. if hasattr(idx, "freq"):
  37. assert "freq='{idx.freqstr}'".format(idx=idx) in str(idx)
  38. def test_view(self):
  39. i = self.create_index()
  40. i_view = i.view("i8")
  41. result = self._holder(i)
  42. tm.assert_index_equal(result, i)
  43. i_view = i.view(self._holder)
  44. result = self._holder(i)
  45. tm.assert_index_equal(result, i_view)
  46. def test_map_callable(self):
  47. index = self.create_index()
  48. expected = index + index.freq
  49. result = index.map(lambda x: x + x.freq)
  50. tm.assert_index_equal(result, expected)
  51. # map to NaT
  52. result = index.map(lambda x: pd.NaT if x == index[0] else x)
  53. expected = pd.Index([pd.NaT] + index[1:].tolist())
  54. tm.assert_index_equal(result, expected)
  55. @pytest.mark.parametrize(
  56. "mapper",
  57. [
  58. lambda values, index: {i: e for e, i in zip(values, index)},
  59. lambda values, index: pd.Series(values, index, dtype=object),
  60. ],
  61. )
  62. def test_map_dictlike(self, mapper):
  63. index = self.create_index()
  64. expected = index + index.freq
  65. # don't compare the freqs
  66. if isinstance(expected, pd.DatetimeIndex):
  67. expected._data.freq = None
  68. result = index.map(mapper(expected, index))
  69. tm.assert_index_equal(result, expected)
  70. expected = pd.Index([pd.NaT] + index[1:].tolist())
  71. result = index.map(mapper(expected, index))
  72. tm.assert_index_equal(result, expected)
  73. # empty map; these map to np.nan because we cannot know
  74. # to re-infer things
  75. expected = pd.Index([np.nan] * len(index))
  76. result = index.map(mapper([], []))
  77. tm.assert_index_equal(result, expected)