|
11 | 11 |
|
12 | 12 | import pandas as pd
|
13 | 13 | from pandas.core.arrays import DatetimeArrayMixin as DatetimeArray
|
| 14 | +from pandas.core.arrays.datetimes import sequence_to_dt64ns |
14 | 15 | import pandas.util.testing as tm
|
15 | 16 |
|
16 | 17 |
|
| 18 | +class TestDatetimeArrayConstructor(object): |
| 19 | + def test_mismatched_timezone_raises(self): |
| 20 | + arr = DatetimeArray(np.array(['2000-01-01T06:00:00'], dtype='M8[ns]'), |
| 21 | + dtype=DatetimeTZDtype(tz='US/Central')) |
| 22 | + dtype = DatetimeTZDtype(tz='US/Eastern') |
| 23 | + with pytest.raises(TypeError, match='data is already tz-aware'): |
| 24 | + DatetimeArray(arr, dtype=dtype) |
| 25 | + |
| 26 | + def test_incorrect_dtype_raises(self): |
| 27 | + with pytest.raises(ValueError, match="Unexpected value for 'dtype'."): |
| 28 | + DatetimeArray(np.array([1, 2, 3], dtype='i8'), dtype='category') |
| 29 | + |
| 30 | + def test_copy(self): |
| 31 | + data = np.array([1, 2, 3], dtype='M8[ns]') |
| 32 | + arr = DatetimeArray(data, copy=False) |
| 33 | + assert arr._data is data |
| 34 | + |
| 35 | + arr = DatetimeArray(data, copy=True) |
| 36 | + assert arr._data is not data |
| 37 | + |
| 38 | + |
17 | 39 | class TestDatetimeArrayComparisons(object):
|
18 | 40 | # TODO: merge this into tests/arithmetic/test_datetime64 once it is
|
19 | 41 | # sufficiently robust
|
@@ -90,3 +112,17 @@ def test_setitem_clears_freq(self):
|
90 | 112 | tz='US/Central'))
|
91 | 113 | a[0] = pd.Timestamp("2000", tz="US/Central")
|
92 | 114 | assert a.freq is None
|
| 115 | + |
| 116 | + |
| 117 | +class TestSequenceToDT64NS(object): |
| 118 | + |
| 119 | + def test_tz_dtype_mismatch_raises(self): |
| 120 | + arr = DatetimeArray._from_sequence(['2000'], tz='US/Central') |
| 121 | + with pytest.raises(TypeError, match='data is already tz-aware'): |
| 122 | + sequence_to_dt64ns(arr, dtype=DatetimeTZDtype(tz="UTC")) |
| 123 | + |
| 124 | + def test_tz_dtype_matches(self): |
| 125 | + arr = DatetimeArray._from_sequence(['2000'], tz='US/Central') |
| 126 | + result, _, _ = sequence_to_dt64ns( |
| 127 | + arr, dtype=DatetimeTZDtype(tz="US/Central")) |
| 128 | + tm.assert_numpy_array_equal(arr._data, result) |
0 commit comments