Python 如何避免在 numpy where 子句后将数据类型从日期更改为纳秒

Python how to avoid data type from date to nanosecond change after numpy where clause

我有一个带日期的数据集,我想做的是将任何早于 2000 年的日期值替换为 2000-12-31 12:00:00,同时保持其他日期不变。但是,我的代码结果在 false 语句

中将日期值转换为纳秒

我的代码:

   data['date'] = np.where(data['date']<=datetime(2000,12,31,12,0,0) , pd.to_datetime('2000-12-31 00:00:00') , data['date'])

我的datatable:

   Date                        value
   1990-01-01 12:00:00           1
   2020-12-31 12:00:00           2 

预计table

   Date                        value
   2000-12-31 12:00:00           1
   2020-12-31 12-00:00           2

现在的实际 table 结果

   Date                        Value  
   2000-12-31 12:00:00           1
   1609416000000                 2 

您可以简单地使用 pandas 中的 .loc[] 函数来仅修改您感兴趣的行,如下所示:

import pandas as pd
import numpy as np
from datetime import datetime

# Creating your sample dataframe
data = pd.DataFrame({'date': [pd.to_datetime('1990-01-01 12:00:00'), pd.to_datetime('2020-12-31 12:00:00')], 'Value': [1,2]})

# filtering your rows to only take the ones before 2000-12-31 12:00:00 and replacing them with 2000-12-31 00:00:00
data.loc[data['date']<=datetime(2000,12,31,12,0,0), 'date'] = pd.to_datetime('2000-12-31 00:00:00')

data.head()

Returns:

 date                   Value
 2000-12-31 00:00:00    1
 2020-12-31 12:00:00    2