Strange output error following example of matirx vector operation in python











up vote
-1
down vote

favorite












I want to do this in python, here is a small example:



number_of_payments = [
[0, 1, 0, 1, 1, 1, 0, 5, 1, 0, 2, 1],
[0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0],
[1, 3, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0]
]
NDD_month = [8, 7, 11]
dates =
for i in range(len(number_of_payments)):
dates.append([NDD_month[i]])
for j in range(1, len(number_of_payments[i])):
dates[i].append((dates[i][j-1] + 12 - number_of_payments[i][j-1]) % 12)
print(dates)


This gives me



[[8, 8, 7, 7, 6, 5, 4, 4, 11, 10, 10, 8], [7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 4], [11, 10, 7, 6, 6, 6, 5, 4, 3, 2, 2, 1]]



Now I try to do the same thing but with the entire set of data but this is what I get (I will paste my whole code below):



# Import modules
import numpy as np
import pandas as pd
import datetime

# Import data file
df = pd.read_csv("Paystring Data.csv")
df.head()

# Get column data into a list
x = list(df)

# Append column data into cpi, NDD, and as of dates
NDD = df['NDD 8/31']
cpi = df['Contractual PI']
as_of_date = pd.Series(pd.to_datetime(df.columns.str[:8], errors='coerce'))
as_of_date = as_of_date[1:13]
NDD_month = pd.to_datetime(NDD, errors = 'coerce').dt.month.tolist()
# print(as_of_date.dt.month)

# Get cash flows
cf = df.iloc[:,1:13].replace('[^0-9.]', '', regex=True).astype(float)
cf = cf.values

# Calculate number of payments
number_of_payments =
for i in range(len(cpi)):
number_of_payments.append((cf[:i + 1] / cpi[i]).astype(int))
np.vstack(number_of_payments).tolist()

# Calculate the new NDD dates
dates =
for i in range(len(number_of_payments)):
dates.append([NDD_month[i]])
for j in range(1, len(number_of_payments[i])):
dates[i].append((dates[i][j-1] + 12 - number_of_payments[i][j-1]) % 12)
print(dates[0])


This just gives me [8]



When it should be [8, 8, 7, 7, 6, 5, 4, 4, 11, 10, 10, 8].



Anyone know how to fix this?










share|improve this question






















  • I suggest you step through a debugger. Your precondition is that your number_of_payments is a list of lists; does that precondition hold in your real code?
    – erip
    Nov 20 at 1:43












  • @erip No idea how to do that in jupyer notebook nor with python I am a c++ programmer
    – Snorrlaxxx
    Nov 20 at 1:43










  • A jupyter notebook is basically an interactive debugger.
    – erip
    Nov 20 at 1:44










  • @erip Do you have any idea what could be causing these issues?
    – Snorrlaxxx
    Nov 20 at 1:45










  • If I had to guess, your data is malformed. You might want to step through a debugger, though.
    – erip
    Nov 20 at 1:46















up vote
-1
down vote

favorite












I want to do this in python, here is a small example:



number_of_payments = [
[0, 1, 0, 1, 1, 1, 0, 5, 1, 0, 2, 1],
[0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0],
[1, 3, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0]
]
NDD_month = [8, 7, 11]
dates =
for i in range(len(number_of_payments)):
dates.append([NDD_month[i]])
for j in range(1, len(number_of_payments[i])):
dates[i].append((dates[i][j-1] + 12 - number_of_payments[i][j-1]) % 12)
print(dates)


This gives me



[[8, 8, 7, 7, 6, 5, 4, 4, 11, 10, 10, 8], [7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 4], [11, 10, 7, 6, 6, 6, 5, 4, 3, 2, 2, 1]]



Now I try to do the same thing but with the entire set of data but this is what I get (I will paste my whole code below):



# Import modules
import numpy as np
import pandas as pd
import datetime

# Import data file
df = pd.read_csv("Paystring Data.csv")
df.head()

# Get column data into a list
x = list(df)

# Append column data into cpi, NDD, and as of dates
NDD = df['NDD 8/31']
cpi = df['Contractual PI']
as_of_date = pd.Series(pd.to_datetime(df.columns.str[:8], errors='coerce'))
as_of_date = as_of_date[1:13]
NDD_month = pd.to_datetime(NDD, errors = 'coerce').dt.month.tolist()
# print(as_of_date.dt.month)

# Get cash flows
cf = df.iloc[:,1:13].replace('[^0-9.]', '', regex=True).astype(float)
cf = cf.values

# Calculate number of payments
number_of_payments =
for i in range(len(cpi)):
number_of_payments.append((cf[:i + 1] / cpi[i]).astype(int))
np.vstack(number_of_payments).tolist()

