在MATLAB中,如果我编写自己的函数,我可以将一个数组传递给该函数,它会自动处理它。我正在尝试用另一种数据科学语言Python做同样的事情,但它没有处理它。
有没有一种简单的方法可以做到这一点,而不必在每次我想要对数组中的所有值进行操作时都进行循环?这是更多的工作!似乎在Python中工作的伟大思想以前就有这样的需求。
我尝试将数据类型转换为list(),因为这是可迭代的,但似乎行不通。我仍然收到一个错误,基本上是说它不想要数组对象。
下面是我的代码:
import scipy
from collections import deque
import numpy as np
import os
from datetime import date, timedelta
def GetExcelData(filename,rowNum,titleCol):
csv = np.genfromtxt(filename, delimiter= ",")
Dates = deque(csv[rowNum,:])
if titleCol == True:
Dates.popleft()
return list(Dates)
def from_excel_ordinal(ordinal, _epoch=date(1900, 1, 1)):
if ordinal > 59: #the function stops working here when I pass my array
ordinal -= 1 # Excel leap year bug, 1900 is not a leap year!
return _epoch + timedelta(days=ordinal - 1) # epoch is day 1
os.chdir("C:/Users/blahblahblah")
filename = "SamplePandL.csv"
Dates = GetExcelData(filename,1,1)
Dates = from_excel_ordinal(Dates) #this is the call with an issue
print(Dates)发布于 2017-03-10 12:30:26
你可以使用map函数来实现。
Dates = from_excel_ordinal(Dates)将上述代码行替换为以下代码
Dates = list(map(from_excel_ordinal,Dates))在上面的代码中,对于日期中出现的每个值,都将调用函数from_excel_ordinal。最后,它被转换为列表并存储为日期。
https://stackoverflow.com/questions/42709973
复制相似问题