Python 映射到列表错误:系列对象不可调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39137506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:55:01  来源:igfitidea点击:

Map to List error: Series object not callable

pythonlistpandasdictionarynsepy

提问by Quantum Dreamer

from nsepy import get_history
from datetime import date
import datetime
import pandas as pd
import numpy as np
file = r'C:\Users\Raspberry-Pi\Desktop\Desktop\List.xlsx'
list = pd.read_excel(file)
list = list['SYMBOL']
start = date.today()-datetime.timedelta(days = 10)
end = date.today()
symb = get_history(symbol='INFY',start = start,end = end)
h = symb.tail(3).High.tolist()
l = symb.tail(3).Low.tolist()
print(type(h))
print(type(l))
x =  map(lambda a,b:a-b,h,l)
print(type(x))
x = list(x)

I am getting error:

我收到错误:

series object not callable

系列对象不可调用

and its pointing to x = list(x)line.

并且它指向x = list(x)线。

采纳答案by jezrael

But I think you can omit mapand use simple subtract and then convert to list:

但我认为您可以省略map并使用简单的减法,然后转换为list

symb = get_history(symbol='INFY',start = start,end = end)
print ((symb.tail(3).High - symb.tail(3).Low).tolist())

Also don't use variable list(reserved word in python) rather L(or something else):

也不要使用变量list(python 中的保留字)而不是 L(或其他东西):

L = pd.read_excel(file)
L = L['SYMBOL']

Sample:

样本:

import pandas as pd

symb = pd.DataFrame({'High':[8,9,7,5,3,4],'Low':[1,2,3,1,0,1]})
print (symb)
   High  Low
0     8    1
1     9    2
2     7    3
3     5    1
4     3    0
5     4    1

print ((symb.tail(3).High - symb.tail(3).Low).tolist())
[4, 3, 3]

EDIT:

编辑:

I try simulate problem:

我尝试模拟问题:

list = pd.DataFrame({'SYMBOL':['sss old','dd','old']})
print (list)
     SYMBOL
0  sss old
1       dd
2      old

list = list['SYMBOL']
print (list)
0    sss old
1         dd
2        old
Name: SYMBOL, dtype: object

print (type(list))
<class 'pandas.core.series.Series'>

x = [1,2,3]

#list is Series, not function
x = list(x)
print (x)
TypeError: 'Series' object is not callable


If change listto L, is important reopen python console, because still same error.

如果更改listL, 很重要重新打开 python 控制台,因为仍然出现相同的错误。

So this works perfectly:

所以这完美地工作:

df = pd.DataFrame({'SYMBOL':['sss old','dd','old']})
print (df)
     SYMBOL
0  sss old
1       dd
2      old

L = df['SYMBOL']
print (L)
0    sss old
1         dd
2        old
Name: SYMBOL, dtype: object

x = [1,2,3]
x = list(x)
print (x)
[1, 2, 3]

回答by hpaulj

list(x)normally means turn xinto a listobject. It's a function that creates a list object. But near the top you redefined list:

list(x)通常意味着x变成一个list物体。它是一个创建列表对象的函数。但在你重新定义的顶部附近list

list = pd.read_excel(file)

Now listis now a pandas seriesobject (as the error message says), and it does not function as a function, i.e. it is not callable, it cannot be used with ().

Now 现在list是一个 pandasseries对象(如错误消息所述),并且它不作为函数运行,即它不是callable,它不能与().

Use a different name for this object. Use a silly name like fooif you can't think of a better descriptor.

为此对象使用不同的名称。foo如果您想不出更好的描述符,请使用愚蠢的名称。

回答by saikumarm

The problem is that you have reassigned reserve word

问题是你重新分配了保留字

list = pd.read_excel(file) 
list = list['SYMBOL']

hence list here is just a list of ['S', 'Y', 'M', 'B'..]. Use another name for this definition the program will work fine

因此这里的列表只是一个列表['S', 'Y', 'M', 'B'..]。为这个定义使用另一个名称程序将正常工作