(实线为普通属性,虚线为property
属性或者getset_descriptor
)
1.Index
的结构
.name
为普通属性,返回Index
的名字.values/._values
为property
属性,返回Index
的内部数据的视图._data
为普通属性,返回Index
的内部数据.shape
为property
属性,返回内部数据的形状._engine
为标签映射管理器,它负责管理label
和下标之间的映射ObjectEngine
对象使用一个哈希表对象PyObjectHashTable
对象(由ObjectEngine
对象的.mmaping
属性给出,该属性是一个getset_descriptor
)将标签映射到其对应的整数下标的。
举例:
import numpy as np import pandas as pd s = pd.Index(['a','b','c','d'],name='index1') s # Index(['a', 'b', 'c', 'd'], dtype='object', name='index1') s.name,s.values,s._values # ('index1', array(['a', 'b', 'c', 'd'], dtype=object), array(['a', 'b', 'c', 'd'], dtype=object)) s._data,s.shape, # (array(['a', 'b', 'c', 'd'], dtype=object), (4,)) s._engine # <pandas._libs.index.ObjectEngine at 0x1e1671803b8>
2.MultiIndex
的结构
.name
为普通属性,返回MultiIndex
的名字。同Index
.values/._values
为property
属性,返回MultiIndex
的内部数据的视图。同Index
._data
为None
,这里是与Index
不同。
.shape
为property
属性,返回内部属性的形状 。同Index
._engine
为标签映射管理器,它负责管理label
和下标之间的映射。同Index
.labels
为property
属性,它返回一个FrozenList
(不可变列表),列表中存储每一级的label
对应的下标(也就是创建MultiIndex
时传入的labels
参数),以FrozenNDArray
的数据类型。.levels
为property
属性,它返回一个FrozenList
(不可变列表),列表中存储每一级的label
(也就是创建MultiIn
dex
时传入的levels
参数),以Index
的数据类型。
举例:
import numpy as np import pandas as pd levels = [['a','b'],['c','d']] labels = [[0,1,0,1],[0,0,1,1]] s = pd.MultiIndex(levels=levels,labels=labels,name=['index1','index2']) s # MultiIndex(levels=[['a', 'b'], ['c', 'd']], labels=[[0, 1, 0, 1], [0, 0, 1, 1]], names=['index1', 'index2']) s.values,s._values # (array([('a', 'c'), ('b', 'c'), ('a', 'd'), ('b', 'd')], dtype=object), array([('a', 'c'), ('b', 'c'), ('a', 'd'), ('b', 'd')], dtype=object)) v1 = s.values v2 = s._values v1.base is v2.base,v1 is v2,s._data # (True, True, None) s.labels,s.labels[0] # (FrozenList([[0, 1, 0, 1], [0, 0, 1, 1]]), FrozenNDArray([0, 1, 0, 1], dtype='int8')) s.levels,s.levels[0] # (FrozenList([['a', 'b'], ['c', 'd']]), Index(['a', 'b'], dtype='object', name='index1'))
3.Seris
的结构
._name
为普通属性,返回Seris
的名字;.name
为property
属性,返回的也是Seris
名字.dtype/.dtypes
为property
属性,返回Series
的数据类型。.ftype/ftypes
为property
属性,返回一个字符串,说明Series
是否稀疏数据。(二者返回的字符串的值相同,但不是同一个字符串对象).values/._values
为property
属性,返回Series
的内部数据的视图.index
为普通属性,返回Series
的索引.shape
为property
属性,返回Series
的数据的形状._data
为普通属性,它返回的是一个SingleBlockManager
对象,该对象负责管理内部数据。SingleBlockManager
的.shape
属性为property
属性,返回内部数据的形状SingleBlockManager
的.blocks
属性为普通属性,返回一个列表,该列表只有一个元素,该元素为一个IntBlock
对象(或者其他的xxxBlock
对象),代表了内部数据。IntBlock
的.values
属性为普通属性,它返回内部数据:一个ndarray
。IntBlock
的.shape
属性为property
属性,它返回内部数据的形状
举例:
s = pd.Series({'a':1,'b':2,'c':3},name='sr') s # a 1 b 2 c 3 Name: sr, dtype: int64 s.name,s._name,s.name is s._name # ('sr', 'sr', True) s.dtype,s.dtypes,s.dtype is s.dtypes # (dtype('int64'), dtype('int64'), True) s.ftype,s.ftypes,s.ftype is s.ftypes # ('int64:dense', 'int64:dense', False) f1,f2 = s.ftype,s.ftypes f1 is f2 # False s.values,s._values,s.values is s._values # (array([1, 2, 3], dtype=int64), array([1, 2, 3], dtype=int64), True) s.index,s.shape # (Index(['a', 'b', 'c'], dtype='object'), (3,)) s._data # SingleBlockManager Items: Index(['a', 'b', 'c'], dtype='object') IntBlock: 3 dtype: int64
4.DataFrame
的结构
.index/columns
属性都为普通属性,它们返回的都是一个Index
对象,参考Series
。.dtypes
属性为property
属性,给出了每列的数值类型。它返回的是一个Series
。并且没有.dtype
属性,这一点与Series
不同。.ftypes
属性为property
属性,给出了每列是否为sparse/dense
的。它返回的是一个Series
。并且没有.ftype
属性,这一点与Series
不同。.values/._values/.shape
属性都为property
属性,参考Series
。._data
属性为普通属性,它返回的是一个BlockManager
对象,该对象负责管理内部数据。该对象的.block
属性(普通属性)返回一个列表,该列表里面有多个元素。DataFrame
尽量用一个数组保存类型相同的列。- 每个元素都为一个
xxBlock
对象。如IntBlock/FloatBlock...
- 一个
xxBlock
可能存储多个列的数据(这些列的数据都是同一个类型)
- 一个
xxBlock
对象的.values
属性(普通属性)就是存储的某个列(或者某些类型相同的列)的内部数据,一个ndarray
xxBlock
对象的.shape
属性(property
属性)就是存储的某个列(或者某些类型相同的列)的内部数据的形状.blocks
属性为property
属性。该属性返回一个字典,该字典的键为不同的数值类型,该字典的值为该数值类型的数值组成的DataFrame
举例:
import numpy as np import pandas as pd df = pd.DataFrame(np.arange(0,12).reshape(3,4),index=['a','b','c'],columns=['c1','c2','c3','c3']) df # c1 c2 c3 c3 a 0 1 2 3 b 4 5 6 7 c 8 9 10 11 df.index,df.columns # (Index(['a', 'b', 'c'], dtype='object'), Index(['c1', 'c2', 'c3', 'c3'], dtype='object')) df.dtypes,type(df.dtypes) # (c1 int32 c2 int32 c3 int32 c3 int32 dtype: object, pandas.core.series.Series) df.ftypes,type(df.ftypes) # (c1 int32:dense c2 int32:dense c3 int32:dense c3 int32:dense dtype: object, pandas.core.series.Series) print(df.values,df._values) v1 = df.values v2 = df._values v1.base is v2.base # [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] True manager = df._data manager #BlockManager Items: Index(['c1', 'c2', 'c3', 'c3'], dtype='object') Axis 1: Index(['a', 'b', 'c'], dtype='object') IntBlock: slice(0, 4, 1), 4 x 3, dtype: int32 manager.blocks # (IntBlock: slice(0, 4, 1), 4 x 3, dtype: int32,) manager.blocks[0].values,manager.blocks[0].shape # (array([[ 0, 4, 8], [ 1, 5, 9], [ 2, 6, 10], [ 3, 7, 11]]), (4, 3)) df.blocks,type(df.blocks),type(df.blocks['int32']) # ({'int32': c1 c2 c3 c3 a 0 1 2 3 b 4 5 6 7 c 8 9 10 11}, dict, pandas.core.frame.DataFrame)