@@ -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
@@ -898,8 +910,13 @@ def to_numpy(self):
898
910
if (is_extension_array_dtype (self .dtype ) or
899
911
is_datetime64tz_dtype (self .dtype )):
900
912
# TODO(DatetimeArray): remove the second clause.
901
- return np .asarray (self ._values )
902
- return self ._values
913
+ result = np .asarray (self ._values , dtype = dtype )
914
+ else :
915
+ result = self ._values
916
+
917
+ if copy :
918
+ result = result .copy ()
919
+ return result
903
920
904
921
@property
905
922
def _ndarray_values (self ):
0 commit comments