Tyomik_mnemonic
Tyomik_mnemonic

Reputation: 956

datetime confusing when pd.DataFrame to SQL Server INSERT

pd.DataFrame has Date column (has datetime data type) with style such as: %dd.%mm.%yyyy #for example 03.08.2020 but after INSERT to SQL Server df.to_sql(con_param) result in RDB looks like 08.03.2020 instead 03.08.2020. Problem is not linear, cause for example 31.07.2020 looks like 31.07.2020. Local of datebase is SQL_Latin1_General_CP1_CI_AS. What is wrong? UPD to sql block fyi

correct_df = pd.read_csv(path)
correct_df = correct_df.astype({
                           'Req_Date': 'datetime64', 'Date': 'datetime64'})
                           
 correct_df.to_sql(con=conn, name='table', schema='schema', if_exists='append', index=False)

Upvotes: 0

Views: 1064

Answers (1)

Tyomik_mnemonic
Tyomik_mnemonic

Reputation: 956

So, thanks to Panagiotis Kanavos messages. If you face with a problem like that, you have to set format of the date string of csv in your DateFrime. For example

df['Date'] = pd.to_datetime(df['Date'], format='%d.%m.%Y') #format=f'{your_date_style}'

It can works. It works for me.

Upvotes: 1

Related Questions