@@ -662,12 +662,53 @@ def view(self, dtype=None):
662
662
# ----------------------------------------------------------------------
663
663
# NDArray Compat
664
664
665
- def __array__ (self , result = None ):
665
+ def __array__ (self , dtype = None ):
666
666
"""
667
- The array interface, return my values.
667
+ Return the values as a NumPy array.
668
+
669
+ Users should not call this directly. Rather, it is invoked by
670
+ :func:`numpy.array` and :func:`numpy.asarray`.
671
+
672
+ Parameters
673
+ ----------
674
+ dtype : str or numpy.dtype, optional
675
+ The dtype to use for the resulting NumPy array. By default,
676
+ the dtype is inferred from the data.
677
+
678
+ Returns
679
+ -------
680
+ numpy.ndarray
681
+ The values in the series converted to a :class:`numpy.ndarary`
682
+ with the specified `dtype`.
683
+
684
+ See Also
685
+ --------
686
+ Series.array : Zero-copy view to the array backing the Series.
687
+ Series.to_numpy : Series method for similar behavior.
688
+
689
+ Examples
690
+ --------
691
+ >>> ser = pd.Series([1, 2, 3])
692
+ >>> np.asarray(ser)
693
+ array([1, 2, 3])
694
+
695
+ For timezone-aware data, the timezones may be retained with
696
+ ``dtype='object'``
697
+
698
+ >>> tzser = pd.Series(pd.date_range('2000', periods=2, tz="CET"))
699
+ >>> np.asarray(tzser, dtype="object")
700
+ array([Timestamp('2000-01-01 00:00:00+0100', tz='CET', freq='D'),
701
+ Timestamp('2000-01-02 00:00:00+0100', tz='CET', freq='D')],
702
+ dtype=object)
703
+
704
+ Or the values may be localized to UTC and the tzinfo discared with
705
+ ``dtype='datetime64[ns]'``
706
+
707
+ >>> np.asarray(tzser, dtype="datetime64[ns]") # doctest: +ELLIPSIS
708
+ array(['1999-12-31T23:00:00.000000000', ...],
709
+ dtype='datetime64[ns]')
668
710
"""
669
- # TODO: change the keyword name from result to dtype?
670
- if (result is None and isinstance (self .array , ABCDatetimeArray )
711
+ if (dtype is None and isinstance (self .array , ABCDatetimeArray )
671
712
and getattr (self .dtype , 'tz' , None )):
672
713
msg = (
673
714
"Converting timezone-aware DatetimeArray to timezone-naive "
@@ -678,8 +719,8 @@ def __array__(self, result=None):
678
719
"To keep the old behavior, pass 'dtype=\" datetime64[ns]\" '."
679
720
)
680
721
warnings .warn (msg , FutureWarning , stacklevel = 3 )
681
- result = 'M8[ns]'
682
- return np .asarray (self .array , result )
722
+ dtype = 'M8[ns]'
723
+ return np .asarray (self .array , dtype )
683
724
684
725
def __array_wrap__ (self , result , context = None ):
685
726
"""
0 commit comments