From f313b794f7946cd19dc4c152805788f095ddc764 Mon Sep 17 00:00:00 2001 From: Victor Villas Date: Sat, 10 Mar 2018 18:20:33 +0000 Subject: [PATCH 1/3] DOC: update Series.view docstring --- pandas/core/series.py | 60 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/pandas/core/series.py b/pandas/core/series.py index 7b9b8a7a75008..22e9dec3f9b25 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -547,6 +547,66 @@ def __len__(self): return len(self._data) def view(self, dtype=None): + """ + Create a new view of the Series. + + This function will return a new Series with a view of the same + underlying values in memory, optionally reinterpreted with a new data + type. The new data type must preserve the same size in bytes as to not + cause index misalignment. + + Series are instantiated with `dtype=float64` by default, so unlike + `numpy.array.view()` this function will not try to preserve the current + Series data type. + + Parameters + ---------- + dtype : data type + Data type object or one of their string representations. + + Returns + ------- + Series + A new Series object as a view of the same data in memory. + + See Also + -------- + numpy.ndarray.view : Return a new view of the same data in memory. + + Examples + -------- + >>> s = pd.Series([-2, -1, 0, 1, 2], dtype='int8') + >>> s + 0 -2 + 1 -1 + 2 0 + 3 1 + 4 2 + dtype: int8 + + The 8 bit signed integer representation of `-1` is `0b10000001`, but + the same bytes represent 255 if read as an 8 bit unsigned integer: + + >>> us = s.view('uint8') + >>> us + 0 254 + 1 255 + 2 0 + 3 1 + 4 2 + dtype: uint8 + + The views share the same underlying values: + + >>> us[0] = 128 + >>> s + 0 -128 + 1 -1 + 2 0 + 3 1 + 4 2 + dtype: int8 + """ return self._constructor(self._values.view(dtype), index=self.index).__finalize__(self) From cffd04b379d3c885bd9b00b8dff46ce9ff15635f Mon Sep 17 00:00:00 2001 From: Victor Villas Date: Sat, 10 Mar 2018 22:11:04 +0000 Subject: [PATCH 2/3] Move comparison with numpy.ndarray.view to Notes --- pandas/core/series.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 22e9dec3f9b25..403ab9c1eebbf 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -555,10 +555,6 @@ def view(self, dtype=None): type. The new data type must preserve the same size in bytes as to not cause index misalignment. - Series are instantiated with `dtype=float64` by default, so unlike - `numpy.array.view()` this function will not try to preserve the current - Series data type. - Parameters ---------- dtype : data type @@ -571,7 +567,15 @@ def view(self, dtype=None): See Also -------- - numpy.ndarray.view : Return a new view of the same data in memory. + numpy.ndarray.view : Equivalent numpy function to create a new view of + the same data in memory. + + Notes + ----- + Series are instantiated with `dtype=float64` by default. While + `numpy.ndarray.view()` will return a view with the same data type as + the original array, Series.view() will try using `float64` and may fail + if the original data type size in bytes is not the same. Examples -------- @@ -584,7 +588,7 @@ def view(self, dtype=None): 4 2 dtype: int8 - The 8 bit signed integer representation of `-1` is `0b10000001`, but + The 8 bit signed integer representation of `-1` is `0b11111111`, but the same bytes represent 255 if read as an 8 bit unsigned integer: >>> us = s.view('uint8') From 4362e383fc34ed9144fa160126ae85fd8e77015e Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Sun, 11 Mar 2018 14:57:42 +0100 Subject: [PATCH 3/3] small edit in backticks --- pandas/core/series.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 403ab9c1eebbf..1fce2097f745b 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -572,10 +572,11 @@ def view(self, dtype=None): Notes ----- - Series are instantiated with `dtype=float64` by default. While - `numpy.ndarray.view()` will return a view with the same data type as - the original array, Series.view() will try using `float64` and may fail - if the original data type size in bytes is not the same. + Series are instantiated with ``dtype=float64`` by default. While + ``numpy.ndarray.view()`` will return a view with the same data type as + the original array, ``Series.view()`` (without specified dtype) + will try using ``float64`` and may fail if the original data type size + in bytes is not the same. Examples --------