无法将日期转换为datetime64 [英] Can't convert dates to datetime64
问题描述
以下代码段:
import pandas as pd
import numpy as np
data = pd.DataFrame({'date': ('13/02/2012', '14/02/2012')})
data['date'] = data['date'].astype('datetime64')
在一台计算机(Windows)上可以正常运行,而在另一台计算机(Linux)上不能运行. numpy和pandas都安装在两者上.
works fine on one machine (windows) and doesn't work on another (linux). Both numpy and pandas are installed on both.
我得到的错误是:
ValueError: Cannot create a NumPy datetime other than NaT with generic units
此错误是什么意思?我第一次看到它,网上找不到很多东西.是否缺少某种依赖性?
What does this error mean? I see it for the first time ever and there is not much on the web I can find. Is it some missing dependency?
推荐答案
请改为执行此操作.熊猫在内部将日期时间保留为datetime64[ns]
.转换次数
像这样的越野车(由于各种numpy版本(尤其是1.6.2)中的问题).使用
熊猫例程,然后像实际的日期时间对象一样运行.你想做什么?
Do this instead. Pandas keeps datestimes internally as datetime64[ns]
. Conversions
like this are very buggy (because of issues in various numpy version, 1.6.2 especially). Use
the pandas routines, then operate like thesee are actual datetime objects. What are you trying to do?
In [30]: pandas.to_datetime(data['date'])
Out[30]:
0 2012-02-13 00:00:00
1 2012-02-14 00:00:00
Name: date, dtype: datetime64[ns]
这篇关于无法将日期转换为datetime64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!