@@ -19,13 +19,16 @@ def test_from_sequence_invalid_type(self):
19
19
def test_only_1dim_accepted (self ):
20
20
arr = np .array ([0 , 1 , 2 , 3 ], dtype = "M8[h]" ).astype ("M8[ns]" )
21
21
22
- with pytest .raises (ValueError , match = "Only 1-dimensional" ):
23
- # 3-dim, we allow 2D to sneak in for ops purposes GH#29853
24
- DatetimeArray (arr .reshape (2 , 2 , 1 ))
22
+ depr_msg = "DatetimeArray.__init__ is deprecated"
23
+ with tm .assert_produces_warning (FutureWarning , match = depr_msg ):
24
+ with pytest .raises (ValueError , match = "Only 1-dimensional" ):
25
+ # 3-dim, we allow 2D to sneak in for ops purposes GH#29853
26
+ DatetimeArray (arr .reshape (2 , 2 , 1 ))
25
27
26
- with pytest .raises (ValueError , match = "Only 1-dimensional" ):
27
- # 0-dim
28
- DatetimeArray (arr [[0 ]].squeeze ())
28
+ with tm .assert_produces_warning (FutureWarning , match = depr_msg ):
29
+ with pytest .raises (ValueError , match = "Only 1-dimensional" ):
30
+ # 0-dim
31
+ DatetimeArray (arr [[0 ]].squeeze ())
29
32
30
33
def test_freq_validation (self ):
31
34
# GH#24623 check that invalid instances cannot be created with the
@@ -36,8 +39,10 @@ def test_freq_validation(self):
36
39
"Inferred frequency h from passed values does not "
37
40
"conform to passed frequency W-SUN"
38
41
)
39
- with pytest .raises (ValueError , match = msg ):
40
- DatetimeArray (arr , freq = "W" )
42
+ depr_msg = "DatetimeArray.__init__ is deprecated"
43
+ with tm .assert_produces_warning (FutureWarning , match = depr_msg ):
44
+ with pytest .raises (ValueError , match = msg ):
45
+ DatetimeArray (arr , freq = "W" )
41
46
42
47
@pytest .mark .parametrize (
43
48
"meth" ,
@@ -72,31 +77,40 @@ def test_from_pandas_array(self):
72
77
tm .assert_datetime_array_equal (result , expected )
73
78
74
79
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
- )
80
+ depr_msg = "DatetimeArray.__init__ is deprecated"
81
+ with tm .assert_produces_warning (FutureWarning , match = depr_msg ):
82
+ arr = DatetimeArray (
83
+ np .array (["2000-01-01T06:00:00" ], dtype = "M8[ns]" ),
84
+ dtype = DatetimeTZDtype (tz = "US/Central" ),
85
+ )
79
86
dtype = DatetimeTZDtype (tz = "US/Eastern" )
80
87
msg = r"dtype=datetime64\[ns.*\] does not match data dtype datetime64\[ns.*\]"
81
- with pytest .raises (TypeError , match = msg ):
82
- DatetimeArray (arr , dtype = dtype )
88
+ with tm .assert_produces_warning (FutureWarning , match = depr_msg ):
89
+ with pytest .raises (TypeError , match = msg ):
90
+ DatetimeArray (arr , dtype = dtype )
83
91
84
92
# also with mismatched tzawareness
85
- with pytest .raises (TypeError , match = msg ):
86
- DatetimeArray (arr , dtype = np .dtype ("M8[ns]" ))
87
- with pytest .raises (TypeError , match = msg ):
88
- DatetimeArray (arr .tz_localize (None ), dtype = arr .dtype )
93
+ with tm .assert_produces_warning (FutureWarning , match = depr_msg ):
94
+ with pytest .raises (TypeError , match = msg ):
95
+ DatetimeArray (arr , dtype = np .dtype ("M8[ns]" ))
96
+ with tm .assert_produces_warning (FutureWarning , match = depr_msg ):
97
+ with pytest .raises (TypeError , match = msg ):
98
+ DatetimeArray (arr .tz_localize (None ), dtype = arr .dtype )
89
99
90
100
def test_non_array_raises (self ):
91
- with pytest .raises (ValueError , match = "list" ):
92
- DatetimeArray ([1 , 2 , 3 ])
101
+ depr_msg = "DatetimeArray.__init__ is deprecated"
102
+ with tm .assert_produces_warning (FutureWarning , match = depr_msg ):
103
+ with pytest .raises (ValueError , match = "list" ):
104
+ DatetimeArray ([1 , 2 , 3 ])
93
105
94
106
def test_bool_dtype_raises (self ):
95
107
arr = np .array ([1 , 2 , 3 ], dtype = "bool" )
96
108
109
+ depr_msg = "DatetimeArray.__init__ is deprecated"
97
110
msg = "Unexpected value for 'dtype': 'bool'. Must be"
98
- with pytest .raises (ValueError , match = msg ):
99
- DatetimeArray (arr )
111
+ with tm .assert_produces_warning (FutureWarning , match = depr_msg ):
112
+ with pytest .raises (ValueError , match = msg ):
113
+ DatetimeArray (arr )
100
114
101
115
msg = r"dtype bool cannot be converted to datetime64\[ns\]"
102
116
with pytest .raises (TypeError , match = msg ):
@@ -109,43 +123,52 @@ def test_bool_dtype_raises(self):
109
123
pd .to_datetime (arr )
110
124
111
125
def test_incorrect_dtype_raises (self ):
112
- with pytest .raises (ValueError , match = "Unexpected value for 'dtype'." ):
113
- DatetimeArray (np .array ([1 , 2 , 3 ], dtype = "i8" ), dtype = "category" )
126
+ depr_msg = "DatetimeArray.__init__ is deprecated"
127
+ with tm .assert_produces_warning (FutureWarning , match = depr_msg ):
128
+ with pytest .raises (ValueError , match = "Unexpected value for 'dtype'." ):
129
+ DatetimeArray (np .array ([1 , 2 , 3 ], dtype = "i8" ), dtype = "category" )
114
130
115
- with pytest .raises (ValueError , match = "Unexpected value for 'dtype'." ):
116
- DatetimeArray (np .array ([1 , 2 , 3 ], dtype = "i8" ), dtype = "m8[s]" )
131
+ with tm .assert_produces_warning (FutureWarning , match = depr_msg ):
132
+ with pytest .raises (ValueError , match = "Unexpected value for 'dtype'." ):
133
+ DatetimeArray (np .array ([1 , 2 , 3 ], dtype = "i8" ), dtype = "m8[s]" )
117
134
118
- with pytest .raises (ValueError , match = "Unexpected value for 'dtype'." ):
119
- DatetimeArray (np .array ([1 , 2 , 3 ], dtype = "i8" ), dtype = "M8[D]" )
135
+ with tm .assert_produces_warning (FutureWarning , match = depr_msg ):
136
+ with pytest .raises (ValueError , match = "Unexpected value for 'dtype'." ):
137
+ DatetimeArray (np .array ([1 , 2 , 3 ], dtype = "i8" ), dtype = "M8[D]" )
120
138
121
139
def test_mismatched_values_dtype_units (self ):
122
140
arr = np .array ([1 , 2 , 3 ], dtype = "M8[s]" )
123
141
dtype = np .dtype ("M8[ns]" )
124
142
msg = "Values resolution does not match dtype."
143
+ depr_msg = "DatetimeArray.__init__ is deprecated"
125
144
126
- with pytest .raises (ValueError , match = msg ):
127
- DatetimeArray (arr , dtype = dtype )
145
+ with tm .assert_produces_warning (FutureWarning , match = depr_msg ):
146
+ with pytest .raises (ValueError , match = msg ):
147
+ DatetimeArray (arr , dtype = dtype )
128
148
129
149
dtype2 = DatetimeTZDtype (tz = "UTC" , unit = "ns" )
130
- with pytest .raises (ValueError , match = msg ):
131
- DatetimeArray (arr , dtype = dtype2 )
150
+ with tm .assert_produces_warning (FutureWarning , match = depr_msg ):
151
+ with pytest .raises (ValueError , match = msg ):
152
+ DatetimeArray (arr , dtype = dtype2 )
132
153
133
154
def test_freq_infer_raises (self ):
134
- with pytest .raises (ValueError , match = "Frequency inference" ):
135
- DatetimeArray (np .array ([1 , 2 , 3 ], dtype = "i8" ), freq = "infer" )
155
+ depr_msg = "DatetimeArray.__init__ is deprecated"
156
+ with tm .assert_produces_warning (FutureWarning , match = depr_msg ):
157
+ with pytest .raises (ValueError , match = "Frequency inference" ):
158
+ DatetimeArray (np .array ([1 , 2 , 3 ], dtype = "i8" ), freq = "infer" )
136
159
137
160
def test_copy (self ):
138
161
data = np .array ([1 , 2 , 3 ], dtype = "M8[ns]" )
139
- arr = DatetimeArray (data , copy = False )
162
+ arr = DatetimeArray . _from_sequence (data , copy = False )
140
163
assert arr ._ndarray is data
141
164
142
- arr = DatetimeArray (data , copy = True )
165
+ arr = DatetimeArray . _from_sequence (data , copy = True )
143
166
assert arr ._ndarray is not data
144
167
145
168
@pytest .mark .parametrize ("unit" , ["s" , "ms" , "us" , "ns" ])
146
169
def test_numpy_datetime_unit (self , unit ):
147
170
data = np .array ([1 , 2 , 3 ], dtype = f"M8[{ unit } ]" )
148
- arr = DatetimeArray (data )
171
+ arr = DatetimeArray . _from_sequence (data )
149
172
assert arr .unit == unit
150
173
assert arr [0 ].unit == unit
151
174
@@ -210,7 +233,7 @@ def test_from_arrowtest_from_arrow_with_different_units_and_timezones_with_(
210
233
dtype = DatetimeTZDtype (unit = pd_unit , tz = pd_tz )
211
234
212
235
result = dtype .__from_arrow__ (arr )
213
- expected = DatetimeArray (
236
+ expected = DatetimeArray . _from_sequence (
214
237
np .array (data , dtype = f"datetime64[{ pa_unit } ]" ).astype (f"datetime64[{ pd_unit } ]" ),
215
238
dtype = dtype ,
216
239
)
@@ -238,7 +261,7 @@ def test_from_arrow_from_empty(unit, tz):
238
261
dtype = DatetimeTZDtype (unit = unit , tz = tz )
239
262
240
263
result = dtype .__from_arrow__ (arr )
241
- expected = DatetimeArray (np .array (data , dtype = f"datetime64[{ unit } ]" ))
264
+ expected = DatetimeArray . _from_sequence (np .array (data , dtype = f"datetime64[{ unit } ]" ))
242
265
expected = expected .tz_localize (tz = tz )
243
266
tm .assert_extension_array_equal (result , expected )
244
267
@@ -254,7 +277,7 @@ def test_from_arrow_from_integers():
254
277
dtype = DatetimeTZDtype (unit = "ns" , tz = "UTC" )
255
278
256
279
result = dtype .__from_arrow__ (arr )
257
- expected = DatetimeArray (np .array (data , dtype = "datetime64[ns]" ))
280
+ expected = DatetimeArray . _from_sequence (np .array (data , dtype = "datetime64[ns]" ))
258
281
expected = expected .tz_localize ("UTC" )
259
282
tm .assert_extension_array_equal (result , expected )
260
283
0 commit comments