[Solved] Parsing date from fetched dataframe – Python


Here are some potential solutions you could use:

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

Option 1. Get rid of the the timestamp and rename the index:

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

Option 2: Just add a date column in front

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   

Option 3: Add a Date column in front and then reset the index

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  

It’s hard to be sure what you’re after, but maybe a quick tutorial in pandas wouldn’t hurt
https://www.tutorialspoint.com/python_pandas/index.htm since you seem to be thinking that Excel knowledge is directly translated into python pandas without a learning curve.

solved Parsing date from fetched dataframe – Python