【问题标题】:Python 3: "TypeError: cannot astype a datetimelike from [datetime64[ns]] to [int32]" - Jupyter NotebookPython 3:“TypeError:无法从 [datetime64[ns]] 到 [int32] 键入 datetimelike” - Jupyter Notebook
【发布时间】:2019-10-11 21:54:43
【问题描述】:

我正在尝试在运行 Python 3 的 Windows 10 机器上运行 jupyter notebook 项目时遇到问题。我从这个函数中得到了上述错误:

buy_per_min = (buy
               .groupby([pd.Grouper(key='timestamp', freq='Min'), 'price'])
               .shares
               .sum()
               .apply(np.log)
               .to_frame('shares')
               .reset_index('price')
               .between_time(market_open, market_close)
               .groupby(level='timestamp', as_index=False, group_keys=False)
               .apply(lambda x: x.nlargest(columns='price', n=depth))
               .reset_index())
buy_per_min.timestamp = buy_per_min.timestamp.add(utc_offset).astype(int)
buy_per_min.info()

问题在于 buy_per_min.timestamp = buy_per_min.timestamp.add(utc_offset).astype(int) 行,但我不完全明白我为什么会得到它。这是完整的回溯:

TypeError                                 Traceback (most recent call last)
<ipython-input-28-396768b710c8> in <module>()
     10                .apply(lambda x: x.nlargest(columns='price', n=depth))
     11                .reset_index())
---> 12 buy_per_min.timestamp = buy_per_min.timestamp.add(utc_offset).astype(int)
     13 buy_per_min.info()

~\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\generic.py in astype(self, dtype, copy, errors, **kwargs)
   5689             # else, only a single dtype is given
   5690             new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors,
-> 5691                                          **kwargs)
   5692             return self._constructor(new_data).__finalize__(self)
   5693 

~\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\internals\managers.py in astype(self, dtype, **kwargs)
    529 
    530     def astype(self, dtype, **kwargs):
--> 531         return self.apply('astype', dtype=dtype, **kwargs)
    532 
    533     def convert(self, **kwargs):

~\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\internals\managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
    393                                             copy=align_copy)
    394 
--> 395             applied = getattr(b, f)(**kwargs)
    396             result_blocks = _extend_blocks(applied, result_blocks)
    397 

~\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\internals\blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
    532     def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs):
    533         return self._astype(dtype, copy=copy, errors=errors, values=values,
--> 534                             **kwargs)
    535 
    536     def _astype(self, dtype, copy=False, errors='raise', values=None,

~\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\internals\blocks.py in _astype(self, dtype, **kwargs)
   2137 
   2138         # delegate
-> 2139         return super(DatetimeBlock, self)._astype(dtype=dtype, **kwargs)
   2140 
   2141     def _can_hold_element(self, element):

~\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\internals\blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
    631 
    632                     # _astype_nansafe works fine with 1-d only
--> 633                     values = astype_nansafe(values.ravel(), dtype, copy=True)
    634 
    635                 # TODO(extension)

~\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\dtypes\cast.py in astype_nansafe(arr, dtype, copy, skipna)
    644         raise TypeError("cannot astype a datetimelike from [{from_dtype}] "
    645                         "to [{to_dtype}]".format(from_dtype=arr.dtype,
--> 646                                                  to_dtype=dtype))
    647 
    648     elif is_timedelta64_dtype(arr):

TypeError: cannot astype a datetimelike from [datetime64[ns]] to [int32]

我需要对时间戳信息进行某种转换吗?它会是什么样子?谢谢!

更新

之前有人问过类似的问题,我已经阅读过,但看不到如何将其应用于我的问题,如果其他人知道,希望能得到解释。可以在这里找到:

Pandas DataFrame - 'cannot astype a datetimelike from [datetime64[ns]] to [float64]' when using ols/linear regression

【问题讨论】:

  • @jezrael,我确实看到了那个答案,但它没有回答我的特定问题,而且我不完全确定错误的含义,所以也希望得到解释。另一个答案也没有解释其背后的“原因”。希望为我的问题找到一个可行的解决方案并解释原因,以便我可以从中学习。谢谢!
  • 当然,可以添加一些数据来测试您的解决方案吗?
  • @jezrael 查看这个项目:github.com/PacktPublishing/… ...向下滚动到 Order Book Depth 下的 In [102]: 以查看所有内容。这是我试图在我的机器上运行的项目,但是当我尝试它时遇到了这个错误。谢谢!

标签: python python-3.x pandas jupyter-notebook


【解决方案1】:

Pandas 无法将日期时间转换为 int32,因此引发错误。如果转换为 np.int64 它可以工作,也可以将 numpy 数组转换为具有错误值的int 或转换为int64 - 然后在nanoseconds 中以本机格式获取日期时间:

rng = pd.date_range('2017-04-03 12:00:45', periods=3)
buy_per_min = pd.DataFrame({'timestamp': rng})  

from datetime import timedelta
utc_offset = timedelta(hours=4)

print (buy_per_min.timestamp.add(utc_offset))
0   2017-04-03 16:00:45
1   2017-04-04 16:00:45
2   2017-04-05 16:00:45
Name: timestamp, dtype: datetime64[ns]

print (buy_per_min.timestamp.add(utc_offset).values)
['2017-04-03T16:00:45.000000000' '2017-04-04T16:00:45.000000000'
 '2017-04-05T16:00:45.000000000']
print (buy_per_min.timestamp.add(utc_offset).values.astype(np.int64))
[1491235245000000000 1491321645000000000 1491408045000000000]

print (buy_per_min.timestamp.add(utc_offset).astype(np.int64))
0    1491235245000000000
1    1491321645000000000
2    1491408045000000000
Name: timestamp, dtype: int64

#https://stackoverflow.com/a/12716674
print (buy_per_min.timestamp.add(utc_offset).values.astype(int))
[ -289111552 -2146205184   291668480]

【讨论】:

  • 太棒了!这为我解释了。感谢您的意见!
【解决方案2】:

我刚刚遇到了一个非常相似的问题:

TypeError: cannot astype a datetimelike from [datetime64[ns]] to [bool]

就我而言,问题是通过添加大括号解决的。比较一下:

df2 = df[
    (df['column1'] != df['column2']) &
    df['column3'] >= '03.02.2020'
].copy()

到这里:

df2 = df[
    (df['column1'] != df['column2']) &
    (df['column3'] >= '03.02.2020')
].copy()

在我的情况下,错误消息似乎只是由 &amp; 运算符被应用于基于日期时间的列 column3 触发。

【讨论】:

    猜你喜欢
    • 2018-11-22
    • 2022-08-15
    • 2020-05-20
    • 1970-01-01
    • 2015-11-02
    • 2023-03-08
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    相关资源
    最近更新 更多