Python and PowerBI Date

I'm new to Python, but have successfully pulled Eikon data and used in PowerBI. I'm attempting to pull data changes between two periods, and I'm able to do so in Python via diff, but when loading in Python lose the Timestamp (I get the difference in days, as diff seems to give change in the data I care about and the days):


from datetime import date, timedelta

import pandas as pd

import eikon as ek


ek.set_app_key('MY APP KEY')


stocks = ek.get_timeseries('DST-STK-T-EIA',

fields=['Close','Timestamp'],

start_date=(date.today()-timedelta(days=(13*365))).strftime("%Y-%m-%d"),

end_date=(date.today()+timedelta(days=(5*365))).strftime("%Y-%m-%d"),interval="weekly"

)

stocks['Timestamp'] = stocks.index


stocks_change = stocks.diff()

Best Answer

  • @Corey.Stewart

    Pandas diff method calculates the difference of a Dataframe element compared with another element in the Dataframe (default is element in previous row). It does not modify the index of the Dataframe, so the result of stocks.diff() in your example still has the timestamp in the Dataframe index. But it does modify all the columns including the column named 'Timestamp' that you create from the Dataframe index. If you would like to have the column containing the timestamp of the timeseries, you can simply create it from Dataframe index after executing the diff method. I.e. delete the line

    stocks['Timestamp'] = stocks.index 

    and at the end of your code snippet (after stocks_change = stocks.diff()) add

    stocks_change['Timestamp'] = stocks_change.index

Answers

  • Hi @Corey.Stewart,


    There are many ways to do this. I would keep the data in one data-frame to keep things simple:


    stocks = ek.get_timeseries(rics = 'DST-STK-T-EIA',
                               fields = ['Close','Timestamp'],
                               start_date = (date.today()-timedelta(days=(13*365))).strftime("%Y-%m-%d"),
                               end_date = (date.today()+timedelta(days=(5*365))).strftime("%Y-%m-%d"),interval="weekly")
    stocks['CLOSE1d'] = stocks.diff()["CLOSE"]


    Does this resolve your issue?