# Calculate the new NDD dates
dates =
for i in range(len(number_of_payments)):
dates.append([NDD_month[i]])
for j in range(1, len(number_of_payments[i])):
dates[i].append((dates[i][j-1] + 12 - number_of_payments[i][j-1]) % 12)
print(dates[0])


This just gives me [8]



When it should be [8, 8, 7, 7, 6, 5, 4, 4, 11, 10, 10, 8].



Anyone know how to fix this?










share|improve this question






















  • I suggest you step through a debugger. Your precondition is that your number_of_payments is a list of lists; does that precondition hold in your real code?
    – erip
    Nov 20 at 1:43












  • @erip No idea how to do that in jupyer notebook nor with python I am a c++ programmer
    – Snorrlaxxx
    Nov 20 at 1:43










  • A jupyter notebook is basically an interactive debugger.
    – erip
    Nov 20 at 1:44










  • @erip Do you have any idea what could be causing these issues?
    – Snorrlaxxx
    Nov 20 at 1:45










  • If I had to guess, your data is malformed. You might want to step through a debugger, though.
    – erip
    Nov 20 at 1:46













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I want to do this in python, here is a small example:



number_of_payments = [
[0, 1, 0, 1, 1, 1, 0, 5, 1, 0, 2, 1],
[0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0],
[1, 3, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0]
]
NDD_month = [8, 7, 11]
dates =
for i in range(len(number_of_payments)):
dates.append([NDD_month[i]])
for j in range(1, len(number_of_payments[i])):
dates[i].append((dates[i][j-1] + 12 - number_of_payments[i][j-1]) % 12)
print(dates)


This gives me



[[8, 8, 7, 7, 6, 5, 4, 4, 11, 10, 10, 8], [7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 4], [11, 10, 7, 6, 6, 6, 5, 4, 3, 2, 2, 1]]



Now I try to do the same thing but with the entire set of data but this is what I get (I will paste my whole code below):



# Import modules
import numpy as np
import pandas as pd
import datetime

# Import data file
df = pd.read_csv("Paystring Data.csv")
df.head()

# Get column data into a list
x = list(df)

# Append column data into cpi, NDD, and as of dates
NDD = df['NDD 8/31']
cpi = df['Contractual PI']
as_of_date = pd.Series(pd.to_datetime(df.columns.str[:8], errors='coerce'))
as_of_date = as_of_date[1:13]
NDD_month = pd.to_datetime(NDD, errors = 'coerce').dt.month.tolist()
# print(as_of_date.dt.month)

# Get cash flows
cf = df.iloc[:,1:13].replace('[^0-9.]', '', regex=True).astype(float)
cf = cf.values

# Calculate number of payments
number_of_payments =
for i in range(len(cpi)):
number_of_payments.append((cf[:i + 1] / cpi[i]).astype(int))
np.vstack(number_of_payments).tolist()

# Calculate the new NDD dates
dates =
for i in range(len(number_of_payments)):
dates.append([NDD_month[i]])
for j in range(1, len(number_of_payments[i])):
dates[i].append((dates[i][j-1] + 12 - number_of_payments[i][j-1]) % 12)
print(dates[0])


This just gives me [8]



When it should be [8, 8, 7, 7, 6, 5, 4, 4, 11, 10, 10, 8].



Anyone know how to fix this?










share|improve this question













I want to do this in python, here is a small example:



number_of_payments = [
[0, 1, 0, 1, 1, 1, 0, 5, 1, 0, 2, 1],
[0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0],
[1, 3, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0]
]
NDD_month = [8, 7, 11]
dates =
for i in range(len(number_of_payments)):
dates.append([NDD_month[i]])
for j in range(1, len(number_of_payments[i])):
dates[i].append((dates[i][j-1] + 12 - number_of_payments[i][j-1]) % 12)
print(dates)


This gives me



[[8, 8, 7, 7, 6, 5, 4, 4, 11, 10, 10, 8], [7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 4], [11, 10, 7, 6, 6, 6, 5, 4, 3, 2, 2, 1]]



Now I try to do the same thing but with the entire set of data but this is what I get (I will paste my whole code below):



# Import modules
import numpy as np
import pandas as pd
import datetime

# Import data file
df = pd.read_csv("Paystring Data.csv")
df.head()

# Get column data into a list
x = list(df)

# Append column data into cpi, NDD, and as of dates
NDD = df['NDD 8/31']
cpi = df['Contractual PI']
as_of_date = pd.Series(pd.to_datetime(df.columns.str[:8], errors='coerce'))
as_of_date = as_of_date[1:13]
NDD_month = pd.to_datetime(NDD, errors = 'coerce').dt.month.tolist()
# print(as_of_date.dt.month)

# Get cash flows
cf = df.iloc[:,1:13].replace('[^0-9.]', '', regex=True).astype(float)
cf = cf.values

