Search code examples
pythondatetimeyahoo-financepandas-datareader

How to show time of data downloaded by mplfinance (index type datetime)


My code is as follows:

import pandas_datareader as web
import datetime as dt

data = web.DataReader('AMZN', 'yahoo', dt.datetime(2021, 5, 3), dt.datetime(2021, 5, 5))
# read stock price of AMZN from Yahoo finance from 5/3/2021 to 5/5/2021

print(data)
print(data.index)
print(data.index[0])

Output: enter image description here

The interesting thing is that the index column does not show time (its type is datetime), see the first print. But if I try to see an individual row, it does include the time (00:00:00, see the 3rd print). How can I include this time to the index while still keeping the type to be datatime? I don't understand why the time is not included in the index column.

In addition to curiosity, the second reason I try to do this is that I want to see the download time of the data.


Solution

  • First a couple corrections:

    1. Although you may be visualizing your financial data with mplfinance, you are downloading it with Pandas DataReader.
    2. Your question is really a question about the Pandas DatetimeIndex class (which mplfinance requires as the index to the dataframe you want to plot).
    3. The type in a DatetimeIndex is not datetime.datetime, but rather numpy.datetime64. That said, if you try to extract it as data.index[0] then pandas will return type pandas.Timestamp. (See also here).

    To answer your question: What you are seeing is a feature of Pandas DatetimeIndex: If it detects that the time portion of all of the Timestamps in a DatetimeIndex are all 00:00:00, then to save space and avoid confusion it will automatically not print the time portion. (See also this question and its answers where someone asked how to remove the time portion.)

    Regarding Pandas DataReader, the download time is not going to be included in the dataframe, and since you are requesting daily data (not intraday data) each row corresponds to a specific date, referring to the entire trading day (Open,High,Low,Close) on that date, not referring to a specific time, therefore the data is not going to contain times (or will contain 00:00:00) for the various dates.

    HTH.

    P.S. I'm not sure if you can get intraday data from 'yahoo' using pandas datareader. Maybe; I don't know. But I believe you can get intraday data using Alpha Vantage by specifying "av-intraday" in the call.

    P.P.S. I don't understand why you want to know what time you actually downloaded the data. That said, you can get the current time when you do the download, and store that somewhere, for example:

    import pandas_datareader as web
    import datetime as dt
    data = web.DataReader(...)
    download_time = dt.datetime.now().strftime("%D %H:%M:%S")
    print(download_time)
    

    05/23/21 11:05:02