Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't convert dates to datetime64

The following piece of code:

import pandas as pd
import numpy as np

data = pd.DataFrame({'date': ('13/02/2012', '14/02/2012')})
data['date'] = data['date'].astype('datetime64')

works fine on one machine (windows) and doesn't work on another (linux). Both numpy and pandas are installed on both.

The error I get is:

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?

like image 824
sashkello Avatar asked Apr 23 '13 00:04

sashkello


People also ask

What is datetime64 in Numpy?

Starting in NumPy 1.7, there are core array data types which natively support datetime functionality. The data type is called datetime64 , so named because datetime is already taken by the Python standard library.


1 Answers

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]
like image 121
Jeff Avatar answered Sep 18 '22 21:09

Jeff