# Calculate number of payments
number_of_payments =
for i in range(len(cpi)):
number_of_payments.append((cf[:i + 1] / cpi[i]).astype(int))
np.vstack(number_of_payments).tolist()

# Calculate the new NDD dates
dates =
for i in range(len(number_of_payments)):
dates.append([NDD_month[i]])
for j in range(1, len(number_of_payments[i])):
dates[i].append((dates[i][j-1] + 12 - number_of_payments[i][j-1]) % 12)
print(dates[0])


This just gives me [8]



When it should be [8, 8, 7, 7, 6, 5, 4, 4, 11, 10, 10, 8].



Anyone know how to fix this?







python list numpy matrix






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 1:31









Snorrlaxxx

12811




12811












  • I suggest you step through a debugger. Your precondition is that your number_of_payments is a list of lists; does that precondition hold in your real code?
    – erip
    Nov 20 at 1:43












  • @erip No idea how to do that in jupyer notebook nor with python I am a c++ programmer
    – Snorrlaxxx
    Nov 20 at 1:43










  • A jupyter notebook is basically an interactive debugger.
    – erip
    Nov 20 at 1:44










  • @erip Do you have any idea what could be causing these issues?
    – Snorrlaxxx
    Nov 20 at 1:45










  • If I had to guess, your data is malformed. You might want to step through a debugger, though.
    – erip
    Nov 20 at 1:46


















  • I suggest you step through a debugger. Your precondition is that your number_of_payments is a list of lists; does that precondition hold in your real code?
    – erip
    Nov 20 at 1:43












  • @erip No idea how to do that in jupyer notebook nor with python I am a c++ programmer
    – Snorrlaxxx
    Nov 20 at 1:43










  • A jupyter notebook is basically an interactive debugger.
    – erip
    Nov 20 at 1:44










  • @erip Do you have any idea what could be causing these issues?
    – Snorrlaxxx
    Nov 20 at 1:45










  • If I had to guess, your data is malformed. You might want to step through a debugger, though.
    – erip
    Nov 20 at 1:46
















I suggest you step through a debugger. Your precondition is that your number_of_payments is a list of lists; does that precondition hold in your real code?
– erip
Nov 20 at 1:43






I suggest you step through a debugger. Your precondition is that your number_of_payments is a list of lists; does that precondition hold in your real code?
– erip
Nov 20 at 1:43














@erip No idea how to do that in jupyer notebook nor with python I am a c++ programmer
– Snorrlaxxx
Nov 20 at 1:43




@erip No idea how to do that in jupyer notebook nor with python I am a c++ programmer
– Snorrlaxxx
Nov 20 at 1:43












A jupyter notebook is basically an interactive debugger.
– erip
Nov 20 at 1:44




A jupyter notebook is basically an interactive debugger.
– erip
Nov 20 at 1:44












@erip Do you have any idea what could be causing these issues?
– Snorrlaxxx
Nov 20 at 1:45




@erip Do you have any idea what could be causing these issues?
– Snorrlaxxx
Nov 20 at 1:45












If I had to guess, your data is malformed. You might want to step through a debugger, though.
– erip
Nov 20 at 1:46




If I had to guess, your data is malformed. You might want to step through a debugger, though.
– erip
Nov 20 at 1:46












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










In your "small example", number_of_payments is a list of list of ints:



number_of_payments = [
[0, 1, 0, 1, 1, 1, 0, 5, 1, 0, 2, 1],
[0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0],
[1, 3, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0]
]


In your real code, number_of_payments is a list of ints:



number_of_payments = 
for i in range(len(cpi)):
number_of_payments.append((cf[:i + 1] / cpi[i]).astype(int))


It seems like you need to figure out how to make your real number_of_payments look like your sample one through nesting.






share|improve this answer























  • I see, could you show me how to figure that out please?
    – Snorrlaxxx
    Nov 20 at 1:54










  • I don't know your domain. You'll have to figure out that logic.
    – erip
    Nov 20 at 1:55










  • What do you mean domain? I am confused what you mean
    – Snorrlaxxx
    Nov 20 at 1:55










  • I guess I'm politely saying that I'm not going to do your job for free.
    – erip
    Nov 20 at 1:56










  • Okay, how much do you want?
    – Snorrlaxxx
    Nov 20 at 1:56











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384990%2fstrange-output-error-following-example-of-matirx-vector-operation-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










In your "small example", number_of_payments is a list of list of ints:



number_of_payments = [
[0, 1, 0, 1, 1, 1, 0, 5, 1, 0, 2, 1],
[0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0],
[1, 3, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0]
]


In your real code, number_of_payments is a list of ints:



number_of_payments = 
for i in range(len(cpi)):
number_of_payments.append((cf[:i + 1] / cpi[i]).astype(int))


It seems like you need to figure out how to make your real number_of_payments look like your sample one through nesting.






