test_map.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import pytest
  2. from pandas import (
  3. DatetimeIndex,
  4. Index,
  5. MultiIndex,
  6. Period,
  7. date_range,
  8. )
  9. import pandas._testing as tm
  10. class TestMap:
  11. def test_map(self):
  12. rng = date_range("1/1/2000", periods=10)
  13. f = lambda x: x.strftime("%Y%m%d")
  14. result = rng.map(f)
  15. exp = Index([f(x) for x in rng], dtype="<U8")
  16. tm.assert_index_equal(result, exp)
  17. def test_map_fallthrough(self, capsys):
  18. # GH#22067, check we don't get warnings about silently ignored errors
  19. dti = date_range("2017-01-01", "2018-01-01", freq="B")
  20. dti.map(lambda x: Period(year=x.year, month=x.month, freq="M"))
  21. captured = capsys.readouterr()
  22. assert captured.err == ""
  23. def test_map_bug_1677(self):
  24. index = DatetimeIndex(["2012-04-25 09:30:00.393000"])
  25. f = index.asof
  26. result = index.map(f)
  27. expected = Index([f(index[0])])
  28. tm.assert_index_equal(result, expected)
  29. @pytest.mark.parametrize("name", [None, "name"])
  30. def test_index_map(self, name):
  31. # see GH#20990
  32. count = 6
  33. index = date_range("2018-01-01", periods=count, freq="M", name=name).map(
  34. lambda x: (x.year, x.month)
  35. )
  36. exp_index = MultiIndex.from_product(((2018,), range(1, 7)), names=[name, name])
  37. tm.assert_index_equal(index, exp_index)