• Register
1 vote
872 views

Problem:

I have a data frame to count my daily costs and when I try to input this to my cost using python script it always shows the error:

TypeError: Object of type 'int64' is not JSON serializable

The script I wrote is:

update_list = []
for i in range(len(cost)):
    update_data = {
        'name': cost['entity_name'].iloc[i],
        'count__c': cost['count'].iloc[i] 
    }
    update_list.append(update_data)

all_data_cursor = all_datapull.costforce_login()
all_data_cursor.bulk.Account.update(update_list)

Can anyone help me out to fix this thing. 

 

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

1 vote

Answer:

Jason did not recognise the numpy data types. So you can use encoder to solve this problem:

import json
import numpy as np

class Npencoder(json.JSONencoder):
    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        elif isinstance(obj, np.floating):
            return float(obj)
        elif isinstance(obj, np.ndarray):
            return obj.tolist()
        else:
            return super(Npencoder, self).default(obj)
(

 Write your codes here
)
json.dumps(data, cls=Npencoder)

I hope you understand what I said. Hope this helps you.

Happy coding.

11 6 4
34,950 points

Related questions

0 votes
1 answer 3 views
3 views
Problem: I am looking to make a loop so that for each element in the list, it will get broken down into each of it's characters. For example, say the number 137 was in the list then it would be turned into [1,3,7]. Then I want to add these numbers together (I ... it). However, I keep getting the error message TypeError: 'int' object is not iterable when I try and run this. What am I doing wrong?
asked 3 days ago Mashhoodch 9.1k points
0 votes
1 answer 17 views
17 views
Problem: I’m working on the count problem, and have come up with the following code: def count(sequence, item): found = 0 for i in len(sequence): if sequence[i] == item: found = found + 1 return found
asked Jan 22 Mashhoodch 9.1k points
0 votes
1 answer 28 views
28 views
On making a small program for math there returned an error "TypeError: 'NoneType' object is not subscriptable. Never before has been seen, this error, so no idea what it means. import math print("The format you should consider:") print str("value 1a")+str(" + ")+ ... /Nathan/Documents/Python/New thing", line 16, in <module> a = lista[1] - lista[0] TypeError: 'NoneType' object is not subscriptable
asked Oct 24, 2020 psandprop 2.4k points
0 votes
1 answer 9 views
9 views
Problem: What the traceback error is pointing out is the misuse of for statement: for i in Updt():. for in python 3 i "Python's for statement iterates? How function' object is not iterable. from getWords import getWords unique = [] def findUniqueWords(words): for word in words
asked Feb 15 Mashhoodch 9.1k points
0 votes
1 answer 6 views
6 views
Problem: I've programmed these for calculating Variance import pandas as pd import xlrd import numpy as np import matplotlib.pyplot as plt credit_card=pd.read_csv("default_of_credit_card_clients_Data.csv",skiprows=1) print(credit_card.head()) for col in credit_card: var[col]=np.var(credit_card(col)) print(var)
asked 3 days ago Mashhoodch 9.1k points
0 votes
1 answer 12 views
12 views
Problem: I am following instructions for a class homework assignment and I am supposed to look up the top 200 most frequently used words in a text file. Here's the last part of the code: fdist1 = FreqDist(NSmyText) vocab=fdist1.keys() vocab[:200] But when I ... 1, in <module> TypeError: 'dict_keys' object is not subscriptable Any suggestions on how to fix this so it can correctly return an answer?
asked 5 days ago Mashhoodch 9.1k points
0 votes
1 answer 65 views
65 views
Problem: when I make a function call to this method create({'1','2'}) I get an TypeError: 'set' object is not subscriptable error on line 'AWS': list(map(lambda id: f"arn:aws:iam::{id}:root", ids[i:i + 200])). Coming from a java background, is this somehow related to typecasting? Does the error mean that I'm passing a set data structure to a list function? How could can this be resovled?
asked Feb 19 Mashhoodch 9.1k points
0 votes
1 answer 7 views
7 views
Problem: I am novice to Python and following a tutorial. There is an example of list in the tutorial : example = list('easyhoss') Now, In tutorial, example= ['e','a',...,'s']. But in my case I am getting following error: >>> example = list('easyhoss') ... , line 1, in <module> TypeError: 'list' object is not callable Please tell me where I am wrong. I searched SO this but it is different.
asked Feb 18 Mashhoodch 9.1k points
0 votes
1 answer 17 views
17 views
Problem: I'm making a small program for math (no particular reason, just kind of wanted to) and I ran into the error "TypeError: 'NoneType' object is not subscriptable. I have never before seen this error, so I have no idea what it means.
asked Feb 4 Mashhoodch 9.1k points
0 votes
1 answer 13 views
13 views
Problem: The Python &ldquo;typeerror: 'list' object is not callable&rdquo; error is raised when I try to access a list as if it were a function. To solve this error, make sure square brackets are used to access or change values in a list rather than curly brackets.
asked Jan 24 Mashhoodch 9.1k points