@@ -1021,7 +1021,13 @@ def ravel(self, order: str_t = "C") -> Self:
1021
1021
1022
1022
def view (self , cls = None ):
1023
1023
"""
1024
- Return a view on self.
1024
+ Return a view of the Index with the specified dtype or a new Index instance.
1025
+
1026
+ This method returns a view of the calling Index object if no arguments are
1027
+ provided. If a dtype is specified through the `cls` argument, it attempts
1028
+ to return a view of the Index with the specified dtype. Note that viewing
1029
+ the Index as a different dtype reinterprets the underlying data, which can
1030
+ lead to unexpected results for non-numeric or incompatible dtype conversions.
1025
1031
1026
1032
Parameters
1027
1033
----------
@@ -1034,27 +1040,38 @@ def view(self, cls=None):
1034
1040
1035
1041
Returns
1036
1042
-------
1037
- numpy.ndarray
1038
- A new view of the same data in memory.
1043
+ Index or ndarray
1044
+ A view of the Index. If `cls` is None, the returned object is an Index
1045
+ view with the same dtype as the calling object. If a numeric `cls` is
1046
+ specified an ndarray view with the new dtype is returned.
1047
+
1048
+ Raises
1049
+ ------
1050
+ ValueError
1051
+ If attempting to change to a dtype in a way that is not compatible with
1052
+ the original dtype's memory layout, for example, viewing an 'int64' Index
1053
+ as 'str'.
1039
1054
1040
1055
See Also
1041
1056
--------
1057
+ Index.copy : Returns a copy of the Index.
1042
1058
numpy.ndarray.view : Returns a new view of array with the same data.
1043
1059
1044
1060
Examples
1045
1061
--------
1046
- >>> s = pd.Series([ 1, 2, 3], index=["1", "2", "3" ])
1047
- >>> s.index. view("object" )
1048
- array(['1', '2', '3' ], dtype=object )
1062
+ >>> idx = pd.Index([- 1, 0, 1 ])
1063
+ >>> idx. view()
1064
+ Index([-1, 0, 1 ], dtype='int64' )
1049
1065
1050
- >>> s = pd.Series([1, 2, 3], index=[-1, 0, 1])
1051
- >>> s.index.view(np.int64)
1052
- array([-1, 0, 1])
1053
- >>> s.index.view(np.float32)
1054
- array([ nan, nan, 0.e+00, 0.e+00, 1.e-45, 0.e+00], dtype=float32)
1055
- >>> s.index.view(np.uint64)
1066
+ >>> idx.view(np.uint64)
1056
1067
array([18446744073709551615, 0, 1],
1057
1068
dtype=uint64)
1069
+
1070
+ Viewing as 'int32' or 'float32' reinterprets the memory, which may lead to
1071
+ unexpected behavior:
1072
+
1073
+ >>> idx.view("float32")
1074
+ array([ nan, nan, 0.e+00, 0.e+00, 1.e-45, 0.e+00], dtype=float32)
1058
1075
"""
1059
1076
# we need to see if we are subclassing an
1060
1077
# index type here
@@ -1809,19 +1826,34 @@ def _get_names(self) -> FrozenList:
1809
1826
"""
1810
1827
Get names on index.
1811
1828
1829
+ This method returns a FrozenList containing the names of the object.
1830
+ It's primarily intended for internal use.
1831
+
1832
+ Returns
1833
+ -------
1834
+ FrozenList
1835
+ A FrozenList containing the object's names, contains None if the object
1836
+ does not have a name.
1837
+
1812
1838
See Also
1813
1839
--------
1814
- Index.name : Return Index or MultiIndex name .
1840
+ Index.name : Index name as a string, or None for MultiIndex .
1815
1841
1816
1842
Examples
1817
1843
--------
1818
1844
>>> idx = pd.Index([1, 2, 3], name="x")
1819
1845
>>> idx.names
1820
1846
FrozenList(['x'])
1821
1847
1822
- >>> idx = s = pd.Index([1, 2, 3], name=("x", "y"))
1848
+ >>> idx = pd.Index([1, 2, 3], name=("x", "y"))
1823
1849
>>> idx.names
1824
1850
FrozenList([('x', 'y')])
1851
+
1852
+ If the index does not have a name set:
1853
+
1854
+ >>> idx = pd.Index([1, 2, 3])
1855
+ >>> idx.names
1856
+ FrozenList([None])
1825
1857
"""
1826
1858
return FrozenList ((self .name ,))
1827
1859
0 commit comments