datetimelike.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """ generic datetimelike tests """
  2. import numpy as np
  3. import pytest
  4. import pandas as pd
  5. import pandas.util.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=%s" % len(idx) in str(idx)
  31. assert "'foo'" in str(idx)
  32. assert idx.__class__.__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='%s'" % idx.freqstr 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. expected = self.index + self.index.freq
  48. result = self.index.map(lambda x: x + x.freq)
  49. tm.assert_index_equal(result, expected)
  50. # map to NaT
  51. result = self.index.map(lambda x: pd.NaT if x == self.index[0] else x)
  52. expected = pd.Index([pd.NaT] + self.index[1:].tolist())
  53. tm.assert_index_equal(result, expected)
  54. @pytest.mark.parametrize(
  55. "mapper",
  56. [
  57. lambda values, index: {i: e for e, i in zip(values, index)},
  58. lambda values, index: pd.Series(values, index)])
  59. def test_map_dictlike(self, mapper):
  60. expected = self.index + self.index.freq
  61. # don't compare the freqs
  62. if isinstance(expected, pd.DatetimeIndex):
  63. expected.freq = None
  64. result = self.index.map(mapper(expected, self.index))
  65. tm.assert_index_equal(result, expected)
  66. expected = pd.Index([pd.NaT] + self.index[1:].tolist())
  67. result = self.index.map(mapper(expected, self.index))
  68. tm.assert_index_equal(result, expected)
  69. # empty map; these map to np.nan because we cannot know
  70. # to re-infer things
  71. expected = pd.Index([np.nan] * len(self.index))
  72. result = self.index.map(mapper([], []))
  73. tm.assert_index_equal(result, expected)
  74. def test_asobject_deprecated(self):
  75. # GH18572
  76. d = self.create_index()
  77. with tm.assert_produces_warning(FutureWarning):
  78. i = d.asobject
  79. assert isinstance(i, pd.Index)