|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +from pandas.core.dtypes.dtypes import DatetimeTZDtype |
| 5 | + |
| 6 | +import pandas as pd |
| 7 | +import pandas._testing as tm |
| 8 | +from pandas.core.arrays import DatetimeArray |
| 9 | +from pandas.core.arrays.datetimes import sequence_to_dt64ns |
| 10 | + |
| 11 | + |
| 12 | +class TestDatetimeArrayConstructor: |
| 13 | + def test_from_sequence_invalid_type(self): |
| 14 | + mi = pd.MultiIndex.from_product([np.arange(5), np.arange(5)]) |
| 15 | + with pytest.raises(TypeError, match="Cannot create a DatetimeArray"): |
| 16 | + DatetimeArray._from_sequence(mi) |
| 17 | + |
| 18 | + def test_only_1dim_accepted(self): |
| 19 | + arr = np.array([0, 1, 2, 3], dtype="M8[h]").astype("M8[ns]") |
| 20 | + |
| 21 | + with pytest.raises(ValueError, match="Only 1-dimensional"): |
| 22 | + # 3-dim, we allow 2D to sneak in for ops purposes GH#29853 |
| 23 | + DatetimeArray(arr.reshape(2, 2, 1)) |
| 24 | + |
| 25 | + with pytest.raises(ValueError, match="Only 1-dimensional"): |
| 26 | + # 0-dim |
| 27 | + DatetimeArray(arr[[0]].squeeze()) |
| 28 | + |
| 29 | + def test_freq_validation(self): |
| 30 | + # GH#24623 check that invalid instances cannot be created with the |
| 31 | + # public constructor |
| 32 | + arr = np.arange(5, dtype=np.int64) * 3600 * 10 ** 9 |
| 33 | + |
| 34 | + msg = ( |
| 35 | + "Inferred frequency H from passed values does not " |
| 36 | + "conform to passed frequency W-SUN" |
| 37 | + ) |
| 38 | + with pytest.raises(ValueError, match=msg): |
| 39 | + DatetimeArray(arr, freq="W") |
| 40 | + |
| 41 | + @pytest.mark.parametrize( |
| 42 | + "meth", |
| 43 | + [ |
| 44 | + DatetimeArray._from_sequence, |
| 45 | + sequence_to_dt64ns, |
| 46 | + pd.to_datetime, |
| 47 | + pd.DatetimeIndex, |
| 48 | + ], |
| 49 | + ) |
| 50 | + def test_mixing_naive_tzaware_raises(self, meth): |
| 51 | + # GH#24569 |
| 52 | + arr = np.array([pd.Timestamp("2000"), pd.Timestamp("2000", tz="CET")]) |
| 53 | + |
| 54 | + msg = ( |
| 55 | + "Cannot mix tz-aware with tz-naive values|" |
| 56 | + "Tz-aware datetime.datetime cannot be converted " |
| 57 | + "to datetime64 unless utc=True" |
| 58 | + ) |
| 59 | + |
| 60 | + for obj in [arr, arr[::-1]]: |
| 61 | + # check that we raise regardless of whether naive is found |
| 62 | + # before aware or vice-versa |
| 63 | + with pytest.raises(ValueError, match=msg): |
| 64 | + meth(obj) |
| 65 | + |
| 66 | + def test_from_pandas_array(self): |
| 67 | + arr = pd.array(np.arange(5, dtype=np.int64)) * 3600 * 10 ** 9 |
| 68 | + |
| 69 | + result = DatetimeArray._from_sequence(arr)._with_freq("infer") |
| 70 | + |
| 71 | + expected = pd.date_range("1970-01-01", periods=5, freq="H")._data |
| 72 | + tm.assert_datetime_array_equal(result, expected) |
| 73 | + |
| 74 | + def test_mismatched_timezone_raises(self): |
| 75 | + arr = DatetimeArray( |
| 76 | + np.array(["2000-01-01T06:00:00"], dtype="M8[ns]"), |
| 77 | + dtype=DatetimeTZDtype(tz="US/Central"), |
| 78 | + ) |
| 79 | + dtype = DatetimeTZDtype(tz="US/Eastern") |
| 80 | + with pytest.raises(TypeError, match="Timezone of the array"): |
| 81 | + DatetimeArray(arr, dtype=dtype) |
| 82 | + |
| 83 | + def test_non_array_raises(self): |
| 84 | + with pytest.raises(ValueError, match="list"): |
| 85 | + DatetimeArray([1, 2, 3]) |
| 86 | + |
| 87 | + def test_bool_dtype_raises(self): |
| 88 | + arr = np.array([1, 2, 3], dtype="bool") |
| 89 | + |
| 90 | + with pytest.raises( |
| 91 | + ValueError, match="The dtype of 'values' is incorrect.*bool" |
| 92 | + ): |
| 93 | + DatetimeArray(arr) |
| 94 | + |
| 95 | + msg = r"dtype bool cannot be converted to datetime64\[ns\]" |
| 96 | + with pytest.raises(TypeError, match=msg): |
| 97 | + DatetimeArray._from_sequence(arr) |
| 98 | + |
| 99 | + with pytest.raises(TypeError, match=msg): |
| 100 | + sequence_to_dt64ns(arr) |
| 101 | + |
| 102 | + with pytest.raises(TypeError, match=msg): |
| 103 | + pd.DatetimeIndex(arr) |
| 104 | + |
| 105 | + with pytest.raises(TypeError, match=msg): |
| 106 | + pd.to_datetime(arr) |
| 107 | + |
| 108 | + def test_incorrect_dtype_raises(self): |
| 109 | + with pytest.raises(ValueError, match="Unexpected value for 'dtype'."): |
| 110 | + DatetimeArray(np.array([1, 2, 3], dtype="i8"), dtype="category") |
| 111 | + |
| 112 | + def test_freq_infer_raises(self): |
| 113 | + with pytest.raises(ValueError, match="Frequency inference"): |
| 114 | + DatetimeArray(np.array([1, 2, 3], dtype="i8"), freq="infer") |
| 115 | + |
| 116 | + def test_copy(self): |
| 117 | + data = np.array([1, 2, 3], dtype="M8[ns]") |
| 118 | + arr = DatetimeArray(data, copy=False) |
| 119 | + assert arr._data is data |
| 120 | + |
| 121 | + arr = DatetimeArray(data, copy=True) |
| 122 | + assert arr._data is not data |
| 123 | + |
| 124 | + |
| 125 | +class TestSequenceToDT64NS: |
| 126 | + def test_tz_dtype_mismatch_raises(self): |
| 127 | + arr = DatetimeArray._from_sequence( |
| 128 | + ["2000"], dtype=DatetimeTZDtype(tz="US/Central") |
| 129 | + ) |
| 130 | + with pytest.raises(TypeError, match="data is already tz-aware"): |
| 131 | + sequence_to_dt64ns(arr, dtype=DatetimeTZDtype(tz="UTC")) |
| 132 | + |
| 133 | + def test_tz_dtype_matches(self): |
| 134 | + arr = DatetimeArray._from_sequence( |
| 135 | + ["2000"], dtype=DatetimeTZDtype(tz="US/Central") |
| 136 | + ) |
| 137 | + result, _, _ = sequence_to_dt64ns(arr, dtype=DatetimeTZDtype(tz="US/Central")) |
| 138 | + tm.assert_numpy_array_equal(arr._data, result) |
| 139 | + |
| 140 | + @pytest.mark.parametrize("order", ["F", "C"]) |
| 141 | + def test_2d(self, order): |
| 142 | + dti = pd.date_range("2016-01-01", periods=6, tz="US/Pacific") |
| 143 | + arr = np.array(dti, dtype=object).reshape(3, 2) |
| 144 | + if order == "F": |
| 145 | + arr = arr.T |
| 146 | + |
| 147 | + res = sequence_to_dt64ns(arr) |
| 148 | + expected = sequence_to_dt64ns(arr.ravel()) |
| 149 | + |
| 150 | + tm.assert_numpy_array_equal(res[0].ravel(), expected[0]) |
| 151 | + assert res[1] == expected[1] |
| 152 | + assert res[2] == expected[2] |
| 153 | + |
| 154 | + res = DatetimeArray._from_sequence(arr) |
| 155 | + expected = DatetimeArray._from_sequence(arr.ravel()).reshape(arr.shape) |
| 156 | + tm.assert_datetime_array_equal(res, expected) |
0 commit comments