Reindexing pandas timeseries from object dtype to datetime dtype

后端 未结 1 641
陌清茗
陌清茗 2021-01-31 16:32

I have a time-series that is not recognized as a DatetimeIndex despite being indexed by standard YYYY-MM-DD strings with valid dates. Coercing them to a valid DatetimeIndex seem

1条回答
  •  执笔经年
    2021-01-31 17:15

    You could use pd.to_datetime:

    In [1]: import pandas as pd
    
    In [2]: pd.to_datetime('2008-02-27')
    Out[2]: datetime.datetime(2008, 2, 27, 0, 0)
    

    This allows you to "clean" the index (or similarly a column) by applying it to the Series:

    df.index = pd.to_datetime(df.index)
    

    or

    df['date_col'] = df['date_col'].apply(pd.to_datetime)
    

    0 讨论(0)
提交回复
热议问题