Casting to unit-less dtype ‘datetime64’ is not supported.

huangapple go评论173阅读模式
英文:

Casting to unit-less dtype 'datetime64' is not supported

问题

这是您提供的代码的翻译:

我有一段Python代码用于指定模式然后创建一个空的数据框 这段Python代码在早期版本的Pandas和Numpy中运行良好 但是使用最新版本时它出现了错误

以下是代码

import pandas as pd
import numpy as np

schema = {'timestamp': np.datetime64, 'instrument_token': int, 'last_price': float, 'volume': int}
data = pd.DataFrame(columns=schema.keys()).astype(schema)

它抛出以下错误

TypeError不支持转换为无单位dtype 'datetime64'请改用'datetime64[ns]'

如果您能帮助解决这个问题我将不胜感激

谢谢和问候

请注意,我已经忽略了您提到的代码部分,只返回了翻译好的内容。如果您有任何其他问题或需要进一步的帮助,请随时告诉我。

英文:

I have a python code which specifies the schema and then creates an empty dataframe. This python code used to work fine in earlier versions of Pandas and Numpy. However, with the latest version, it fails.

Here is the code:

import pandas as pd
import numpy as np

schema = {'timestamp': np.datetime64, 'instrument_token': int, 'last_price': float, 'volume': int}
data = pd.DataFrame(columns=schema.keys()).astype(schema)

It throws the following error:

> TypeError: Casting to unit-less dtype 'datetime64' is not supported. Pass e.g. 'datetime64[ns]' instead.

I would appreciate if you can help resolve this.

thanks and regards,

答案1

得分: 1

你必须指定内部存储的单位('ns','ms','s',...):

import pandas as pd
import numpy as np

# 在这里指定 --v
schema = {'timestamp': 'datetime64[ns]', 'instrument_token': int, 'last_price': float, 'volume': int}
data = pd.DataFrame(columns=schema.keys()).astype(schema)

输出:

>>> data.dtypes
timestamp           datetime64[ns]
instrument_token             int64
last_price                 float64
volume                       int64
dtype: object
英文:

You have to specify the unit for internal storage ('ns', 'ms', 's', ...):

import pandas as pd
import numpy as np

#                           HERE --v
schema = {'timestamp': 'datetime64[ns]', 'instrument_token': int, 'last_price': float, 'volume': int}
data = pd.DataFrame(columns=schema.keys()).astype(schema)

Output:

>>> data.dtypes
timestamp           datetime64[ns]
instrument_token             int64
last_price                 float64
volume                       int64
dtype: object

答案2

得分: 0

我遇到了同样的问题。通过降级到pandas 1.5.3版本或任何早于2.0.0版本的版本来解决它。如果你使用的是pandas 1.5.3版本,你应该会看到一个弃用警告,但你的代码将能够正常工作。希望这有所帮助。

pip install pandas==1.5.3

如果你想让它在最新的pandas版本或从2.0.0版本开始的任何版本上工作,尝试将模式更新为类似这样:

{'timestamp': 'datetime64[ns]'}
英文:

I have the same problem. Solved it by downgrading to pandas 1.5.3 version or any version earlier than 2.0.0 . If you are on pandas version 1.5.3, you should see a depreciating warning on this but your code will work. Hope this helps.

pip install pandas==1.5.3

If you want to make it work on latest pandas version or any version from 2.0.0 onwards, try update the schema to something like this:

{'timestamp': 'datetime64[ns]'}

huangapple
  • 本文由 发表于 2023年6月5日 13:44:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定