Posted By: Anonymous
I was trying to find a way to separate the date from the time in part of my code. When I run the code I get the following result:
However I would like it to just be ‘2021-05-27’. If anybody can help me with this I’d appreciate it, thanks.
import datetime as dt from datetime import date import matplotlib.pyplot as plt from matplotlib import style import pandas as pd import pandas_datareader as web import os start=dt.datetime(2020,1,1) end=date.today() df=web.DataReader("AAPL",'yahoo',start,end) newlast_day=df.index[-1] print(newlast_day)
newlast_day is a Timestamp rather than string. It has time zone information (although the time zone values are all zeros). If you just want to get the time string without the time zone part, you can use the
pandas.DatetimeIndex.strftime() function to format the display when you get it from a pandas DatetimeIndex object, as follows:
newlast_day = df.index[-1].strftime('%Y-%m-%d')