2

In MATLAB, if I write my own function I can pass an array to that function and it automagically just deals with it. I'm trying to do the same thing in Python, the other Data Science language, and it's not dealing with it.

Is there an easy way to do this without having to make loops every time I want to operate on all values in an array? That's a lot more work! It seems like the great minds working in Python would have had something like this need before.

I tried switching the data type to a list() since that's iterable, but that didn't seem to work. I'm still getting an error that basically says it doesn't want an array object.

Here's my code:

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)
3
  • I think the magic you might be looking for is in numpy. For instance, you could do that if statement with numpy.where. Commented Mar 10, 2017 at 4:04
  • If your list is one dimensional, maybe you could try something like map(from_excel_ordinal, Dates). It would apply said function to every element. Commented Mar 10, 2017 at 4:20
  • you can use map or lambda to do the same, for map just use like Dates = list(map(from_excel_ordinal,Dates)) Commented Mar 10, 2017 at 5:08

1 Answer 1

1

Well you can use map function for that.

Dates = from_excel_ordinal(Dates)

Replace the above line from the code with the following code

Dates = list(map(from_excel_ordinal,Dates))

In the above code, for each value present in Dates, function from_excel_ordinal will be called. Finally it gets converted into list and stored into Dates.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.