python - pandas raises ValueError on DatetimeIndex Conversion -


i converting iso-8601 formatted values unix values. inexplicable reason line

a_col = pd.datetimeindex(a_col).astype(np.int64)/10**6 

raises error

valueerror: unable convert 0 2001-06-29

... (abbreviated output of column

name: datecol, dtype: datetime64[ns] datetime dtype

this odd because i've guaranteed each value in datetime.datetime format can see here:

if a_col.dtypes (np.dtype('object') or np.dtype('o')):       a_col = a_col.apply(lambda x: x if isinstance(x, datetime.datetime) else epoch) a_col = pd.datetimeindex(a_col).astype(np.int64)/10**6 

epoch datetime.datetime.

when check dtypes of column gives me error it's "object), i'm checking for. there i'm missing?

assuming time zone us/eastern (based on dataset) , dataframe named df, please try following:

import datetime dt time import mktime import pytz  df['job start date'] = \     df['job start date'].apply(lambda x: mktime(pytz.timezone('us/eastern').localize(x)                                          .astimezone(pytz.utc).timetuple()))  >>> df['job start date'].head() 0     993816000 1    1080824400 2    1052913600 3    1080824400 4    1075467600 name: job start date, dtype: float64 

you first need make 'naive' datetime objects timezone aware (to us/eastern) , convert them utc. finally, pass new utc aware datetime object timetable mtkime function time module.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -