w3resource

Pandas Series: view() function

Create a new view of Pandas Series

The view() function is used to create a new view of the Series.

This function returns 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.

Syntax:

Series.view(self, dtype=None)
Pandas Series view image
Name Description Type/Default Value Required / Optional
dtype   Data type object or one of their string representations. data type Required

Returns: Series - A new Series object as a view of the same data in memory.

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([-2, -3, 0, 3, 2], dtype='int8')
s

Output:

0   -2
1   -3
2    0
3    3
4    2
dtype: int8
Pandas Series view image

Example - The 8 bit signed integer representation of -1 is 0b11111111, but the same bytes represent 255 if read as an 8 bit unsigned integer:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([-2, -3, 0, 3, 2], dtype='int8')
us = s.view('uint8')
us

Output:

0    254
1    253
2      0
3      3
4      2
dtype: uint8

Example - The views share the same underlying values:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([-2, -3, 0, 3, 2], dtype='int8')
us = s.view('uint8')
us[0] = 128
s

Output:

0   -128
1     -3
2      0
3      3
4      2
dtype: int8

Previous: Squeeze 1 dimensional axis objects into scalars
Next: Concatenate two or more Pandas series



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/pandas/series/series-view.php