【问题标题】:Parsing date from fetched dataframe - Python从获取的数据框中解析日期 - Python
【发布时间】:2020-09-29 13:16:00
【问题描述】:

请你看一下好吗?我试图在我的数据框中有一个索引为 1980-12-12 的列,所以基本上是日期。目前,当我尝试选择此列时,我无法定位它。

我知道如何为 csv/excel 文件执行此操作,但如何索引/创建日期为 1980-12-12 的列?

谢谢!

为我的体积图获取数据的功能:

from yahooquery import Ticker

def getVolumeGraph():
    aapl = Ticker('aapl')

    df = aapl.history(period='max', interval='1d')

    print(df.info())
    print(df)

    #return fig

df

                           high       close  ...  dividends  splits
1980-12-12 14:30:00    0.515625    0.513393  ...        0.0     0.0
1980-12-15 14:30:00    0.488839    0.486607  ...        0.0     0.0
1980-12-16 14:30:00    0.453125    0.450893  ...        0.0     0.0
1980-12-17 14:30:00    0.464286    0.462054  ...        0.0     0.0
1980-12-18 14:30:00    0.477679    0.475446  ...        0.0     0.0
...                         ...         ...  ...        ...     ...
2020-06-03 13:30:00  326.200012  325.119995  ...        0.0     0.0
2020-06-04 13:30:00  325.619995  322.320007  ...        0.0     0.0
2020-06-05 13:30:00  331.750000  331.500000  ...        0.0     0.0
2020-06-08 13:30:00  333.600006  333.459991  ...        0.0     0.0
2020-06-09 13:30:00  345.609985  343.989990  ...        0.0     0.0

df.info()

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 9957 entries, 1980-12-12 14:30:00 to 2020-06-09 13:30:00
Data columns (total 8 columns):
 #   Column     Non-Null Count  Dtype  
---  ------     --------------  -----  
 0   high       9957 non-null   float64
 1   close      9957 non-null   float64
 2   low        9957 non-null   float64
 3   volume     9957 non-null   float64
 4   open       9957 non-null   float64
 5   adjclose   9957 non-null   float64
 6   dividends  9957 non-null   float64
 7   splits     9957 non-null   float64
dtypes: float64(8)
memory usage: 1020.1 KB

【问题讨论】:

  • df.index.date?
  • “你不能定位它”是什么意思?期望的输出是什么?
  • 不是列,是df的索引。 df.info() 对您来说非常明显:“DatetimeIndex:9957 个条目,1980-12-12 14:30:00 到 2020-06-09 13:30:00”
  • 哦,是的,早上的心灵打击。谢谢大家。

标签: python pandas


【解决方案1】:

以下是您可以使用的一些潜在解决方案:

import pandas as pd
from yahooquery import Ticker
from datetime import datetime, date

aapl = Ticker('aapl')
df = aapl.history(period='max', interval='1d')

df

                      volume       low      high     close      open      adjclose  dividends  splits
1980-12-12 14:30:00  117258400.0  0.513393  0.515625  0.513393  0.513393  0.405683        0.0     0.0
1980-12-15 14:30:00   43971200.0  0.486607  0.488839  0.486607  0.488839  0.384517        0.0     0.0
1980-12-16 14:30:00   26432000.0  0.450893  0.453125  0.450893  0.453125  0.356296        0.0     0.0
1980-12-17 14:30:00   21610400.0  0.462054  0.464286  0.462054  0.462054  0.365115        0.0     0.0
1980-12-18 14:30:00   18362400.0  0.475446  0.477679  0.475446  0.475446  0.375698        0.0     0.0

选项 1. 去掉时间戳并重命名索引:

df.index = df.index.normalize()
df.index.name = 'Date'

Date        volume       low      high     close      open       adjclose  dividends  splits                                                                                
1980-12-12  117258400.0  0.513393  0.515625  0.513393  0.513393  0.405683        0.0     0.0
1980-12-15   43971200.0  0.486607  0.488839  0.486607  0.488839  0.384517        0.0     0.0
1980-12-16   26432000.0  0.450893  0.453125  0.450893  0.453125  0.356296        0.0     0.0
1980-12-17   21610400.0  0.462054  0.464286  0.462054  0.462054  0.365115        0.0     0.0
1980-12-18   18362400.0  0.475446  0.477679  0.475446  0.475446  0.375698        0.0     0.0

选项2:只需在前面添加一个日期列

df.insert(0, 'Date', df.index.normalize())

                          Date      open     close      high       volume  \
1980-12-12 14:30:00 1980-12-12  0.513393  0.513393  0.515625  117258400.0   
1980-12-15 14:30:00 1980-12-15  0.488839  0.486607  0.488839   43971200.0   
1980-12-16 14:30:00 1980-12-16  0.453125  0.450893  0.453125   26432000.0   
1980-12-17 14:30:00 1980-12-17  0.462054  0.462054  0.464286   21610400.0   
1980-12-18 14:30:00 1980-12-18  0.475446  0.475446  0.477679   18362400.0   

选项3:在前面添加一个日期列,然后重置索引

df.insert(0, 'Date', df.index.normalize())
df.reset_index(inplace=True, drop=True)

        Date      high       volume     close      open       low  adjclose  \
0 1980-12-12  0.515625  117258400.0  0.513393  0.513393  0.513393  0.405683   
1 1980-12-15  0.488839   43971200.0  0.486607  0.488839  0.486607  0.384517   
2 1980-12-16  0.453125   26432000.0  0.450893  0.453125  0.450893  0.356296   
3 1980-12-17  0.464286   21610400.0  0.462054  0.462054  0.462054  0.365115   
4 1980-12-18  0.477679   18362400.0  0.475446  0.475446  0.475446  0.375698  

很难确定你在追求什么,但也许一个关于 pandas 的快速教程不会有什么坏处 https://www.tutorialspoint.com/python_pandas/index.htm 因为您似乎认为 Excel 知识直接翻译成 python pandas 没有学习曲线。

【讨论】:

    猜你喜欢
    • 2016-09-01
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    相关资源
    最近更新 更多