简体   繁体   English

“类型错误:无法理解数据类型”比较 dtype np.datetime64

[英]"TypeError: data type not understood" comparing dtype np.datetime64

as per this response , I'm comparing a subtype datetime64[ns, US/Central] to np.datetime64 :根据此回复,我将子类型datetime64[ns, US/Central]np.datetime64进行比较

        columns = self._obj.columns
        for dtype in self._obj.dtypes:
            print("testing:", dtype)
            if np.issubdtype(dtype, np.datetime64):
               ...
testing: datetime64[ns, US/Central]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-2ddcb445a42c> in <module>
...
...(self, dataframe, *dataframes)
    147         for dtype in self._obj.dtypes:
    148             print("testing:", dtype)
--> 149             if np.issubdtype(dtype, np.datetime64):

...
TypeError: data type not understood

if run head() in this dataframe I get normal timestamps in that column:如果在这个数据帧中运行 head() ,我会在该列中得到正常的时间戳:

ts
0   2020-02-22 12:11:40-06:00   NaN
1   2020-02-22 12:11:41-06:00   NaN

pandas.熊猫。 version '1.0.2'版本“1.0.2”
numpy.麻木。 version '1.18.1'版本“1.18.1”
any ideas?有任何想法吗? thanks谢谢

not ideal, but I fixed this with string comparison:不理想,但我用字符串比较解决了这个问题:

if (str(dtype).startswith("datetime64")):
   ...

转换 unix 时间戳的 np.array (dtype ' <u21') to np.datetime64< div><div id="text_translate"><p> 我正在寻找处理大量数据,因此我对计算以下内容的最快方法感兴趣:</p><p> 我将以下 np.array 作为 np.ndarray 的一部分,我想将其从“&lt;U21”转换为“np.datetime64”(毫秒)。</p><p> 当我在一个条目上执行以下代码时,它可以工作:</p><pre> tmp_array[:,0][0].astype(int).astype('datetime64[ms]')</pre><p> 结果:numpy.datetime64('2019-10-09T22:54:00.000')</p><p> 当我像这样在子数组上执行相同的操作时:</p><pre> tmp_array[:,0] = tmp_array[:,0].astype(int).astype('datetime64[ms]')</pre><p> 我总是收到以下错误:</p><pre> RuntimeError: The string provided for NumPy ISO datetime formatting was too short, with length 21</pre><p> numpy 版本 1.22.4</p><pre> array(['1570661640000', '1570661700000', '1570661760000'],dtype='&lt;U21')</pre></div></u21')> - Converting np.array of unix timestamps (dtype '<U21') to np.datetime64

暂无
暂无

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

相关问题 转换 unix 时间戳的 np.array (dtype ' <u21') to np.datetime64< div><div id="text_translate"><p> 我正在寻找处理大量数据,因此我对计算以下内容的最快方法感兴趣:</p><p> 我将以下 np.array 作为 np.ndarray 的一部分,我想将其从“&lt;U21”转换为“np.datetime64”(毫秒)。</p><p> 当我在一个条目上执行以下代码时,它可以工作:</p><pre> tmp_array[:,0][0].astype(int).astype('datetime64[ms]')</pre><p> 结果:numpy.datetime64('2019-10-09T22:54:00.000')</p><p> 当我像这样在子数组上执行相同的操作时:</p><pre> tmp_array[:,0] = tmp_array[:,0].astype(int).astype('datetime64[ms]')</pre><p> 我总是收到以下错误:</p><pre> RuntimeError: The string provided for NumPy ISO datetime formatting was too short, with length 21</pre><p> numpy 版本 1.22.4</p><pre> array(['1570661640000', '1570661700000', '1570661760000'],dtype='&lt;U21')</pre></div></u21')> - Converting np.array of unix timestamps (dtype '<U21') to np.datetime64 在numpy数组中,dtype np.datetime64或pandas对象datetime.date更快吗? - In a numpy array is dtype np.datetime64 or pandas object datetime.date faster? 如何将Pandas数据框列从np.datetime64转换为datetime? - How to convert a Pandas data frame column from np.datetime64 to datetime? 在 Python Dictionary Key 中提取日期,其中 Key 的类型为 np.datetime64 - Extract Date in Python Dictionary Key, where Key is of type np.datetime64 TypeError: 数据类型“int64”不被理解 - TypeError: data type ' int64' not understood 使用`np.datetime64`对象填充空Numpy数组时出错 - Error filling in empty Numpy array with `np.datetime64` objects 从&#39;to_julian_date()&#39;转换为&#39;np.datetime64&#39; - convert from 'to_julian_date()' to 'np.datetime64' 将pandas dataframe列转换为np.datetime64 - convert pandas dataframe column to np.datetime64 如何只比较np.datetime64到一个月? - How to compare np.datetime64 up to month only? 从 np.datetime64 转换为浮点年 - convert from np.datetime64 to float year
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM