简体   繁体   English

Pandas DataFrame - 用一些旧日期替换日期时间列的 NULL 值

[英]Pandas DataFrame - Replace NULL value of Datetime column with some old date

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 ?问题:我们如何将 Pandas Pandas DataFramedatetime时间列的NULL值替换为类似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.我正在使用Pandas数据框将大型数据文件导入到SQL Server 2019表中。 My following code correctly replaces NULL values of numeric columns to 0, and NULL values of object (string) columns to empty string.我的以下代码正确地将数字列的 NULL 值替换为 0,并将 object(字符串)列的 NULL 值替换为空字符串。 But it does not change the NULL values of the datetime columns to 1900-01-01 00:00:00.000 :但它不会将日期时间列的 NULL 值更改为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参考: pandas.DataFrame.select_dtypes和这篇文章

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?我想问题是“fillna”没有选择“NULL”,因为这是NA 值的 sql 关键字 - 但不是 pandas。也许尝试直接替换代码中的那个字符串?

df[c].replace('NULL','1900-01-01 00:00:00.000')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM