0

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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

The package that I am calling is returning the DataFrame and the target system that is processing the data (SQL Server) is expecting a DataFrame to no getting away from Pandas.
Then df.index should return the date you're looking for. Working with Pandas is really easy, don't get me wrong. My point was, perhaps read the docs and see how you can get what you want out of it.
Got it - thank you Rikki. DateTimeIndex is an 'type' of 'property' index.
Precisely. So you could set df.index to df['timestamp'] or just call df.reset_index(inplace=True) which will reset your index to default (pandas.pydata.org/pandas-docs/stable/reference/api/…). Please accept the answer when you get a chance. Thank you.
1

Solution is the following;

OutputDataSet.insert(loc=0,column=''Date'', value=pd.to_datetime(OutputDataSet.index))

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.