-2

I have a simple code for getting a stock price data that looks like this.

import yfinance as yf
import datetime
import pandas as pd

#To get BNI price data
getBNI = yf.Ticker("BBNI.JK")
priceBNI = getBNI.history(period="1mo")

I would like to extract the dates within the 'priceBNI' variable but I am unable to. Any idea how to extract the dates? Thank you in advance.

1 Answers1

0

priceBNI is a pd.Dataframe, but I'm not sure if "Date" is a column or the index, so I'll cover both cases.

If "Date" is a column, just access using its name:

priceBNI["Date"]

If "Date" is the dataframe index, access the index attribute:

priceBNI.index

Both values will return a "Series-like" value. If you want to use the index like a series, use priceBNI.index.to_series(). If you want any of those as a list, add .tolist() at the end, to transform the values in the Series into a Python list.

So, assuming that the Date is the index, you could get a list of its values using this:

priceBNI.index.tolist()
aaossa
  • 3,763
  • 2
  • 21
  • 34