Skip to content

Commit a44bae3

Browse files
villasvjorisvandenbossche
authored andcommitted
DOC: update the Series.view docstring (pandas-dev#20220)
1 parent 233103f commit a44bae3

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

pandas/core/series.py

+65
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,71 @@ def __len__(self):
547547
return len(self._data)
548548

549549
def view(self, dtype=None):
550+
"""
551+
Create a new view of the Series.
552+
553+
This function will return a new Series with a view of the same
554+
underlying values in memory, optionally reinterpreted with a new data
555+
type. The new data type must preserve the same size in bytes as to not
556+
cause index misalignment.
557+
558+
Parameters
559+
----------
560+
dtype : data type
561+
Data type object or one of their string representations.
562+
563+
Returns
564+
-------
565+
Series
566+
A new Series object as a view of the same data in memory.
567+
568+
See Also
569+
--------
570+
numpy.ndarray.view : Equivalent numpy function to create a new view of
571+
the same data in memory.
572+
573+
Notes
574+
-----
575+
Series are instantiated with ``dtype=float64`` by default. While
576+
``numpy.ndarray.view()`` will return a view with the same data type as
577+
the original array, ``Series.view()`` (without specified dtype)
578+
will try using ``float64`` and may fail if the original data type size
579+
in bytes is not the same.
580+
581+
Examples
582+
--------
583+
>>> s = pd.Series([-2, -1, 0, 1, 2], dtype='int8')
584+
>>> s
585+
0 -2
586+
1 -1
587+
2 0
588+
3 1
589+
4 2
590+
dtype: int8
591+
592+
The 8 bit signed integer representation of `-1` is `0b11111111`, but
593+
the same bytes represent 255 if read as an 8 bit unsigned integer:
594+
595+
>>> us = s.view('uint8')
596+
>>> us
597+
0 254
598+
1 255
599+
2 0
600+
3 1
601+
4 2
602+
dtype: uint8
603+
604+
The views share the same underlying values:
605+
606+
>>> us[0] = 128
607+
>>> s
608+
0 -128
609+
1 -1
610+
2 0
611+
3 1
612+
4 2
613+
dtype: int8
614+
"""
550615
return self._constructor(self._values.view(dtype),
551616
index=self.index).__finalize__(self)
552617

0 commit comments

Comments
 (0)