“DataFrame”对象没有属性“DatetimeIndex”

2024-06-26 04:39:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试返回一个日期设置为DateTimeIndex的Pandas。我尝试过许多类似的事情

inx=OutputDataSet.DatetimeIndex.to_pydatetime()

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

但我一直在犯错误

'DataFrame' object has no attribute 'DatetimeIndex'

这是信息属性显示的内容

<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

因此,显然存在一个DatetimeIndex

打印输出显示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

理想情况下,我希望数据帧显示为

    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

任何帮助都将不胜感激


Tags: todataframecloseopennullnonhighvolume
2条回答

解决办法如下

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

我认为DatetimeIndex是pandas.DataFrame上的索引类型。每个DataFrame都带有属性index,并且index可以是从DateTimeIndexPeriodIndexTimedeltaIndexguide here)的不同类型

因此,在您的输出中,Date是您的索引。如果这不是您期望的输出,您可能需要重新考虑使用熊猫数据帧

相关问题 更多 >