isin 不处理 datetimes 的 ndarray 除了弃用警告

isin not working on ndarray of datetimes except with deprecation warning

我有以下两个日期时间数组:

日期A:

        datesA
array([datetime.datetime(2000, 1, 4, 0, 0),
       datetime.datetime(2000, 1, 5, 0, 0),
       datetime.datetime(2000, 1, 6, 0, 0),
       datetime.datetime(2000, 1, 7, 0, 0),
       datetime.datetime(2000, 1, 8, 0, 0),
       datetime.datetime(2000, 1, 9, 0, 0),
       datetime.datetime(2000, 1, 10, 0, 0),
       datetime.datetime(2000, 1, 11, 0, 0),
       datetime.datetime(2000, 1, 12, 0, 0)], dtype=object)

和日期 B:

datesB
array([datetime.datetime(2000, 1, 4, 0, 0, tzinfo=<UTC>),
       datetime.datetime(2000, 1, 5, 0, 0, tzinfo=<UTC>),
       datetime.datetime(2000, 1, 6, 0, 0, tzinfo=<UTC>),
       datetime.datetime(2000, 1, 7, 0, 0, tzinfo=<UTC>),
       datetime.datetime(2000, 1, 10, 0, 0, tzinfo=<UTC>),
       datetime.datetime(2000, 1, 11, 0, 0, tzinfo=<UTC>),
       datetime.datetime(2000, 1, 12, 0, 0, tzinfo=<UTC>),
       datetime.datetime(2000, 1, 13, 0, 0, tzinfo=<UTC>),
       datetime.datetime(2000, 1, 14, 0, 0, tzinfo=<UTC>)], dtype=object)

我想在 datesA 中找到不在 datesB 中的日期。使用 ~isin() 如下所示 returns True 用于所有行,而不仅仅是不在 datesB 中的行:

datesA_not_in_datesB = ~np.isin(datesA,datesB)

datesA_not_in_datesB.reshape(-1,1)
array([[ True],
       [ True],
       [ True],
       [ True],
       [ True],
       [ True],
       [ True],
       [ True],
       [ True]])

datesA 第 4 行和第 5 行(datetime.datetime(2000, 1, 8, 0, 0)datetime.datetime(2000 , 1, 9, 0, 0) ) 是唯一不在 datesB 中且应该 return True.

的记录

我发现 isin() 这个问题不适用于那些帖子中报告的日期时间:

有人在上面的帖子中建议的修复方法是:

datesA_not_in_datesB = ~np.isin(datesA.astype('datetime64[ns]'),datesB.astype('datetime64[ns]'))
​
C:\Users\Username\anaconda3\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
  """Entry point for launching an IPython kernel.

datesA_not_in_datesB.reshape(-1,1)
array([[False],
       [False],
       [False],
       [False],
       [ True],
       [ True],
       [False],
       [False],
       [False]])

除了我收到一条警告消息外,这有效:

DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future """Entry point for launching an IPython kernel.

我已经尝试了一些方法来从 datesB 中删除时区 .replace(tzinfo=None) 信息以使 isnan 工作而不必使用 .astype('datetime64[ns]') 并找到没有 DeprecationWarning[= 的解决方案56=] 但无济于事。

有人可以就如何获得与

相同的结果提出建议吗
datesA_not_in_datesB = ~np.isin(datesA.astype('datetime64[ns]'),datesB.astype('datetime64[ns]'))

但不会导致 DeprecationWarning?

非常感谢您抽出宝贵时间来帮助解决这个问题。

我从日期时间对象中删除了 tzinfo=<UTC>

此代码给了我零个警告..

抱歉,如果我错过了重点!

import numpy as np
import datetime

from pytz import UTC

datesA = np.array([datetime.datetime(2000, 1, 4, 0, 0),
    datetime.datetime(2000, 1, 5, 0, 0),
    datetime.datetime(2000, 1, 6, 0, 0),
    datetime.datetime(2000, 1, 7, 0, 0),
    datetime.datetime(2000, 1, 8, 0, 0),
    datetime.datetime(2000, 1, 9, 0, 0),
    datetime.datetime(2000, 1, 10, 0, 0),
    datetime.datetime(2000, 1, 11, 0, 0),
    datetime.datetime(2000, 1, 12, 0, 0)], dtype=object)

datesB = np.array([datetime.datetime(2000, 1, 4, 0, 0, tzinfo=UTC),
    datetime.datetime(2000, 1, 5, 0, 0, tzinfo=UTC),
    datetime.datetime(2000, 1, 6, 0, 0, tzinfo=UTC),
    datetime.datetime(2000, 1, 7, 0, 0, tzinfo=UTC),
    datetime.datetime(2000, 1, 10, 0, 0, tzinfo=UTC),
    datetime.datetime(2000, 1, 11, 0, 0, tzinfo=UTC),
    datetime.datetime(2000, 1, 12, 0, 0, tzinfo=UTC),
    datetime.datetime(2000, 1, 13, 0, 0, tzinfo=UTC),
    datetime.datetime(2000, 1, 14, 0, 0, tzinfo=UTC)], dtype=object)

datesB = np.array([d.replace(tzinfo=None) for d in datesB])

datesA_not_in_datesB = ~np.isin(datesA,datesB)

print(datesA_not_in_datesB)
>>> [False False False False  True  True False False False]

reshaped = datesA_not_in_datesB.reshape(-1,1)

print(reshaped)

>>> [[False]
 [False]
 [False]
[False]
[ True]
[ True]
[False]
[False]
[False]]