Mik*_*ers 6 python numpy data-analysis pandas
I'm starting to learn python, numpy and panda's and I have a really basic question, about sizes.
Please see the next code blocks:
1. Length: 6, dtype: int64
# create a Series from a dict
pd.Series({key: value for key, value in zip('abcdef', range(6))})
Run Code Online (Sandbox Code Playgroud)
vs.
2. Length: 6, dtype: int32
# but why does this generate a smaller integer size???
pd.Series(range(6), index=list('abcdef'))
Run Code Online (Sandbox Code Playgroud)
Question So I think when you put a list, numpy array, dictionary etc. in the pd.Series you will get int64 but when you put just the range(6) in the pd.Series you will get int32. Can someone please make this a little bit clear to me?
Sorry for the very basic question.
@Edit : I'm using Pandas version 0.20.1 and Numpy 1.12.1
它们在语义上的不同之处在于,在第一个版本中,您传递一个具有单个标量值的字典,因此 dtype 变为int64,对于第二个版本,您传递一个range可以轻松转换为 numpy 数组的 ,这是int32:
In[57]:
np.array(range(6)).dtype
Out[57]: dtype('int32')
Run Code Online (Sandbox Code Playgroud)
因此,pandas 的构造series在第一个实例中涉及数据类型匹配,而在第二个实例中则不需要,因为它可以转换为 numpy 数组,并且 numpy 已确定int32在这种情况下首选该数据类型
更新
看来这取决于您的numpy版本,也许还取决于pandas版本。我正在运行 python 3.6、numpy 1.12.1 和 pandas 0.20.3,得到上述结果。我也运行 Windows 7 64 位
@jeremycg 正在运行 pandas0.19.2和numpy1.11.2 并观察到相同的结果,而 @coldspeed 正在运行numpy1.13.1 并观察到int64。
由此得出的结论是,意志dtype很大程度上取决于numpy做什么。
我相信,当我们通过这种情况时,这条线就是所谓的。range
subarr = np.array(arr, dtype=object, copy=copy)
Run Code Online (Sandbox Code Playgroud)
返回的类型由操作系统决定numpy,在我的例子中,Windows 已将 C Long 定义为 32 位。请参阅相关内容:numpy array dtype is come as int32 by default in a windows 10 64 bit machine
| 归档时间: |
|
| 查看次数: |
1766 次 |
| 最近记录: |