sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)无法适应类型“ dict”

时间:2019-12-03 19:14:39

标签: python python-3.x database amazon-redshift

我正在尝试使用下面的代码行使用pandas将插入批量插入redshift db。

df.to_sql(table_name, conn, index=False, if_exists='replace')

出现以下错误。

       cursor, statement, parameters, context
  File "/home/ubuntu/trell/env/lib/python3.6/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 854, in do_executemany
    cursor.executemany(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'dict'
[SQL: INSERT INTO events_20180625 (event_date, event_timestamp, event_name, event_params, event_previous_timestamp, user_pseudo_id, user_properties, user_first_touch_timestamp, device, geo, app_info, traffic_source, stream_id, platform) VALUES (%(event_date)s, %(event_timestamp)s, %(event_name)s, %(event_params)s, %(event_previous_timestamp)s, %(user_pseudo_id)s, %(user_properties)s, %(user_first_touch_timestamp)s, %(device)s, %(geo)s, %(app_info)s, %(traffic_source)s, %(stream_id)s, %(platform)s)]
[parameters: ({'event_date': '20180625', 'event_timestamp': '1529948530245005', 'event_name': 'firebase_campaign', 'event_params': '{"medium": "(not set)", "firebase_event_origin": "app", "source": "(not set)", "firebase_screen_class": "TaleEmotionTagActivity", "item_name": "NARRA ... (24 characters truncated) ... ory": "Create Post", "error_value": "Festivals Around the World_CT", "firebase_previous_class": "TrailListActivity", "previous_app_version": "3.1.0"}', 'event_previous_timestamp': '1529948527702005', 'user_pseudo_id': 'ffb01b060c860317c9baba657ffe402d', 'user_properties': '{"first_open_time": "1529172000000", "first_open_time_set_timestamp_micros": "1529171511734000"}', 'user_first_touch_timestamp': '1529948488186000', 'device': {'category': 'mobile', 'mobile_brand_name': 'Samsung', 'mobile_model_name': 'SM-A500G', 'mobile_marketing_name': 'Galaxy A5', 'mobile_os_hardware_mode ... (81 characters truncated) ... ', 'advertising_id': '44f5dd05-1f78-461a-be16-f6c4b1665920', 'language': 'en-gb', 'is_limited_ad_tracking': 'No', 'time_zone_offset_seconds': '19800'}, 'geo': {'continent': 'Asia', 'country': 'India', 'region': 'Gujarat', 'city': 'Ahmedabad'}, 'app_info': {'id': 'app.trell', 'version': '3.3.8', 'firebase_app_id': '1:808772988865:android:8db93cca16602100', 'install_source': 'com.android.vending'}, 'traffic_source': {'name': '(direct)', 'medium': '(none)', 'source': '(direct)'}, 'stream_id': '1051639513', 'platform': 'ANDROID'}, 

尝试在具有字典值的列上进行以下操作,但仍在错误上方得到相同的结果。

df['event_params'] = list(map(lambda x: json.dumps(x), df['event_params']))
df['user_properties'] = list(map(lambda x: json.dumps(x), df['user_properties']))

任何领导都高度赞赏。谢谢。

0 个答案:

没有答案