Yariv
Yariv

Reputation: 13321

How to convert from pandas.DatetimeIndex to numpy.datetime64?

How to convert from pandas.DatetimeIndex to numpy.datetime64?

I get:

>>> type(df.index.to_datetime())
Out[56]: pandas.tseries.index.DatetimeIndex

Is it safe to do numpy.array(datetimeindex,dtype=numpy.datetime64)?

Upvotes: 13

Views: 24586

Answers (3)

Majo_Jose
Majo_Jose

Reputation: 832

If your index is supposed to be the date or datetime values, you can convert it to a DatetimeIndex

   df.index = pd.to_datetime(df.index)

Upvotes: 1

Wes McKinney
Wes McKinney

Reputation: 105471

The data inside is of datetime64 dtype (datetime64[ns] to be precise). Just take the values attribute of the index. Note it will be nanosecond unit.

Upvotes: 15

Calvin Cheng
Calvin Cheng

Reputation: 36506

I would do this:-

new_array = np.array(df.index.to_pydatetime(), dtype=numpy.datetime64)

using the to_pydatetime() method.

Upvotes: 9

Related Questions