|
21 | 21 | is_extension_array_dtype, is_extension_type, is_hashable, is_integer,
|
22 | 22 | is_iterator, is_list_like, is_scalar, is_string_like, is_timedelta64_dtype)
|
23 | 23 | from pandas.core.dtypes.generic import (
|
24 |
| - ABCDataFrame, ABCDatetimeIndex, ABCSeries, ABCSparseArray, ABCSparseSeries) |
| 24 | + ABCDataFrame, ABCDatetimeArray, ABCDatetimeIndex, ABCSeries, |
| 25 | + ABCSparseArray, ABCSparseSeries) |
25 | 26 | from pandas.core.dtypes.missing import (
|
26 | 27 | isna, na_value_for_dtype, notna, remove_na_arraylike)
|
27 | 28 |
|
@@ -658,11 +659,66 @@ def view(self, dtype=None):
|
658 | 659 | # ----------------------------------------------------------------------
|
659 | 660 | # NDArray Compat
|
660 | 661 |
|
661 |
| - def __array__(self, result=None): |
| 662 | + def __array__(self, dtype=None): |
662 | 663 | """
|
663 |
| - The array interface, return my values. |
664 |
| - """ |
665 |
| - return self.get_values() |
| 664 | + Return the values as a NumPy array. |
| 665 | +
|
| 666 | + Users should not call this directly. Rather, it is invoked by |
| 667 | + :func:`numpy.array` and :func:`numpy.asarray`. |
| 668 | +
|
| 669 | + Parameters |
| 670 | + ---------- |
| 671 | + dtype : str or numpy.dtype, optional |
| 672 | + The dtype to use for the resulting NumPy array. By default, |
| 673 | + the dtype is inferred from the data. |
| 674 | +
|
| 675 | + Returns |
| 676 | + ------- |
| 677 | + numpy.ndarray |
| 678 | + The values in the series converted to a :class:`numpy.ndarary` |
| 679 | + with the specified `dtype`. |
| 680 | +
|
| 681 | + See Also |
| 682 | + -------- |
| 683 | + pandas.array : Create a new array from data. |
| 684 | + Series.array : Zero-copy view to the array backing the Series. |
| 685 | + Series.to_numpy : Series method for similar behavior. |
| 686 | +
|
| 687 | + Examples |
| 688 | + -------- |
| 689 | + >>> ser = pd.Series([1, 2, 3]) |
| 690 | + >>> np.asarray(ser) |
| 691 | + array([1, 2, 3]) |
| 692 | +
|
| 693 | + For timezone-aware data, the timezones may be retained with |
| 694 | + ``dtype='object'`` |
| 695 | +
|
| 696 | + >>> tzser = pd.Series(pd.date_range('2000', periods=2, tz="CET")) |
| 697 | + >>> np.asarray(tzser, dtype="object") |
| 698 | + array([Timestamp('2000-01-01 00:00:00+0100', tz='CET', freq='D'), |
| 699 | + Timestamp('2000-01-02 00:00:00+0100', tz='CET', freq='D')], |
| 700 | + dtype=object) |
| 701 | +
|
| 702 | + Or the values may be localized to UTC and the tzinfo discared with |
| 703 | + ``dtype='datetime64[ns]'`` |
| 704 | +
|
| 705 | + >>> np.asarray(tzser, dtype="datetime64[ns]") # doctest: +ELLIPSIS |
| 706 | + array(['1999-12-31T23:00:00.000000000', ...], |
| 707 | + dtype='datetime64[ns]') |
| 708 | + """ |
| 709 | + if (dtype is None and isinstance(self.array, ABCDatetimeArray) |
| 710 | + and getattr(self.dtype, 'tz', None)): |
| 711 | + msg = ( |
| 712 | + "Converting timezone-aware DatetimeArray to timezone-naive " |
| 713 | + "ndarray with 'datetime64[ns]' dtype. In the future, this " |
| 714 | + "will return an ndarray with 'object' dtype where each " |
| 715 | + "element is a 'pandas.Timestamp' with the correct 'tz'.\n\t" |
| 716 | + "To accept the future behavior, pass 'dtype=object'.\n\t" |
| 717 | + "To keep the old behavior, pass 'dtype=\"datetime64[ns]\"'." |
| 718 | + ) |
| 719 | + warnings.warn(msg, FutureWarning, stacklevel=3) |
| 720 | + dtype = 'M8[ns]' |
| 721 | + return np.asarray(self.array, dtype) |
666 | 722 |
|
667 | 723 | def __array_wrap__(self, result, context=None):
|
668 | 724 | """
|
|
0 commit comments