pd.Series.prod()函数 [英] The pd.Series.prod() function

查看:330
本文介绍了pd.Series.prod()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是基本知识,但我仍然无法弄清楚.

This should be probably elementary but still I can not figure it out.

我正在阅读pd.Series上的文档并进行简单的练习.

I am reading the documentation on pd.Series and doing simple exercises.

我的代码如下:

import pandas as pd
import numpy as np    
pd.Series([2, 4, 6]).prod()
Out[7]: 48

a = pd.Series(np.arange(1, 100, 3))

a
Out[9]: 
0      1
1      4
2      7
3     10
4     13
5     16
6     19
7     22
8     25
9     28
10    31
11    34
12    37
13    40
14    43
15    46
16    49
17    52
18    55
19    58
20    61
21    64
22    67
23    70
24    73
25    76
26    79
27    82
28    85
29    88
30    91
31    94
32    97
dtype: int32

a.prod()
Out[10]: 0

a = pd.Series(np.arange(1, 100, 2))

a.prod()
Out[15]: -373459037

type(a)
Out[18]: pandas.core.series.Series

我的问题是,为什么这种不稳定的行为(在我看来)?为什么a.prod()第一次计算为0,然后计算为负整数?

My question is why this erratic -to my eyes-- behavior? Why a.prod() the first time evaluates to 0 and then evaluates to a negative integer?

您的建议将不胜感激.

推荐答案

我们可以直接使用numpy的np.prod与指定的dtype来克服int溢出:

We can directly use numpy's np.prod with specified dtype to overcome int overflow :

np.prod(a.values,dtype=np.int64)
Out[938]: 5196472710489536419

这篇关于pd.Series.prod()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