test_constructors.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import numpy as np
  2. import pytest
  3. from pandas._libs.tslibs import iNaT
  4. from pandas._libs.tslibs.period import IncompatibleFrequency
  5. import pandas as pd
  6. import pandas._testing as tm
  7. from pandas.core.arrays import (
  8. PeriodArray,
  9. period_array,
  10. )
  11. @pytest.mark.parametrize(
  12. "data, freq, expected",
  13. [
  14. ([pd.Period("2017", "D")], None, [17167]),
  15. ([pd.Period("2017", "D")], "D", [17167]),
  16. ([2017], "D", [17167]),
  17. (["2017"], "D", [17167]),
  18. ([pd.Period("2017", "D")], pd.tseries.offsets.Day(), [17167]),
  19. ([pd.Period("2017", "D"), None], None, [17167, iNaT]),
  20. (pd.Series(pd.date_range("2017", periods=3)), None, [17167, 17168, 17169]),
  21. (pd.date_range("2017", periods=3), None, [17167, 17168, 17169]),
  22. (pd.period_range("2017", periods=4, freq="Q"), None, [188, 189, 190, 191]),
  23. ],
  24. )
  25. def test_period_array_ok(data, freq, expected):
  26. result = period_array(data, freq=freq).asi8
  27. expected = np.asarray(expected, dtype=np.int64)
  28. tm.assert_numpy_array_equal(result, expected)
  29. def test_period_array_readonly_object():
  30. # https://github.com/pandas-dev/pandas/issues/25403
  31. pa = period_array([pd.Period("2019-01-01")])
  32. arr = np.asarray(pa, dtype="object")
  33. arr.setflags(write=False)
  34. result = period_array(arr)
  35. tm.assert_period_array_equal(result, pa)
  36. result = pd.Series(arr)
  37. tm.assert_series_equal(result, pd.Series(pa))
  38. result = pd.DataFrame({"A": arr})
  39. tm.assert_frame_equal(result, pd.DataFrame({"A": pa}))
  40. def test_from_datetime64_freq_changes():
  41. # https://github.com/pandas-dev/pandas/issues/23438
  42. arr = pd.date_range("2017", periods=3, freq="D")
  43. result = PeriodArray._from_datetime64(arr, freq="M")
  44. expected = period_array(["2017-01-01", "2017-01-01", "2017-01-01"], freq="M")
  45. tm.assert_period_array_equal(result, expected)
  46. @pytest.mark.parametrize(
  47. "data, freq, msg",
  48. [
  49. (
  50. [pd.Period("2017", "D"), pd.Period("2017", "A")],
  51. None,
  52. "Input has different freq",
  53. ),
  54. ([pd.Period("2017", "D")], "A", "Input has different freq"),
  55. ],
  56. )
  57. def test_period_array_raises(data, freq, msg):
  58. with pytest.raises(IncompatibleFrequency, match=msg):
  59. period_array(data, freq)
  60. def test_period_array_non_period_series_raies():
  61. ser = pd.Series([1, 2, 3])
  62. with pytest.raises(TypeError, match="dtype"):
  63. PeriodArray(ser, freq="D")
  64. def test_period_array_freq_mismatch():
  65. arr = period_array(["2000", "2001"], freq="D")
  66. with pytest.raises(IncompatibleFrequency, match="freq"):
  67. PeriodArray(arr, freq="M")
  68. with pytest.raises(IncompatibleFrequency, match="freq"):
  69. PeriodArray(arr, freq=pd.tseries.offsets.MonthEnd())
  70. def test_from_sequence_disallows_i8():
  71. arr = period_array(["2000", "2001"], freq="D")
  72. msg = str(arr[0].ordinal)
  73. with pytest.raises(TypeError, match=msg):
  74. PeriodArray._from_sequence(arr.asi8, dtype=arr.dtype)
  75. with pytest.raises(TypeError, match=msg):
  76. PeriodArray._from_sequence(list(arr.asi8), dtype=arr.dtype)
  77. def test_from_td64nat_sequence_raises():
  78. # GH#44507
  79. td = pd.NaT.to_numpy("m8[ns]")
  80. dtype = pd.period_range("2005-01-01", periods=3, freq="D").dtype
  81. arr = np.array([None], dtype=object)
  82. arr[0] = td
  83. msg = "Value must be Period, string, integer, or datetime"
  84. with pytest.raises(ValueError, match=msg):
  85. PeriodArray._from_sequence(arr, dtype=dtype)
  86. with pytest.raises(ValueError, match=msg):
  87. pd.PeriodIndex(arr, dtype=dtype)
  88. with pytest.raises(ValueError, match=msg):
  89. pd.Index(arr, dtype=dtype)
  90. with pytest.raises(ValueError, match=msg):
  91. pd.array(arr, dtype=dtype)
  92. with pytest.raises(ValueError, match=msg):
  93. pd.Series(arr, dtype=dtype)
  94. with pytest.raises(ValueError, match=msg):
  95. pd.DataFrame(arr, dtype=dtype)