Parse a Pandas column to Datetime when importing table from SQL database and filtering rows by date

后端 未结 5 873
执念已碎
执念已碎 2020-12-01 09:22

I have a DataFrame with column named date. How can we convert/parse the \'date\' column to a DateTime object?

I loaded the dat

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 09:56

    Pandas is aware of the object datetime but when you use some of the import functions it is taken as a string. So what you need to do is make sure the column is set as the datetime type not as a string. Then you can make your query.

    df['date']  = pd.to_datetime(df['date'])
    df_masked = df[(df['date'] > datetime.date(2012,4,1)) & (df['date'] < datetime.date(2012,4,4))]
    

提交回复
热议问题