0

Question: How can we replace the NULL values of a datetime column in Pandas DataFrame to something like 1900-01-01 00:00:00.000?

I am using Pandas data frame to import a large data file into a SQL Server 2019 table. My following code correctly replaces NULL values of numeric columns to 0, and NULL values of object (string) columns to empty string. But it does not change the NULL values of the datetime columns to 1900-01-01 00:00:00.000:

import sqlalchemy as sq
import datetime
import pandas as pd
import numpy as np

............
............
c = df.select_dtypes(np.datetime64).columns
df[c] = df[c].fillna('1900-01-01 00:00:00.000')
df.fillna('', inplace=True)

Ref: pandas.DataFrame.select_dtypes and this SO post

2
  • In the line df[cols] = df[c].fillna('1900-01-01 00:00:00.000'), what is cols? Commented Apr 15, 2022 at 14:53
  • 1
    @PeterLeimbigler That was a typo. I corrected that. Thank you for pointing it out early. Commented Apr 15, 2022 at 14:56

1 Answer 1

0

I guess the issue is that 'fillna' doesn't pick up 'NULL' as this is a sql keyword for NA values - but not for pandas. Maybe try replacing directly that string in your code?

df[c].replace('NULL','1900-01-01 00:00:00.000')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.