yfinance

from the yfinance library how can I read the ex-dividend date?


This code should return the ex-dividend date:

import yfinance as yf

yf.Ticker('AGNC').info['exDividendDate']

but I get this as an output: 1661817600

I am wondering if there is a way to get the date from that number ?


Solution

  • It looks like this number is obtained based on seconds. In order to get the real date, you can use pd.to_datetime to convert the seconds to calendar date.

    import pandas as pd
    
    pd.to_datetime(1661817600, unit='s')
    Out[6]: Timestamp('2022-08-30 00:00:00')
    

    or you can use the built-in datetime package in Python.

    from datetime import datetime
    
    print(datetime.fromtimestamp(1661817600))
    2022-08-30 08:00:00