@@ -240,6 +240,48 @@ def test_round(self, tz_naive_fixture):
240
240
expected = dti - pd .Timedelta (minutes = 1 )
241
241
tm .assert_index_equal (result , expected )
242
242
243
+ def test_array_interface (self , datetime_index ):
244
+ arr = DatetimeArray (datetime_index )
245
+
246
+ # default asarray gives the same underlying data (for tz naive)
247
+ result = np .asarray (arr )
248
+ expected = arr ._data
249
+ assert result is expected
250
+ tm .assert_numpy_array_equal (result , expected )
251
+ result = np .array (arr , copy = False )
252
+ assert result is expected
253
+ tm .assert_numpy_array_equal (result , expected )
254
+
255
+ # specifying M8[ns] gives the same result as default
256
+ result = np .asarray (arr , dtype = 'datetime64[ns]' )
257
+ expected = arr ._data
258
+ assert result is expected
259
+ tm .assert_numpy_array_equal (result , expected )
260
+ result = np .array (arr , dtype = 'datetime64[ns]' , copy = False )
261
+ assert result is expected
262
+ tm .assert_numpy_array_equal (result , expected )
263
+ result = np .array (arr , dtype = 'datetime64[ns]' )
264
+ assert result is not expected
265
+ tm .assert_numpy_array_equal (result , expected )
266
+
267
+ # to object dtype
268
+ result = np .asarray (arr , dtype = object )
269
+ expected = np .array (list (arr ), dtype = object )
270
+ tm .assert_numpy_array_equal (result , expected )
271
+
272
+ # to other dtype always copies
273
+ result = np .asarray (arr , dtype = 'int64' )
274
+ assert result is not arr .asi8
275
+ assert not np .may_share_memory (arr , result )
276
+ expected = arr .asi8 .copy ()
277
+ tm .assert_numpy_array_equal (result , expected )
278
+
279
+ # other dtypes handled by numpy
280
+ for dtype in ['float64' , str ]:
281
+ result = np .asarray (arr , dtype = dtype )
282
+ expected = np .asarray (arr ).astype (dtype )
283
+ tm .assert_numpy_array_equal (result , expected )
284
+
243
285
def test_array_object_dtype (self , tz_naive_fixture ):
244
286
# GH#23524
245
287
tz = tz_naive_fixture
@@ -255,7 +297,7 @@ def test_array_object_dtype(self, tz_naive_fixture):
255
297
result = np .array (dti , dtype = object )
256
298
tm .assert_numpy_array_equal (result , expected )
257
299
258
- def test_array (self , tz_naive_fixture ):
300
+ def test_array_tz (self , tz_naive_fixture ):
259
301
# GH#23524
260
302
tz = tz_naive_fixture
261
303
dti = pd .date_range ('2016-01-01' , periods = 3 , tz = tz )
@@ -265,13 +307,18 @@ def test_array(self, tz_naive_fixture):
265
307
result = np .array (arr , dtype = 'M8[ns]' )
266
308
tm .assert_numpy_array_equal (result , expected )
267
309
310
+ result = np .array (arr , dtype = 'datetime64[ns]' )
311
+ tm .assert_numpy_array_equal (result , expected )
312
+
268
313
# check that we are not making copies when setting copy=False
269
314
result = np .array (arr , dtype = 'M8[ns]' , copy = False )
270
315
assert result .base is expected .base
271
316
assert result .base is not None
317
+ result = np .array (arr , dtype = 'datetime64[ns]' , copy = False )
318
+ assert result .base is expected .base
319
+ assert result .base is not None
272
320
273
321
def test_array_i8_dtype (self , tz_naive_fixture ):
274
- # GH#23524
275
322
tz = tz_naive_fixture
276
323
dti = pd .date_range ('2016-01-01' , periods = 3 , tz = tz )
277
324
arr = DatetimeArray (dti )
@@ -283,10 +330,10 @@ def test_array_i8_dtype(self, tz_naive_fixture):
283
330
result = np .array (arr , dtype = np .int64 )
284
331
tm .assert_numpy_array_equal (result , expected )
285
332
286
- # check that we are not making copies when setting copy=False
333
+ # check that we are still making copies when setting copy=False
287
334
result = np .array (arr , dtype = 'i8' , copy = False )
288
- assert result .base is expected .base
289
- assert result .base is not None
335
+ assert result .base is not expected .base
336
+ assert result .base is None
290
337
291
338
def test_from_array_keeps_base (self ):
292
339
# Ensure that DatetimeArray._data.base isn't lost.
@@ -470,6 +517,48 @@ def test_int_properties(self, timedelta_index, propname):
470
517
471
518
tm .assert_numpy_array_equal (result , expected )
472
519
520
+ def test_array_interface (self , timedelta_index ):
521
+ arr = TimedeltaArray (timedelta_index )
522
+
523
+ # default asarray gives the same underlying data
524
+ result = np .asarray (arr )
525
+ expected = arr ._data
526
+ assert result is expected
527
+ tm .assert_numpy_array_equal (result , expected )
528
+ result = np .array (arr , copy = False )
529
+ assert result is expected
530
+ tm .assert_numpy_array_equal (result , expected )
531
+
532
+ # specifying m8[ns] gives the same result as default
533
+ result = np .asarray (arr , dtype = 'timedelta64[ns]' )
534
+ expected = arr ._data
535
+ assert result is expected
536
+ tm .assert_numpy_array_equal (result , expected )
537
+ result = np .array (arr , dtype = 'timedelta64[ns]' , copy = False )
538
+ assert result is expected
539
+ tm .assert_numpy_array_equal (result , expected )
540
+ result = np .array (arr , dtype = 'timedelta64[ns]' )
541
+ assert result is not expected
542
+ tm .assert_numpy_array_equal (result , expected )
543
+
544
+ # to object dtype
545
+ result = np .asarray (arr , dtype = object )
546
+ expected = np .array (list (arr ), dtype = object )
547
+ tm .assert_numpy_array_equal (result , expected )
548
+
549
+ # to other dtype always copies
550
+ result = np .asarray (arr , dtype = 'int64' )
551
+ assert result is not arr .asi8
552
+ assert not np .may_share_memory (arr , result )
553
+ expected = arr .asi8 .copy ()
554
+ tm .assert_numpy_array_equal (result , expected )
555
+
556
+ # other dtypes handled by numpy
557
+ for dtype in ['float64' , str ]:
558
+ result = np .asarray (arr , dtype = dtype )
559
+ expected = np .asarray (arr ).astype (dtype )
560
+ tm .assert_numpy_array_equal (result , expected )
561
+
473
562
def test_take_fill_valid (self , timedelta_index ):
474
563
tdi = timedelta_index
475
564
arr = TimedeltaArray (tdi )
@@ -543,3 +632,26 @@ def test_int_properties(self, period_index, propname):
543
632
expected = np .array (getattr (pi , propname ))
544
633
545
634
tm .assert_numpy_array_equal (result , expected )
635
+
636
+ def test_array_interface (self , period_index ):
637
+ arr = PeriodArray (period_index )
638
+
639
+ # default asarray gives objects
640
+ result = np .asarray (arr )
641
+ expected = np .array (list (arr ), dtype = object )
642
+ tm .assert_numpy_array_equal (result , expected )
643
+
644
+ # to object dtype (same as default)
645
+ result = np .asarray (arr , dtype = object )
646
+ tm .assert_numpy_array_equal (result , expected )
647
+
648
+ # to other dtypes
649
+ with pytest .raises (TypeError ):
650
+ np .asarray (arr , dtype = 'int64' )
651
+
652
+ with pytest .raises (TypeError ):
653
+ np .asarray (arr , dtype = 'float64' )
654
+
655
+ result = np .asarray (arr , dtype = 'S20' )
656
+ expected = np .asarray (arr ).astype ('S20' )
657
+ tm .assert_numpy_array_equal (result , expected )
0 commit comments