@@ -857,18 +857,22 @@ def array(self):
857
857
result = PandasArray (result )
858
858
return result
859
859
860
- def to_numpy (self ):
860
+ def to_numpy (self , dtype = None , copy = False ):
861
861
"""
862
862
A NumPy ndarray representing the values in this Series or Index.
863
863
864
864
.. versionadded:: 0.24.0
865
865
866
- The returned array will be the same up to equality (values equal
867
- in `self` will be equal in the returned array; likewise for values
868
- that are not equal). When `self` contains an ExtensionArray, the
869
- dtype may be different. For example, for a category-dtype Series,
870
- ``to_numpy()`` will return a NumPy array and the categorical dtype
871
- will be lost.
866
+
867
+ Parameters
868
+ ----------
869
+ dtype : str or numpy.dtype, optional
870
+ The dtype to pass to :meth:`numpy.asarray`
871
+ copy : bool, default False
872
+ Whether to ensure that the returned value is a not a view on
873
+ another array. Note that ``copy=False`` does not *ensure* that
874
+ ``to_numpy()`` is no-copy. Rather, ``copy=True`` ensure that
875
+ a copy is made, even if not strictly necessary.
872
876
873
877
Returns
874
878
-------
@@ -882,10 +886,18 @@ def to_numpy(self):
882
886
883
887
Notes
884
888
-----
889
+ The returned array will be the same up to equality (values equal
890
+ in `self` will be equal in the returned array; likewise for values
891
+ that are not equal). When `self` contains an ExtensionArray, the
892
+ dtype may be different. For example, for a category-dtype Series,
893
+ ``to_numpy()`` will return a NumPy array and the categorical dtype
894
+ will be lost.
895
+
896
+
885
897
For NumPy dtypes, this will be a reference to the actual data stored
886
- in this Series or Index. Modifying the result in place will modify
887
- the data stored in the Series or Index (not that we recommend doing
888
- that).
898
+ in this Series or Index (assuming ``copy=False``) . Modifying the result
899
+ in place will modify the data stored in the Series or Index (not that
900
+ we recommend doing that).
889
901
890
902
For extension types, ``to_numpy()`` *may* require copying data and
891
903
coercing the result to a NumPy type (possibly object), which may be
@@ -910,12 +922,37 @@ def to_numpy(self):
910
922
>>> ser = pd.Series(pd.Categorical(['a', 'b', 'a']))
911
923
>>> ser.to_numpy()
912
924
array(['a', 'b', 'a'], dtype=object)
925
+
926
+ Specify the `dtype` to control how datetime-aware data is represented.
927
+ Use ``dtype=object`` to return an ndarray of pandas :class:`Timestamp`
928
+ objects, each with the correct ``tz``.
929
+
930
+ >>> ser = pd.Series(pd.date_range('2000', periods=2, tz="CET"))
931
+ >>> ser.to_numpy(dtype=object)
932
+ array([Timestamp('2000-01-01 00:00:00+0100', tz='CET', freq='D'),
933
+ Timestamp('2000-01-02 00:00:00+0100', tz='CET', freq='D')],
934
+ dtype=object)
935
+
936
+ Or ``dtype='datetime64[ns]'`` to return an ndarray of native
937
+ datetime64 values. The values are converted to UTC and the timezone
938
+ info is dropped.
939
+
940
+ >>> ser.to_numpy(dtype="datetime64[ns]")
941
+ ... # doctest: +ELLIPSIS
942
+ array(['1999-12-31T23:00:00.000000000', '2000-01-01T23:00:00...'],
943
+ dtype='datetime64[ns]')
913
944
"""
914
945
if (is_extension_array_dtype (self .dtype ) or
915
946
is_datetime64tz_dtype (self .dtype )):
916
947
# TODO(DatetimeArray): remove the second clause.
917
- return np .asarray (self ._values )
918
- return self ._values
948
+ # TODO(GH-24345): Avoid potential double copy
949
+ result = np .asarray (self ._values , dtype = dtype )
950
+ else :
951
+ result = self ._values
952
+
953
+ if copy :
954
+ result = result .copy ()
955
+ return result
919
956
920
957
@property
921
958
def _ndarray_values (self ):
0 commit comments