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

后端 未结 5 871
执念已碎
执念已碎 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:36

    pandas already reads that as a datetime object! So what you want is to select rows between two dates and you can do that by masking:

    df_masked = df[(df.date > '2012-04-01') & (df.date < '2012-04-04')]
    

    Because you said that you were getting an error from the string for some reason, try this:

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

提交回复
热议问题