'DataFrame' object has no attribute 'DatetimeIndex'

Question:

I am trying to return a Pandas with Date that has been set as the DateTimeIndex. I have tried many things similar to

inx=OutputDataSet.DatetimeIndex.to_pydatetime()

or

inx=OutputDataSet.DatetimeIndex.to_pydatetime(format =''%y-%m-%d'')

But I keep getting the error

'DataFrame' object has no attribute 'DatetimeIndex'

This is what the info attribute is showing;

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1047 entries, 2017-01-03 to 2021-02-19
Data columns (total 6 columns):
Open         1047 non-null float64
High         1047 non-null float64
Low          1047 non-null float64
Close        1047 non-null float64
Adj Close    1047 non-null float64
Volume       1047 non-null int64
dtypes: float64(5), int64(1)
memory usage: 57.3 KB

So clearly there is a DatetimeIndex

The print output has no problem displaying the DatetimeIndex

                     Open       High   ...     Adj Close   Volume
Date                               ...                       
2017-01-03   2.870000   2.940000   ...      2.842312   146804
2017-01-04   2.950000   3.090000   ...      2.871415   292371
2017-01-05   3.000000   3.000000   ...      2.871415   186374
2017-01-06   2.950000   2.970000   ...      2.764707   141723
2017-01-09   2.900000   2.970000   ...      2.842312    60815

Ideally I would like the DataFrame to present as

    Date         1047 non-null datetime
    Open         1047 non-null float64
    High         1047 non-null float64
    Low          1047 non-null float64
    Close        1047 non-null float64
    Adj Close    1047 non-null float64
    Volume       1047 non-null int64

Any help would be appreciated

Asked By: Ben Watson

||

Answers:

I think DatetimeIndex is the type of index you have on your pandas.DataFrame. Every DataFrame comes with the property index and index could be of different types from DateTimeIndex to PeriodIndex and TimedeltaIndex (guide here)

So in your output, Date IS your index. You might want to rethink using pandas DataFrame if that’s not the output you are expecting.

Answered By: Rikki

Solution is the following;

OutputDataSet.insert(loc=0,column=''Date'', value=pd.to_datetime(OutputDataSet.index))
Answered By: Ben Watson

If OutputDataSet is your dataFrame, you should call DatetimeIndex as a method in pandas and not the dataFrame.
You will want to call pd.DatetimeIndex and not OutputDataSet.DatetimeIndex.
Same to to_pydatetime. It should be pd.to_pydatetime

Answered By: George Odette