share|improve this answer























  • I see, could you show me how to figure that out please?
    – Snorrlaxxx
    Nov 20 at 1:54










  • I don't know your domain. You'll have to figure out that logic.
    – erip
    Nov 20 at 1:55










  • What do you mean domain? I am confused what you mean
    – Snorrlaxxx
    Nov 20 at 1:55










  • I guess I'm politely saying that I'm not going to do your job for free.
    – erip
    Nov 20 at 1:56










  • Okay, how much do you want?
    – Snorrlaxxx
    Nov 20 at 1:56















up vote
2
down vote



accepted










In your "small example", number_of_payments is a list of list of ints:



number_of_payments = [
[0, 1, 0, 1, 1, 1, 0, 5, 1, 0, 2, 1],
[0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0],
[1, 3, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0]
]


In your real code, number_of_payments is a list of ints:



number_of_payments = 
for i in range(len(cpi)):
number_of_payments.append((cf[:i + 1] / cpi[i]).astype(int))


It seems like you need to figure out how to make your real number_of_payments look like your sample one through nesting.






share|improve this answer























  • I see, could you show me how to figure that out please?
    – Snorrlaxxx
    Nov 20 at 1:54










  • I don't know your domain. You'll have to figure out that logic.
    – erip
    Nov 20 at 1:55










  • What do you mean domain? I am confused what you mean
    – Snorrlaxxx
    Nov 20 at 1:55










  • I guess I'm politely saying that I'm not going to do your job for free.
    – erip
    Nov 20 at 1:56










  • Okay, how much do you want?
    – Snorrlaxxx
    Nov 20 at 1:56













up vote
2
down vote



accepted







up vote
2
down vote



accepted






In your "small example", number_of_payments is a list of list of ints:



number_of_payments = [
[0, 1, 0, 1, 1, 1, 0, 5, 1, 0, 2, 1],
[0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0],
[1, 3, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0]
]


In your real code, number_of_payments is a list of ints:



number_of_payments = 
for i in range(len(cpi)):
number_of_payments.append((cf[:i + 1] / cpi[i]).astype(int))


It seems like you need to figure out how to make your real number_of_payments look like your sample one through nesting.






share|improve this answer














In your "small example", number_of_payments is a list of list of ints:



number_of_payments = [
[0, 1, 0, 1, 1, 1, 0, 5, 1, 0, 2, 1],
[0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0],
[1, 3, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0]
]


In your real code, number_of_payments is a list of ints:



number_of_payments = 
for i in range(len(cpi)):
number_of_payments.append((cf[:i + 1] / cpi[i]).astype(int))


It seems like you need to figure out how to make your real number_of_payments look like your sample one through nesting.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 1:59

























answered Nov 20 at 1:52









erip

10.1k43674




10.1k43674












  • I see, could you show me how to figure that out please?
    – Snorrlaxxx
    Nov 20 at 1:54










  • I don't know your domain. You'll have to figure out that logic.
    – erip
    Nov 20 at 1:55










  • What do you mean domain? I am confused what you mean
    – Snorrlaxxx
    Nov 20 at 1:55










  • I guess I'm politely saying that I'm not going to do your job for free.
    – erip
    Nov 20 at 1:56










  • Okay, how much do you want?
    – Snorrlaxxx
    Nov 20 at 1:56


















  • I see, could you show me how to figure that out please?
    – Snorrlaxxx
    Nov 20 at 1:54










  • I don't know your domain. You'll have to figure out that logic.
    – erip
    Nov 20 at 1:55










  • What do you mean domain? I am confused what you mean
    – Snorrlaxxx
    Nov 20 at 1:55










  • I guess I'm politely saying that I'm not going to do your job for free.
    – erip
    Nov 20 at 1:56










  • Okay, how much do you want?
    – Snorrlaxxx
    Nov 20 at 1:56
















I see, could you show me how to figure that out please?
– Snorrlaxxx
Nov 20 at 1:54




I see, could you show me how to figure that out please?
– Snorrlaxxx
Nov 20 at 1:54












I don't know your domain. You'll have to figure out that logic.
– erip
Nov 20 at 1:55




I don't know your domain. You'll have to figure out that logic.
– erip
Nov 20 at 1:55












What do you mean domain? I am confused what you mean
– Snorrlaxxx
Nov 20 at 1:55




What do you mean domain? I am confused what you mean
– Snorrlaxxx
Nov 20 at 1:55












I guess I'm politely saying that I'm not going to do your job for free.
– erip
Nov 20 at 1:56




I guess I'm politely saying that I'm not going to do your job for free.
– erip
Nov 20 at 1:56












Okay, how much do you want?
– Snorrlaxxx
Nov 20 at 1:56




Okay, how much do you want?
– Snorrlaxxx
Nov 20 at 1:56


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384990%2fstrange-output-error-following-example-of-matirx-vector-operation-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno