Pandas set_index does not set the index

后端 未结 1 924
旧巷少年郎
旧巷少年郎 2020-12-02 15:24

Say I create a pandas DataFrame with two columns, b (a DateTime) and c (an integer). Now I want to make a DatetimeIndex from the values in the firs

相关标签:
1条回答
  • 2020-12-02 16:25

    set_index is not inplace (unless you pass inplace=True). otherwise all correct

    In [7]: df = df.set_index(pd.DatetimeIndex(df['b']))
    
    In [8]: df
    Out[8]: 
    <class 'pandas.core.frame.DataFrame'>
    DatetimeIndex: 100 entries, 2013-06-14 09:10:23.523845 to 2013-06-14 10:12:51.650043
    Data columns (total 2 columns):
    b    100  non-null values
    c    100  non-null values
    dtypes: datetime64[ns](1), int64(1)
    

    also as a FYI, in forthcoming 0.12 release (next week), you can pass unit=us to specify units of microseconds since epoch

    In [13]: pd.to_datetime(a,unit='us')
    Out[13]: 
    <class 'pandas.tseries.index.DatetimeIndex'>
    [2013-06-14 13:10:23.523845, ..., 2013-06-14 14:12:51.650043]
    Length: 100, Freq: None, Timezone: None
    
    0 讨论(0)
提交回复
热议问题