Home > Net >  How to fix date time format in Pandas Python
How to fix date time format in Pandas Python

Time:05-28

I have a data Frame df enter image description here so i converted it to date time using pandas, stock_data['Date'] = pd.to_datetime(stock_data['Date'],unit='s')

enter image description here

but when i'm plotting this, i get this.... enter image description here

I think this is not a right format for date and time so, i tried converting this to time stamp.

df['date'] = pd.DatetimeIndex(df.Date).asi8
but then also no luck how do i fix this?

CodePudding user response:

In my opinion, there are two solutions.

1.I downloaded the data from yfinance and specifically reset the indexes to turn the 'Datetime' indexes into a column. And I draw a scatter, specifying a list of indexes. Possible that you have these indexes and have buy?

import matplotlib.pyplot as plt
import yfinance as yf

df = yf.download(tickers='AAPL', period="3d", interval="15m")
df = df.reset_index()

plt.scatter(df['Datetime'].values[[7, 55, 70]], df['Close'].values[[7, 55, 70]], color='magenta')
plt.plot(df['Datetime'].values, df['Close'].values)
plt.legend(['stock', 'point'])
plt.show()

2.And also try to specify indexes in plt.plot, when drawing line, something like this:

plt.plot(df['C'].index, df['C'])

Perhaps because of this, such a picture turns out that the scatter appears somewhere at the beginning of the indexes.

  • Related