Python & Pandas - pd.Series difference between int32 and int64

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

EdC*_*ica 4

它们在语义上的不同之处在于,在第一个版本中,您传递一个具有单个标量值的字典,因此 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.2numpy1.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

  • 大概就是这个原因。我的 numpy 是 1.13.1,我也得到了 Bharath 的结果。 (2认同)
  • 情况必须如此:/sf/ask/2539501331/ (2认同)
  • @ayhan,我相信 pandas 会尝试在传入的数据上调用“np.array”构造函数(如果它是可迭代的或类似数组的),因此数据类型将来自“numpy”。在第一种情况下,对于以此形式传递的标量类型,默认值为“int64” (2认同)