pandas filtering and comparing dates

后端 未结 2 1936
广开言路
广开言路 2020-12-05 11:22

I have a sql file which consists of the data below which I read into pandas.

df = pandas.read_sql(\'Database count details\', con=engine,
                            


        
2条回答
  •  广开言路
    2020-12-05 11:42

    Using datetime.date(2019, 1, 10) works because pandas coerce the date to a date time under the hood. This however, will no longer be the case in future versions of pandas.

    From version 0.24 and up, it now issue a warning:

    FutureWarning: Comparing Series of datetimes with 'datetime.date'. Currently, the 'datetime.date' is coerced to a datetime. In the future pandas will not coerce, and a TypeError will be raised.

    The better solution is the one proposed on its official documentation as Pandas replacement for python datetime.datetime object.

    To provide an example referencing OP's initial dataset, this is how you would use it:

    import pandas
    cond1 = df.newest_date_available < pd.Timestamp(2016,1,10)
    df.loc[cond1, ]
    

提交回复
热议问题