pandas.Series.view#

Series.view(dtype=None)[源代码]#

创建系列的新视图。

此函数将返回一个新的Series,其中包含内存中相同基础值的视图,并可选择使用新的数据类型重新解释。新数据类型必须以字节为单位保留相同的大小,以避免导致索引未对齐。

参数
dtype数据类型

数据类型对象或其字符串表示形式之一。

退货
系列

一个新的Series对象,作为内存中相同数据的视图。

参见

numpy.ndarray.view

等价的NumPy函数,在内存中创建相同数据的新视图。

注意事项

序列实例化为 dtype=float64 默认情况下。而当 numpy.ndarray.view() 将返回与原始数组具有相同数据类型的视图, Series.view() (未指定数据类型)将尝试使用 float64 并且如果以字节为单位的原始数据类型大小不相同,则可能失败。

示例

>>> s = pd.Series([-2, -1, 0, 1, 2], dtype='int8')
>>> s
0   -2
1   -1
2    0
3    1
4    2
dtype: int8

的8位带符号整数表示 -10b11111111 ,但如果读取为8位无符号整数,则相同的字节表示255:

>>> us = s.view('uint8')
>>> us
0    254
1    255
2      0
3      1
4      2
dtype: uint8

这些视图共享相同的基本价值:

>>> us[0] = 128
>>> s
0   -128
1     -1
2      0
3      1
4      2
dtype: int8