我正在尝试将python数据框架加载到Watson函数Action中。我犯了以下错误。你知道吗
{ "error": "Internal error. Object of type module is not JSON serializable" }
我正在Watson Function Action中加载一个Watson Notebook数据框,创建一个返回Json的函数,从笔记本中我测试它,如果Json返回它就会工作,但我不知道我是否需要函数中的某些内容
数据帧代码
dfData = dfData[['SOURCE','NUMBER_ID','DATE_OPEN','DATE_CLOSE','COMPANY','CATEGORY','SUBCATEGORY','LOGICAL_NAME','CLR_TXT_SERVICIO','STATUS',]]
jsondf = dfData.to_dict(orient='record')
print(jsondf)
I have tried with:
#jsondf = dfData.to_json( orient='records')
print(jsondf)
#jsondf = jsondf.dumps(dfData, cls=JSONEncoder)
print(jsondf)
但都不管用
import sys
import types
import pandas as pd
import numpy as np
import ibm_boto3
import json as jsondf
import dateutil.relativedelta
from pandas import DataFrame
from botocore.client import Config
from datetime import datetime, date, time, timedelta
def main(dict):
def __iter__(self):
client_22e74e133ed74557a9183bca634893be = ibm_boto3.client(service_name='s3',
ibm_api_key_id='XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
ibm_auth_endpoint="https://iam.bluemix.net/oidc/token",
config=Config(signature_version='oauth'),
endpoint_url='https://s3-api.us-geo.objectstorage.service.networklayer.com')
streaming_body_14 = client_22e74e133ed74557a9183bca634893be.get_object(Bucket='clarotp-donotdelete-pr-jfrmyss7bazrmn', Key='Vista_Mayo.xlsx')['Body']
# add missing __iter__ method so pandas accepts body as file-like object
if not hasattr(streaming_body_14, "__iter__"): streaming_body_14.__iter__ = types.MethodType( __iter__, streaming_body_14 )
#convertir el streaming body del archivo Excel a dataframe
def load_df(fileobject,sheetname):
excelFile = pd.ExcelFile(fileobject)
dfData = excelFile.parse(sheetname)
return dfData
#se crea el dateframe (dfData)
dfData = load_df(streaming_body_14, 'Exportar Hoja de Trabajo')
dfData = dfData[dfData['NUMBER_ID'].str.contains('IM1010935', case=False)]
jsondf = dfData.to_dict(orient='record')
return {'message': [jsondf] }
这是我创建的函数,但它不起作用
结果:
{ "error": "Internal error. Object of type module is not JSON serializable" } Logs: []
如果没有完整的回溯,就很难知道是哪行代码导致了问题。但是,从该错误消息中:
我们知道代码试图将模块对象转换为json。你知道吗
这条线
根据您当前的代码,这一行相当于
注意,该值包含一个模块对象,不能序列化为json。你知道吗
也许应该缩进return语句,以便
jsondf
是在这一行中分配的值?你知道吗所以你有
不是
无论如何,我建议您不要在代码中使用已导入的模块的名称作为变量。你知道吗
相关问题 更多 >
编程相关推荐