test_datetime.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from datetime import datetime
  2. import numpy as np
  3. from pandas import (
  4. DataFrame,
  5. Index,
  6. MultiIndex,
  7. Period,
  8. Series,
  9. period_range,
  10. to_datetime,
  11. )
  12. import pandas._testing as tm
  13. def test_multiindex_period_datetime():
  14. # GH4861, using datetime in period of multiindex raises exception
  15. idx1 = Index(["a", "a", "a", "b", "b"])
  16. idx2 = period_range("2012-01", periods=len(idx1), freq="M")
  17. s = Series(np.random.randn(len(idx1)), [idx1, idx2])
  18. # try Period as index
  19. expected = s.iloc[0]
  20. result = s.loc["a", Period("2012-01")]
  21. assert result == expected
  22. # try datetime as index
  23. result = s.loc["a", datetime(2012, 1, 1)]
  24. assert result == expected
  25. def test_multiindex_datetime_columns():
  26. # GH35015, using datetime as column indices raises exception
  27. mi = MultiIndex.from_tuples(
  28. [(to_datetime("02/29/2020"), to_datetime("03/01/2020"))], names=["a", "b"]
  29. )
  30. df = DataFrame([], columns=mi)
  31. expected_df = DataFrame(
  32. [],
  33. columns=MultiIndex.from_arrays(
  34. [[to_datetime("02/29/2020")], [to_datetime("03/01/2020")]], names=["a", "b"]
  35. ),
  36. )
  37. tm.assert_frame_equal(df, expected_df)