pandas warning with pd.to_datetime

后端 未结 1 714
清酒与你
清酒与你 2021-01-15 01:52

Using pandas 0.6.2. I want to change a dataframe to datetime type, here is the dataframe

>>> tt.head()
0    2015-02-01 00:         


        
相关标签:
1条回答
  • 2021-01-15 02:20

    Just do it on the entire Series as to_datetime can operate on array-like args and assign directly to the column:

    In [72]:
    df['date'] = pd.to_datetime(df['date'])
    df.info()
    
    <class 'pandas.core.frame.DataFrame'>
    Int64Index: 5 entries, 0 to 4
    Data columns (total 1 columns):
    date    5 non-null datetime64[ns]
    dtypes: datetime64[ns](1)
    memory usage: 80.0 bytes
    
    In [73]:
    df
    
    Out[73]:
                         date
    index                    
    0     2015-02-01 00:46:28
    1     2015-02-01 00:59:56
    2     2015-02-01 00:16:27
    3     2015-02-01 00:33:45
    4     2015-02-01 13:48:29
    

    If you changed your loop to this then it would work:

    In [80]:
    for i in df.index:
        df.loc[i,'date']=pd.to_datetime(df.loc[i, 'date'])
    df
    
    Out[80]:
                          date
    index                     
    0      2015-02-01 00:46:28
    1      2015-02-01 00:59:56
    2      2015-02-01 00:16:27
    3      2015-02-01 00:33:45
    4      2015-02-01 13:48:29
    

    the code moans because you're operating on potentially a copy of that row on the df and not a view, using the new indexers avoids this ambiguity

    EDIT

    It looks like you're using an ancient version of pandas, the following should work:

    tt[1].apply(lambda x: x.hour)
    
    0 讨论(0)
提交回复
热议问题