将时间戳记/日期时间更改为整数的Numpy
不是什么问题,而是让我感到困惑的东西.
Not so much a question but something puzzling me.
我有一列日期看起来像这样:
I have a column of dates that looks something like this:
0 NaT
1 1996-04-01
2 2000-03-01
3 NaT
4 NaT
5 NaT
6 NaT
7 NaT
8 NaT
我想将NaT转换为静态值.(假设我将pda导入为pd,将numpy导入为np).
I'd like to convert it the NaTs to a static value. (Assume I imported pandas as pd and numpy as np).
如果我这样做:
mydata['mynewdate'] = mydata.mydate.replace(
np.NaN, pd.datetime(1994,6,30,0,0))
一切都好,我明白了
0 1994-06-30
1 1996-04-01
2 2000-03-01
3 1994-06-30
4 1994-06-30
5 1994-06-30
6 1994-06-30
7 1994-06-30
8 1994-06-30
但如果我这样做:
mydata['mynewdate'] = np.where(
mydata['mydate'].isnull(), pd.datetime(1994,6,30,0,0),mydata['mydate'])
我得到:
0 1994-06-30 00:00:00
1 828316800000000000
2 951868800000000000
3 1994-06-30 00:00:00
4 1994-06-30 00:00:00
5 1994-06-30 00:00:00
6 1994-06-30 00:00:00
7 1994-06-30 00:00:00
8 1994-06-30 00:00:00
此操作将原始的非空日期转换为整数.我认为可能是数据类型混合在一起,所以我这样做了:
This operation converts the original, non-null dates to integers. I thought there might be a mix-up of data types, so I did this:
mydata['mynewdate'] = np.where(
mydata['mydate'].isnull(), pd.datetime(1994,6,30,0,0),pd.to_datetime(mydata['mydate']))
仍然得到:
0 1994-06-30 00:00:00
1 828316800000000000
2 951868800000000000
3 1994-06-30 00:00:00
4 1994-06-30 00:00:00
5 1994-06-30 00:00:00
6 1994-06-30 00:00:00
7 1994-06-30 00:00:00
8 1994-06-30 00:00:00
请注意(不要问):是的,我有一个更好的替换null的解决方案.这个问题不是关于替换空值(因为标题表明不是),而是关于如何用numpy处理日期.我之所以问,是因为我将有更复杂的条件来选择将来要替换的日期,并认为numpy将在哪里完成这项工作.
Please note (and don't ask): Yes, I have a better solution for replacing nulls. This question is not about replacing nulls (as the title indicates that it is not) but how numpy where is handling dates. I ask because I will have more complex conditions to select dates to replace in the future, and thought numpy where would do the job.
有什么想法吗?
这是由于Numpy的 datetime64
,熊猫的 Timestamp
和/或datetime.datetime
.我通过将替换值从一开始就设置为 numpy.datetime64
来解决了该问题.
It's due to wonky interactions between Numpy's datetime64
, Pandas' Timestamp
, and/or datetime.datetime
. I fixed it by setting the replacement value to be a numpy.datetime64
from the start.
static_date = np.datetime64('1994-06-30')
# static_date = np.datetime64(pd.datetime(1994, 6, 30))
mydata.assign(
mynewdate=np.where(
mydata.mydate.isnull(),
static_date,
mydata.mydate
)
)
mydate mynewdate
0 NaT 1994-06-30
1 1996-04-01 1996-04-01
2 2000-03-01 2000-03-01
3 NaT 1994-06-30
4 NaT 1994-06-30
5 NaT 1994-06-30
6 NaT 1994-06-30
7 NaT 1994-06-30
8 NaT 1994-06-30