Reputation: 299
Is there a preferred way to convert a pandas DateTimeIndex to a column of YYYYMMDD integers? I need the YYYYMMDD integer format for legacy storage back into a pre-existing SQLite table that assumes dates are integers.
Pandas' to_sql() fails with an 'sqlite3.InterfaceError' when using the SQLite flavour and index=True. Apparently using an SQLalchemy flavor can convert the DateTimeIndex to string, but again I need to have this as an YYYYMMDD integer.
While there seems to be to_datetime() and to_pydatetime() functions, there doesn't seem to be a from_datetime() function.
Upvotes: 3
Views: 4589
Reputation: 365617
I'm confused by what you want. You're trying to convert a DateTimeIndex
to YYYYMMDD
format; why would you need a from_datetime
method to do that?
Anyway, you can either map/broadcast the Pandas Timestamp.strftime
function over it, or use that to_pydatetime
function that you found and then map the Python datetime.strftime
function over the resulting array.
Apparently using an SQLalchemy flavor can convert the DateTimeIndex to string, but again I need to have this as an YYYYMMDD integer.
That's easy. Given a YYYYMMDD
string, to convert that to an integer, you just call int
on it—or, if it's in a Pandas/Numpy array, you map or broadcast int
over it, or, even more simply, just cast its dtype.
For example:
>>> dti = pd.DatetimeIndex(['2014-08-31', '2014-09-01'])
>>> pdt = dti.to_pydatetime()
>>> sdt = np.vectorize(lambda s: s.strftime('%Y%m%d'))(pdt)
>>> idt = sdt.astype('I4')
>>> idt
array([20140831, 20140901], dtype=uint32)
(I'm not suggesting that this is the most efficient or most readable way to convert a DateTimeIndex
to an array of YYYYMMDD
integers, just that it's doable with the functions you appear to already know about, and if this isn't what you want, your question doesn't make sense.)
Upvotes: 1