TypeError:无法将DatetimeIndex转换为dtype datetime64 [us]
import time
import pandas as pd
import sqlalchemy
from sqlalchemy import create_engine
import psycopg2
engine1 = create_engine('postgresql://postgres:postgres@localhost:5432/db1')
engine2 = create_engine('postgresql://postgres:postgres@localhost:5432/db2')
df = pd.read_sql_query("""
select id,text,created_date,is_read,to_user_id,is_new,url,text_ar,text_en from notifications_notification where created_date <= NOW() - interval '1 month'""",engine1)
df= df.rename(columns={ 'id':'original_id','text':'text','created_date':'created_date','is_read':'is_read','to_user_id':'to_user_id','is_new':'is_new','url':'url','text_ar':'text_ar','text_en':'text_en'})
df.index = df.index.values.astype('datetime64[us]')
df.to_sql(name='notifications_notification_archive',con=engine2,if_exists='append')
答案 0 :(得分:0)
现在已修复,我使用了以下几行:
df['created_date'] = pd.to_datetime(df['created_date'])
df['created_date'] = df['created_date'].astype('datetime64[us]')
df.set_index('created_date', inplace=True)
df.to_sql(name='notifications_notification_archive',con=engine2,if_exists='append')