Python Time Series - Convert Date Index to Pandas Column

Hi All,


In a simple time series, how do you convert the timestamp, which is in an index to a pandas column formated in dateimte?

Best Answer

  • If you have a dataframe named 'df' with DatetimeIndex, the following command will add a column labeled 'Timestamp' and the values corresponding to the values of the index:

    df['Timestamp'] = df.index

    E.g.

    df = ek.get_timeseries('EUR=','CLOSE')
    df['Timestamp'] = df.index
    df.info()

    This returns:

    <class 'pandas.core.frame.DataFrame'>
    DatetimeIndex: 71 entries, 2020-09-28 to 2021-01-04
    Data columns (total 2 columns):
    # Column Non-Null Count Dtype
    --- ------ -------------- -----
    0 CLOSE 71 non-null float64
    1 Timestamp 71 non-null datetime64[ns]
    dtypes: datetime64[ns](1), float64(1)
    memory usage: 1.7 KB

Answers