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