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)
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
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
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics